Processes are the primitive units for allocation of system resources. Each process has its own address space and (usually) one thread of control. A process executes a program; you can have multiple processes executing the same program, but each process has its own copy of the program within its own address space and executes it independently of the other copies.
Processes are organized hierarchically. Each process has a parent process which explicitly arranged to create it. The processes created by a given parent are called its child processes. A child inherits many of its attributes from the parent process.
This chapter describes how a program can create, terminate, and control child processes. Actually, there are three distinct operations involved: creating a new child process, causing the new process to execute a program, and coordinating the completion of the child process with the original program.
The system
function provides a simple, portable mechanism for
running another program; it does all three steps automatically. If you
need more control over the details of how this is done, you can use the
primitive functions to do each step individually instead.
The easy way to run another program is to use the system
function. This function does all the work of running a subprogram, but
it doesn't give you much control over the details: you have to wait
until the subprogram terminates before you can do anything else.
sh
to run the command.
In particular, it searches the directories in PATH
to find
programs to execute. The return value is -1
if it wasn't
possible to create the shell process, and otherwise is the status of the
shell process. See section 23.6 Process Completion, for details on how this
status code can be interpreted.
The system
function is declared in the header file
`stdlib.h'.
Portability Note: Some C implementations may not have any
notion of a command processor that can execute other programs. You can
determine whether a command processor exists by executing
system (NULL)
; if the return value is nonzero, a command
processor is available.
The popen
and pclose
functions (see section 10.2 Pipe to a Subprocess) are closely related to the system
function. They
allow the parent process to communicate with the standard input and
output channels of the command being executed.
This section gives an overview of processes and of the steps involved in creating a process and making it run another program.
Each process is named by a process ID number. A unique process ID is allocated to each process when it is created. The lifetime of a process ends when its termination is reported to its parent process; at that time, all of the process resources, including its process ID, are freed.
Processes are created with the fork
system call (so the operation
of creating a new process is sometimes called forking a process).
The child process created by fork
is a copy of the original
parent process, except that it has its own process ID.
After forking a child process, both the parent and child processes
continue to execute normally. If you want your program to wait for a
child process to finish executing before continuing, you must do this
explicitly after the fork operation, by calling wait
or
waitpid
(see section 23.6 Process Completion). These functions give you
limited information about why the child terminated--for example, its
exit status code.
A newly forked child process continues to execute the same program as
its parent process, at the point where the fork
call returns.
You can use the return value from fork
to tell whether the program
is running in the parent process or the child.
Having several processes run the same program is only occasionally
useful. But the child can execute another program using one of the
exec
functions; see section 23.5 Executing a File. The program that the
process is executing is called its process image. Starting
execution of a new program causes the process to forget all about its
previous process image; when the new program exits, the process exits
too, instead of returning to the previous process image.
The pid_t
data type represents process IDs. You can get the
process ID of a process by calling getpid
. The function
getppid
returns the process ID of the parent of the current
process (this is also known as the parent process ID). Your
program should include the header files `unistd.h' and
`sys/types.h' to use these functions.
pid_t
data type is a signed integer type which is capable
of representing a process ID. In the GNU library, this is an int
.
getpid
function returns the process ID of the current process.
getppid
function returns the process ID of the parent of the
current process.
The fork
function is the primitive for creating a process.
It is declared in the header file `unistd.h'.
fork
function creates a new process.
If the operation is successful, there are then both parent and child
processes and both see fork
return, but with different values: it
returns a value of 0
in the child process and returns the child's
process ID in the parent process.
If process creation failed, fork
returns a value of -1
in
the parent process. The following errno
error conditions are
defined for fork
:
EAGAIN
RLIMIT_NPROC
resource limit, which can usually be increased;
see section 17.6 Limiting Resource Usage.
ENOMEM
The specific attributes of the child process that differ from the parent process are:
vfork
function is similar to fork
but on systems it
is more efficient; however, there are restrictions you must follow to
use it safely.
While fork
makes a complete copy of the calling process's
address space and allows both the parent and child to execute
independently, vfork
does not make this copy. Instead, the
child process created with vfork
shares its parent's address
space until it calls exits or one of the exec
functions. In the
meantime, the parent process suspends execution.
You must be very careful not to allow the child process created with
vfork
to modify any global data or even local variables shared
with the parent. Furthermore, the child process cannot return from (or
do a long jump out of) the function that called vfork
! This
would leave the parent process's control information very confused. If
in doubt, use fork
instead.
Some operating systems don't really implement vfork
. The GNU C
library permits you to use vfork
on all systems, but actually
executes fork
if vfork
isn't available. If you follow
the proper precautions for using vfork
, your program will still
work even if the system uses fork
instead.
This section describes the exec
family of functions, for executing
a file as a process image. You can use these functions to make a child
process execute a new program after it has been forked.
The functions in this family differ in how you specify the arguments, but otherwise they all do the same thing. They are declared in the header file `unistd.h'.
execv
function executes the file named by filename as a
new process image.
The argv argument is an array of null-terminated strings that is
used to provide a value for the argv
argument to the main
function of the program to be executed. The last element of this array
must be a null pointer. By convention, the first element of this array
is the file name of the program sans directory names. See section 22.1 Program Arguments, for full details on how programs can access these arguments.
The environment for the new process image is taken from the
environ
variable of the current process image; see
section 22.2 Environment Variables, for information about environments.
execv
, but the argv strings are
specified individually instead of as an array. A null pointer must be
passed as the last such argument.
execv
, but permits you to specify the environment
for the new program explicitly as the env argument. This should
be an array of strings in the same format as for the environ
variable; see section 22.2.1 Environment Access.
execl
, but permits you to specify the
environment for the new program explicitly. The environment argument is
passed following the null pointer that marks the last argv
argument, and should be an array of strings in the same format as for
the environ
variable.
execvp
function is similar to execv
, except that it
searches the directories listed in the PATH
environment variable
(see section 22.2.2 Standard Environment Variables) to find the full file name of a
file from filename if filename does not contain a slash.
This function is useful for executing system utility programs, because it looks for them in the places that the user has chosen. Shells use it to run the commands that users type.
execl
, except that it performs the same
file name searching as the execvp
function.
The size of the argument list and environment list taken together must
not be greater than ARG_MAX
bytes. See section 28.1 General Capacity Limits. In
the GNU system, the size (which compares against ARG_MAX
)
includes, for each string, the number of characters in the string, plus
the size of a char *
, plus one, rounded up to a multiple of the
size of a char *
. Other systems may have somewhat different
rules for counting.
These functions normally don't return, since execution of a new program
causes the currently executing program to go away completely. A value
of -1
is returned in the event of a failure. In addition to the
usual file name errors (see section 6.2.3 File Name Errors), the following
errno
error conditions are defined for these functions:
E2BIG
ARG_MAX
bytes. The GNU system has no
specific limit on the argument list size, so this error code cannot
result, but you may get ENOMEM
instead if the arguments are too
big for available memory.
ENOEXEC
ENOMEM
If execution of the new file succeeds, it updates the access time field of the file as if the file had been read. See section 9.8.9 File Times, for more details about access times of files.
The point at which the file is closed again is not specified, but is at some point before the process exits or before another process image is executed.
Executing a new process image completely changes the contents of memory, copying only the argument and environment strings to new locations. But many other attributes of the process are unchanged:
If the set-user-ID and set-group-ID mode bits of the process image file are set, this affects the effective user ID and effective group ID (respectively) of the process. These concepts are discussed in detail in section 26.2 The Persona of a Process.
Signals that are set to be ignored in the existing process image are also set to be ignored in the new process image. All other signals are set to the default action in the new process image. For more information about signals, see section 21 Signal Handling.
File descriptors open in the existing process image remain open in the
new process image, unless they have the FD_CLOEXEC
(close-on-exec) flag set. The files that remain open inherit all
attributes of the open file description from the existing process image,
including file locks. File descriptors are discussed in section 8 Low-Level Input/Output.
Streams, by contrast, cannot survive through exec
functions,
because they are located in the memory of the process itself. The new
process image has no streams except those it creates afresh. Each of
the streams in the pre-exec
process image has a descriptor inside
it, and these descriptors do survive through exec
(provided that
they do not have FD_CLOEXEC
set). The new process image can
reconnect these to new streams using fdopen
(see section 8.4 Descriptors and Streams).
The functions described in this section are used to wait for a child process to terminate or stop, and determine its status. These functions are declared in the header file `sys/wait.h'.
waitpid
function is used to request status information from a
child process whose process ID is pid. Normally, the calling
process is suspended until the child process makes status information
available by terminating.
Other values for the pid argument have special interpretations. A
value of -1
or WAIT_ANY
requests status information for
any child process; a value of 0
or WAIT_MYPGRP
requests
information for any child process in the same process group as the
calling process; and any other negative value - pgid
requests information for any child process whose process group ID is
pgid.
If status information for a child process is available immediately, this
function returns immediately without waiting. If more than one eligible
child process has status information available, one of them is chosen
randomly, and its status is returned immediately. To get the status
from the other eligible child processes, you need to call waitpid
again.
The options argument is a bit mask. Its value should be the
bitwise OR (that is, the `|' operator) of zero or more of the
WNOHANG
and WUNTRACED
flags. You can use the
WNOHANG
flag to indicate that the parent process shouldn't wait;
and the WUNTRACED
flag to request status information from stopped
processes as well as processes that have terminated.
The status information from the child process is stored in the object that status-ptr points to, unless status-ptr is a null pointer.
The return value is normally the process ID of the child process whose
status is reported. If the WNOHANG
option was specified and no
child process is waiting to be noticed, the value is zero. A value of
-1
is returned in case of error. The following errno
error conditions are defined for this function:
EINTR
ECHILD
EINVAL
These symbolic constants are defined as values for the pid argument
to the waitpid
function.
WAIT_ANY
-1
) specifies that
waitpid
should return status information about any child process.
WAIT_MYPGRP
0
) specifies that waitpid
should
return status information about any child process in the same process
group as the calling process.
These symbolic constants are defined as flags for the options
argument to the waitpid
function. You can bitwise-OR the flags
together to obtain a value to use as the argument.
WNOHANG
waitpid
should return immediately
instead of waiting, if there is no child process ready to be noticed.
WUNTRACED
waitpid
should report the status of any
child processes that have been stopped as well as those that have
terminated.
waitpid
, and is used to wait
until any one child process terminates. The call:
wait (&status)
is exactly equivalent to:
waitpid (-1, &status, 0)
wait4
is equivalent to
waitpid (pid, status-ptr, options)
.
If usage is not null, wait4
stores usage figures for the
child process in *rusage
(but only if the child has
terminated, not if it has stopped). See section 17.5 Resource Usage.
This function is a BSD extension.
Here's an example of how to use waitpid
to get the status from
all child processes that have terminated, without ever waiting. This
function is designed to be a handler for SIGCHLD
, the signal that
indicates that at least one child process has terminated.
void sigchld_handler (int signum) { int pid; int status; while (1) { pid = waitpid (WAIT_ANY, &status, WNOHANG); if (pid < 0) { perror ("waitpid"); break; } if (pid == 0) break; notice_termination (pid, status); } }
If the exit status value (see section 22.3 Program Termination) of the child
process is zero, then the status value reported by waitpid
or
wait
is also zero. You can test for other kinds of information
encoded in the returned status value using the following macros.
These macros are defined in the header file `sys/wait.h'.
exit
or _exit
.
WIFEXITED
is true of status, this macro returns the
low-order 8 bits of the exit status value from the child process.
See section 22.3.2 Exit Status.
WIFSIGNALED
is true of status, this macro returns the
signal number of the signal that terminated the child process.
WIFSTOPPED
is true of status, this macro returns the
signal number of the signal that caused the child process to stop.
The GNU library also provides these related facilities for compatibility
with BSD Unix. BSD uses the union wait
data type to represent
status values rather than an int
. The two representations are
actually interchangeable; they describe the same bit patterns. The GNU
C Library defines macros such as WEXITSTATUS
so that they will
work on either kind of object, and the wait
function is defined
to accept either type of pointer as its status-ptr argument.
These functions are declared in `sys/wait.h'.
int w_termsig
WTERMSIG
macro.
int w_coredump
WCOREDUMP
macro.
int w_retcode
WEXITSTATUS
macro.
int w_stopsig
WSTOPSIG
macro.
Instead of accessing these members directly, you should use the equivalent macros.
The wait3
function is the predecessor to wait4
, which is
more flexible. wait3
is now obsolete.
wait3
is equivalent to
waitpid (-1, status-ptr, options)
.
If usage is not null, wait3
stores usage figures for the
child process in *rusage
(but only if the child has
terminated, not if it has stopped). See section 17.5 Resource Usage.
Here is an example program showing how you might write a function
similar to the built-in system
. It executes its command
argument using the equivalent of `sh -c command'.
#include <stddef.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> /* Execute the command using this shell program. */ #define SHELL "/bin/sh" int my_system (const char *command) { int status; pid_t pid; pid = fork (); if (pid == 0) { /* This is the child process. Execute the shell command. */ execl (SHELL, SHELL, "-c", command, NULL); _exit (EXIT_FAILURE); } else if (pid < 0) /* The fork failed. Report failure. */ status = -1; else /* This is the parent process. Wait for the child to complete. */ if (waitpid (pid, &status, 0) != pid) status = -1; return status; }
There are a couple of things you should pay attention to in this example.
Remember that the first argv
argument supplied to the program
represents the name of the program being executed. That is why, in the
call to execl
, SHELL
is supplied once to name the program
to execute and a second time to supply a value for argv[0]
.
The execl
call in the child process doesn't return if it is
successful. If it fails, you must do something to make the child
process terminate. Just returning a bad status code with return
would leave two processes running the original program. Instead, the
right behavior is for the child process to report failure to its parent
process.
Call _exit
to accomplish this. The reason for using _exit
instead of exit
is to avoid flushing fully buffered streams such
as stdout
. The buffers of these streams probably contain data
that was copied from the parent process by the fork
, data that
will be output eventually by the parent process. Calling exit
in
the child would output the data twice. See section 22.3.5 Termination Internals.
Go to the first, previous, next, last section, table of contents.