02-02-2013, 11:07 AM
Processes
1Processes.pdf (Size: 236.09 KB / Downloads: 20)
INTRODUCTION
ARUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two
terminal windows showing on your screen, then you are probably running the
same terminal program twice—you have two terminal processes. Each terminal
window is probably running a shell; each running shell is another process.When you
invoke a command from a shell, the corresponding program is executed in a new
process; the shell process resumes when that process completes.
Advanced programmers often use multiple cooperating processes in a single application
to enable the application to do more than one thing at once, to increase
application robustness, and to make use of already-existing programs.
Most of the process manipulation functions described in this chapter are similar to
those on other UNIX systems. Most are declared in the header file
the man page for each function to be sure.
Looking at Processes
Even as you sit down at your computer, there are processes running. Every executing
program uses one or more processes. Let’s start by taking a look at the processes
already on your computer.
Process IDs
Each process in a Linux system is identified by its unique process ID, sometimes
referred to as pid. Process IDs are 16-bit numbers that are assigned sequentially by
Linux as new processes are created.
Every process also has a parent process (except the special init process, described in
Section 3.4.3,“Zombie Processes”).Thus, you can think of the processes on a Linux
system as arranged in a tree, with the init process at its root.The parent process ID, or
ppid, is simply the process ID of the process’s parent.
When referring to process IDs in a C or C++ program, always use the pid_t
typedef, which is defined in <sys/types.h>.A program can obtain the process ID of
the process it’s running in with the getpid() system call, and it can obtain the process
ID of its parent process with the getppid() system call. For instance, the program in
Listing 3.1 prints its process ID and its parent’s process ID.
Viewing Active Processes
The ps command displays the processes that are running on your system.The
GNU/Linux version of ps has lots of options because it tries to be compatible with
versions of ps on several other UNIX variants.These options control which processes
are listed and what information about each is shown.
Killing a Process
You can kill a running process with the kill command. Simply specify on the command
line the process ID of the process to be killed.
The kill command works by sending the process a SIGTERM, or termination,
signal.1 This causes the process to terminate, unless the executing program explicitly
handles or masks the SIGTERM signal. Signals are described in Section 3.3,“Signals.”
Creating Processes
Two common techniques are used for creating a new process.The first is relatively
simple but should be used sparingly because it is inefficient and has considerably
security risks.The second technique is more complex but provides greater flexibility,
speed, and security.
Using system
The system function in the standard C library provides an easy way to execute a
command from within a program, much as if the command had been typed into a
shell. In fact, system creates a subprocess running the standard Bourne shell (/bin/sh)
and hands the command to that shell for execution. For example, this program in
Listing 3.2 invokes the ls command to display the contents of the root directory, as if
you typed ls -l / into a shell.
Calling fork
When a program calls fork, a duplicate process, called the child process, is created.The
parent process continues executing the program from the point that fork was called.
The child process, too, executes the same program from the same place.
So how do the two processes differ? First, the child process is a new process and
therefore has a new process ID, distinct from its parent’s process ID.One way for a
program to distinguish whether it’s in the parent process or the child process is to call
getpid. However, the fork function provides different return values to the parent and
child processes—one process “goes in” to the fork call, and two processes “come out,”
with different return values.The return value in the parent process is the process ID of
the child.The return value in the child process is zero. Because no process ever has a
process ID of zero, this makes it easy for the program whether it is now running as the
parent or the child process.
Using fork and exec Together
A common pattern to run a subprogram within a program is first to fork the process
and then exec the subprogram.This allows the calling program to continue execution
in the parent process while the calling program is replaced by the subprogram in the
child process.
The program in Listing 3.4, like Listing 3.2, lists the contents of the root directory
using the ls command. Unlike the previous example, though, it invokes the ls command
directly, passing it the command-line arguments -l and / rather than invoking it
through a shell.
Process Scheduling
Linux schedules the parent and child processes independently; there’s no guarantee of
which one will run first, or how long it will run before Linux interrupts it and lets the
other process (or some other process on the system) run. In particular, none, part, or all
of the ls command may run in the child process before the parent completes.2 Linux
promises that each process will run eventually—no process will be completely starved
of execution resources.
Signals
Signals are mechanisms for communicating with and manipulating processes in Linux.
The topic of signals is a large one; here we discuss some of the most important signals
and techniques that are used for controlling processes.
A signal is a special message sent to a process. Signals are asynchronous; when a
process receives a signal, it processes the signal immediately, without finishing the current
function or even the current line of code.There are several dozen different signals,
each with a different meaning. Each signal type is specified by its signal number,
but in programs, you usually refer to a signal by its name.