Blame manual/process.texi

Packit 6c4009
@node Processes, Inter-Process Communication, Program Basics, Top
Packit 6c4009
@c %MENU% How to create processes and run other programs
Packit 6c4009
@chapter Processes
Packit 6c4009
Packit 6c4009
@cindex process
Packit 6c4009
@dfn{Processes} are the primitive units for allocation of system
Packit 6c4009
resources.  Each process has its own address space and (usually) one
Packit 6c4009
thread of control.  A process executes a program; you can have multiple
Packit 6c4009
processes executing the same program, but each process has its own copy
Packit 6c4009
of the program within its own address space and executes it
Packit 6c4009
independently of the other copies.
Packit 6c4009
Packit 6c4009
@cindex child process
Packit 6c4009
@cindex parent process
Packit 6c4009
Processes are organized hierarchically.  Each process has a @dfn{parent
Packit 6c4009
process} which explicitly arranged to create it.  The processes created
Packit 6c4009
by a given parent are called its @dfn{child processes}.  A child
Packit 6c4009
inherits many of its attributes from the parent process.
Packit 6c4009
Packit 6c4009
This chapter describes how a program can create, terminate, and control
Packit 6c4009
child processes.  Actually, there are three distinct operations
Packit 6c4009
involved: creating a new child process, causing the new process to
Packit 6c4009
execute a program, and coordinating the completion of the child process
Packit 6c4009
with the original program.
Packit 6c4009
Packit 6c4009
The @code{system} function provides a simple, portable mechanism for
Packit 6c4009
running another program; it does all three steps automatically.  If you
Packit 6c4009
need more control over the details of how this is done, you can use the
Packit 6c4009
primitive functions to do each step individually instead.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Running a Command::           The easy way to run another program.
Packit 6c4009
* Process Creation Concepts::   An overview of the hard way to do it.
Packit 6c4009
* Process Identification::      How to get the process ID of a process.
Packit 6c4009
* Creating a Process::          How to fork a child process.
Packit 6c4009
* Executing a File::            How to make a process execute another program.
Packit 6c4009
* Process Completion::          How to tell when a child process has completed.
Packit 6c4009
* Process Completion Status::   How to interpret the status value
Packit 6c4009
                                 returned from a child process.
Packit 6c4009
* BSD Wait Functions::  	More functions, for backward compatibility.
Packit 6c4009
* Process Creation Example::    A complete example program.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Running a Command
Packit 6c4009
@section Running a Command
Packit 6c4009
@cindex running a command
Packit 6c4009
Packit 6c4009
The easy way to run another program is to use the @code{system}
Packit 6c4009
function.  This function does all the work of running a subprogram, but
Packit 6c4009
it doesn't give you much control over the details: you have to wait
Packit 6c4009
until the subprogram terminates before you can do anything else.
Packit 6c4009
Packit 6c4009
@deftypefun int system (const char *@var{command})
Packit 6c4009
@standards{ISO, stdlib.h}
Packit 6c4009
@pindex sh
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
Packit 6c4009
@c system @ascuplugin @ascuheap @asulock @aculock @acsmem
Packit 6c4009
@c  do_system @ascuplugin @ascuheap @asulock @aculock @acsmem
Packit 6c4009
@c   sigemptyset dup ok
Packit 6c4009
@c   libc_lock_lock @asulock @aculock
Packit 6c4009
@c   ADD_REF ok
Packit 6c4009
@c   sigaction dup ok
Packit 6c4009
@c   SUB_REF ok
Packit 6c4009
@c   libc_lock_unlock @aculock
Packit 6c4009
@c   sigaddset dup ok
Packit 6c4009
@c   sigprocmask dup ok
Packit 6c4009
@c   CLEANUP_HANDLER @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c    libc_cleanup_region_start @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c     pthread_cleanup_push_defer @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c      CANCELLATION_P @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c       CANCEL_ENABLED_AND_CANCELED ok
Packit 6c4009
@c       do_cancel @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c    cancel_handler ok
Packit 6c4009
@c     kill syscall ok
Packit 6c4009
@c     waitpid dup ok
Packit 6c4009
@c     libc_lock_lock ok
Packit 6c4009
@c     sigaction dup ok
Packit 6c4009
@c     libc_lock_unlock ok
Packit 6c4009
@c   FORK ok
Packit 6c4009
@c    clone syscall ok
Packit 6c4009
@c   waitpid dup ok
Packit 6c4009
@c   CLEANUP_RESET ok
Packit 6c4009
@c    libc_cleanup_region_end ok
Packit 6c4009
@c     pthread_cleanup_pop_restore ok
Packit 6c4009
@c  SINGLE_THREAD_P ok
Packit 6c4009
@c  LIBC_CANCEL_ASYNC @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c   libc_enable_asynccancel @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c    CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS dup ok
Packit 6c4009
@c    do_cancel dup @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c  LIBC_CANCEL_RESET ok
Packit 6c4009
@c   libc_disable_asynccancel ok
Packit 6c4009
@c    lll_futex_wait dup ok
Packit 6c4009
This function executes @var{command} as a shell command.  In @theglibc{},
Packit 6c4009
it always uses the default shell @code{sh} to run the command.
Packit 6c4009
In particular, it searches the directories in @code{PATH} to find
Packit 6c4009
programs to execute.  The return value is @code{-1} if it wasn't
Packit 6c4009
possible to create the shell process, and otherwise is the status of the
Packit 6c4009
shell process.  @xref{Process Completion}, for details on how this
Packit 6c4009
status code can be interpreted.
Packit 6c4009
Packit 6c4009
If the @var{command} argument is a null pointer, a return value of zero
Packit 6c4009
indicates that no command processor is available.
Packit 6c4009
Packit 6c4009
This function is a cancellation point in multi-threaded programs.  This
Packit 6c4009
is a problem if the thread allocates some resources (like memory, file
Packit 6c4009
descriptors, semaphores or whatever) at the time @code{system} is
Packit 6c4009
called.  If the thread gets canceled these resources stay allocated
Packit 6c4009
until the program ends.  To avoid this calls to @code{system} should be
Packit 6c4009
protected using cancellation handlers.
Packit 6c4009
@c ref pthread_cleanup_push / pthread_cleanup_pop
Packit 6c4009
Packit 6c4009
@pindex stdlib.h
Packit 6c4009
The @code{system} function is declared in the header file
Packit 6c4009
@file{stdlib.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@strong{Portability Note:} Some C implementations may not have any
Packit 6c4009
notion of a command processor that can execute other programs.  You can
Packit 6c4009
determine whether a command processor exists by executing
Packit 6c4009
@w{@code{system (NULL)}}; if the return value is nonzero, a command
Packit 6c4009
processor is available.
Packit 6c4009
Packit 6c4009
The @code{popen} and @code{pclose} functions (@pxref{Pipe to a
Packit 6c4009
Subprocess}) are closely related to the @code{system} function.  They
Packit 6c4009
allow the parent process to communicate with the standard input and
Packit 6c4009
output channels of the command being executed.
Packit 6c4009
Packit 6c4009
@node Process Creation Concepts
Packit 6c4009
@section Process Creation Concepts
Packit 6c4009
Packit 6c4009
This section gives an overview of processes and of the steps involved in
Packit 6c4009
creating a process and making it run another program.
Packit 6c4009
Packit 6c4009
@cindex process ID
Packit 6c4009
@cindex process lifetime
Packit 6c4009
Each process is named by a @dfn{process ID} number.  A unique process ID
Packit 6c4009
is allocated to each process when it is created.  The @dfn{lifetime} of
Packit 6c4009
a process ends when its termination is reported to its parent process;
Packit 6c4009
at that time, all of the process resources, including its process ID,
Packit 6c4009
are freed.
Packit 6c4009
Packit 6c4009
@cindex creating a process
Packit 6c4009
@cindex forking a process
Packit 6c4009
@cindex child process
Packit 6c4009
@cindex parent process
Packit 6c4009
Processes are created with the @code{fork} system call (so the operation
Packit 6c4009
of creating a new process is sometimes called @dfn{forking} a process).
Packit 6c4009
The @dfn{child process} created by @code{fork} is a copy of the original
Packit 6c4009
@dfn{parent process}, except that it has its own process ID.
Packit 6c4009
Packit 6c4009
After forking a child process, both the parent and child processes
Packit 6c4009
continue to execute normally.  If you want your program to wait for a
Packit 6c4009
child process to finish executing before continuing, you must do this
Packit 6c4009
explicitly after the fork operation, by calling @code{wait} or
Packit 6c4009
@code{waitpid} (@pxref{Process Completion}).  These functions give you
Packit 6c4009
limited information about why the child terminated---for example, its
Packit 6c4009
exit status code.
Packit 6c4009
Packit 6c4009
A newly forked child process continues to execute the same program as
Packit 6c4009
its parent process, at the point where the @code{fork} call returns.
Packit 6c4009
You can use the return value from @code{fork} to tell whether the program
Packit 6c4009
is running in the parent process or the child.
Packit 6c4009
Packit 6c4009
@cindex process image
Packit 6c4009
Having several processes run the same program is only occasionally
Packit 6c4009
useful.  But the child can execute another program using one of the
Packit 6c4009
@code{exec} functions; see @ref{Executing a File}.  The program that the
Packit 6c4009
process is executing is called its @dfn{process image}.  Starting
Packit 6c4009
execution of a new program causes the process to forget all about its
Packit 6c4009
previous process image; when the new program exits, the process exits
Packit 6c4009
too, instead of returning to the previous process image.
Packit 6c4009
Packit 6c4009
@node Process Identification
Packit 6c4009
@section Process Identification
Packit 6c4009
Packit 6c4009
The @code{pid_t} data type represents process IDs.  You can get the
Packit 6c4009
process ID of a process by calling @code{getpid}.  The function
Packit 6c4009
@code{getppid} returns the process ID of the parent of the current
Packit 6c4009
process (this is also known as the @dfn{parent process ID}).  Your
Packit 6c4009
program should include the header files @file{unistd.h} and
Packit 6c4009
@file{sys/types.h} to use these functions.
Packit 6c4009
@pindex sys/types.h
Packit 6c4009
@pindex unistd.h
Packit 6c4009
Packit 6c4009
@deftp {Data Type} pid_t
Packit 6c4009
@standards{POSIX.1, sys/types.h}
Packit 6c4009
The @code{pid_t} data type is a signed integer type which is capable
Packit 6c4009
of representing a process ID.  In @theglibc{}, this is an @code{int}.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftypefun pid_t getpid (void)
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{getpid} function returns the process ID of the current process.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun pid_t getppid (void)
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{getppid} function returns the process ID of the parent of the
Packit 6c4009
current process.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Creating a Process
Packit 6c4009
@section Creating a Process
Packit 6c4009
Packit 6c4009
The @code{fork} function is the primitive for creating a process.
Packit 6c4009
It is declared in the header file @file{unistd.h}.
Packit 6c4009
@pindex unistd.h
Packit 6c4009
Packit 6c4009
@deftypefun pid_t fork (void)
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c The nptl/.../linux implementation safely collects fork_handlers into
Packit 6c4009
@c an alloca()ed linked list and increments ref counters; it uses atomic
Packit 6c4009
@c ops and retries, avoiding locking altogether.  It then takes the
Packit 6c4009
@c IO_list lock, resets the thread-local pid, and runs fork.  The parent
Packit 6c4009
@c restores the thread-local pid, releases the lock, and runs parent
Packit 6c4009
@c handlers, decrementing the ref count and signaling futex wait if
Packit 6c4009
@c requested by unregister_atfork.  The child bumps the fork generation,
Packit 6c4009
@c sets the thread-local pid, resets cpu clocks, initializes the robust
Packit 6c4009
@c mutex list, the stream locks, the IO_list lock, the dynamic loader
Packit 6c4009
@c lock, runs the child handlers, reseting ref counters to 1, and
Packit 6c4009
@c initializes the fork lock.  These are all safe, unless atfork
Packit 6c4009
@c handlers themselves are unsafe.
Packit 6c4009
The @code{fork} function creates a new process.
Packit 6c4009
Packit 6c4009
If the operation is successful, there are then both parent and child
Packit 6c4009
processes and both see @code{fork} return, but with different values: it
Packit 6c4009
returns a value of @code{0} in the child process and returns the child's
Packit 6c4009
process ID in the parent process.
Packit 6c4009
Packit 6c4009
If process creation failed, @code{fork} returns a value of @code{-1} in
Packit 6c4009
the parent process.  The following @code{errno} error conditions are
Packit 6c4009
defined for @code{fork}:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EAGAIN
Packit 6c4009
There aren't enough system resources to create another process, or the
Packit 6c4009
user already has too many processes running.  This means exceeding the
Packit 6c4009
@code{RLIMIT_NPROC} resource limit, which can usually be increased;
Packit 6c4009
@pxref{Limits on Resources}.
Packit 6c4009
Packit 6c4009
@item ENOMEM
Packit 6c4009
The process requires more space than the system can supply.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The specific attributes of the child process that differ from the
Packit 6c4009
parent process are:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
The child process has its own unique process ID.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The parent process ID of the child process is the process ID of its
Packit 6c4009
parent process.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The child process gets its own copies of the parent process's open file
Packit 6c4009
descriptors.  Subsequently changing attributes of the file descriptors
Packit 6c4009
in the parent process won't affect the file descriptors in the child,
Packit 6c4009
and vice versa.  @xref{Control Operations}.  However, the file position
Packit 6c4009
associated with each descriptor is shared by both processes;
Packit 6c4009
@pxref{File Position}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The elapsed processor times for the child process are set to zero;
Packit 6c4009
see @ref{Processor Time}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The child doesn't inherit file locks set by the parent process.
Packit 6c4009
@c !!! flock locks shared
Packit 6c4009
@xref{Control Operations}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The child doesn't inherit alarms set by the parent process.
Packit 6c4009
@xref{Setting an Alarm}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
The set of pending signals (@pxref{Delivery of Signal}) for the child
Packit 6c4009
process is cleared.  (The child process inherits its mask of blocked
Packit 6c4009
signals and signal actions from the parent process.)
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun pid_t vfork (void)
Packit 6c4009
@standards{BSD, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c The vfork implementation proper is a safe syscall, but it may fall
Packit 6c4009
@c back to fork if the vfork syscall is not available.
Packit 6c4009
The @code{vfork} function is similar to @code{fork} but on some systems
Packit 6c4009
it is more efficient; however, there are restrictions you must follow to
Packit 6c4009
use it safely.
Packit 6c4009
Packit 6c4009
While @code{fork} makes a complete copy of the calling process's address
Packit 6c4009
space and allows both the parent and child to execute independently,
Packit 6c4009
@code{vfork} does not make this copy.  Instead, the child process
Packit 6c4009
created with @code{vfork} shares its parent's address space until it
Packit 6c4009
calls @code{_exit} or one of the @code{exec} functions.  In the
Packit 6c4009
meantime, the parent process suspends execution.
Packit 6c4009
Packit 6c4009
You must be very careful not to allow the child process created with
Packit 6c4009
@code{vfork} to modify any global data or even local variables shared
Packit 6c4009
with the parent.  Furthermore, the child process cannot return from (or
Packit 6c4009
do a long jump out of) the function that called @code{vfork}!  This
Packit 6c4009
would leave the parent process's control information very confused.  If
Packit 6c4009
in doubt, use @code{fork} instead.
Packit 6c4009
Packit 6c4009
Some operating systems don't really implement @code{vfork}.  @Theglibc{}
Packit 6c4009
permits you to use @code{vfork} on all systems, but actually
Packit 6c4009
executes @code{fork} if @code{vfork} isn't available.  If you follow
Packit 6c4009
the proper precautions for using @code{vfork}, your program will still
Packit 6c4009
work even if the system uses @code{fork} instead.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Executing a File
Packit 6c4009
@section Executing a File
Packit 6c4009
@cindex executing a file
Packit 6c4009
@cindex @code{exec} functions
Packit 6c4009
Packit 6c4009
This section describes the @code{exec} family of functions, for executing
Packit 6c4009
a file as a process image.  You can use these functions to make a child
Packit 6c4009
process execute a new program after it has been forked.
Packit 6c4009
Packit 6c4009
To see the effects of @code{exec} from the point of view of the called
Packit 6c4009
program, see @ref{Program Basics}.
Packit 6c4009
Packit 6c4009
@pindex unistd.h
Packit 6c4009
The functions in this family differ in how you specify the arguments,
Packit 6c4009
but otherwise they all do the same thing.  They are declared in the
Packit 6c4009
header file @file{unistd.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int execv (const char *@var{filename}, char *const @var{argv}@t{[]})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{execv} function executes the file named by @var{filename} as a
Packit 6c4009
new process image.
Packit 6c4009
Packit 6c4009
The @var{argv} argument is an array of null-terminated strings that is
Packit 6c4009
used to provide a value for the @code{argv} argument to the @code{main}
Packit 6c4009
function of the program to be executed.  The last element of this array
Packit 6c4009
must be a null pointer.  By convention, the first element of this array
Packit 6c4009
is the file name of the program sans directory names.  @xref{Program
Packit 6c4009
Arguments}, for full details on how programs can access these arguments.
Packit 6c4009
Packit 6c4009
The environment for the new process image is taken from the
Packit 6c4009
@code{environ} variable of the current process image; see
Packit 6c4009
@ref{Environment Variables}, for information about environments.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int execl (const char *@var{filename}, const char *@var{arg0}, @dots{})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This is similar to @code{execv}, but the @var{argv} strings are
Packit 6c4009
specified individually instead of as an array.  A null pointer must be
Packit 6c4009
passed as the last such argument.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int execve (const char *@var{filename}, char *const @var{argv}@t{[]}, char *const @var{env}@t{[]})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is similar to @code{execv}, but permits you to specify the environment
Packit 6c4009
for the new program explicitly as the @var{env} argument.  This should
Packit 6c4009
be an array of strings in the same format as for the @code{environ}
Packit 6c4009
variable; see @ref{Environment Access}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int execle (const char *@var{filename}, const char *@var{arg0}, @dots{}, char *const @var{env}@t{[]})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This is similar to @code{execl}, but permits you to specify the
Packit 6c4009
environment for the new program explicitly.  The environment argument is
Packit 6c4009
passed following the null pointer that marks the last @var{argv}
Packit 6c4009
argument, and should be an array of strings in the same format as for
Packit 6c4009
the @code{environ} variable.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int execvp (const char *@var{filename}, char *const @var{argv}@t{[]})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{execvp} function is similar to @code{execv}, except that it
Packit 6c4009
searches the directories listed in the @code{PATH} environment variable
Packit 6c4009
(@pxref{Standard Environment}) to find the full file name of a
Packit 6c4009
file from @var{filename} if @var{filename} does not contain a slash.
Packit 6c4009
Packit 6c4009
This function is useful for executing system utility programs, because
Packit 6c4009
it looks for them in the places that the user has chosen.  Shells use it
Packit 6c4009
to run the commands that users type.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int execlp (const char *@var{filename}, const char *@var{arg0}, @dots{})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This function is like @code{execl}, except that it performs the same
Packit 6c4009
file name searching as the @code{execvp} function.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The size of the argument list and environment list taken together must
Packit 6c4009
not be greater than @code{ARG_MAX} bytes.  @xref{General Limits}.  On
Packit 6c4009
@gnuhurdsystems{}, the size (which compares against @code{ARG_MAX})
Packit 6c4009
includes, for each string, the number of characters in the string, plus
Packit 6c4009
the size of a @code{char *}, plus one, rounded up to a multiple of the
Packit 6c4009
size of a @code{char *}.  Other systems may have somewhat different
Packit 6c4009
rules for counting.
Packit 6c4009
Packit 6c4009
These functions normally don't return, since execution of a new program
Packit 6c4009
causes the currently executing program to go away completely.  A value
Packit 6c4009
of @code{-1} is returned in the event of a failure.  In addition to the
Packit 6c4009
usual file name errors (@pxref{File Name Errors}), the following
Packit 6c4009
@code{errno} error conditions are defined for these functions:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item E2BIG
Packit 6c4009
The combined size of the new program's argument list and environment
Packit 6c4009
list is larger than @code{ARG_MAX} bytes.  @gnuhurdsystems{} have no
Packit 6c4009
specific limit on the argument list size, so this error code cannot
Packit 6c4009
result, but you may get @code{ENOMEM} instead if the arguments are too
Packit 6c4009
big for available memory.
Packit 6c4009
Packit 6c4009
@item ENOEXEC
Packit 6c4009
The specified file can't be executed because it isn't in the right format.
Packit 6c4009
Packit 6c4009
@item ENOMEM
Packit 6c4009
Executing the specified file requires more storage than is available.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
If execution of the new file succeeds, it updates the access time field
Packit 6c4009
of the file as if the file had been read.  @xref{File Times}, for more
Packit 6c4009
details about access times of files.
Packit 6c4009
Packit 6c4009
The point at which the file is closed again is not specified, but
Packit 6c4009
is at some point before the process exits or before another process
Packit 6c4009
image is executed.
Packit 6c4009
Packit 6c4009
Executing a new process image completely changes the contents of memory,
Packit 6c4009
copying only the argument and environment strings to new locations.  But
Packit 6c4009
many other attributes of the process are unchanged:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
The process ID and the parent process ID.  @xref{Process Creation Concepts}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Session and process group membership.  @xref{Concepts of Job Control}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Real user ID and group ID, and supplementary group IDs.  @xref{Process
Packit 6c4009
Persona}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Pending alarms.  @xref{Setting an Alarm}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Current working directory and root directory.  @xref{Working
Packit 6c4009
Directory}.  On @gnuhurdsystems{}, the root directory is not copied when
Packit 6c4009
executing a setuid program; instead the system default root directory
Packit 6c4009
is used for the new program.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
File mode creation mask.  @xref{Setting Permissions}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Process signal mask; see @ref{Process Signal Mask}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Pending signals; see @ref{Blocking Signals}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Elapsed processor time associated with the process; see @ref{Processor Time}.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
If the set-user-ID and set-group-ID mode bits of the process image file
Packit 6c4009
are set, this affects the effective user ID and effective group ID
Packit 6c4009
(respectively) of the process.  These concepts are discussed in detail
Packit 6c4009
in @ref{Process Persona}.
Packit 6c4009
Packit 6c4009
Signals that are set to be ignored in the existing process image are
Packit 6c4009
also set to be ignored in the new process image.  All other signals are
Packit 6c4009
set to the default action in the new process image.  For more
Packit 6c4009
information about signals, see @ref{Signal Handling}.
Packit 6c4009
Packit 6c4009
File descriptors open in the existing process image remain open in the
Packit 6c4009
new process image, unless they have the @code{FD_CLOEXEC}
Packit 6c4009
(close-on-exec) flag set.  The files that remain open inherit all
Packit 6c4009
attributes of the open file descriptors from the existing process image,
Packit 6c4009
including file locks.  File descriptors are discussed in @ref{Low-Level I/O}.
Packit 6c4009
Packit 6c4009
Streams, by contrast, cannot survive through @code{exec} functions,
Packit 6c4009
because they are located in the memory of the process itself.  The new
Packit 6c4009
process image has no streams except those it creates afresh.  Each of
Packit 6c4009
the streams in the pre-@code{exec} process image has a descriptor inside
Packit 6c4009
it, and these descriptors do survive through @code{exec} (provided that
Packit 6c4009
they do not have @code{FD_CLOEXEC} set).  The new process image can
Packit 6c4009
reconnect these to new streams using @code{fdopen} (@pxref{Descriptors
Packit 6c4009
and Streams}).
Packit 6c4009
Packit 6c4009
@node Process Completion
Packit 6c4009
@section Process Completion
Packit 6c4009
@cindex process completion
Packit 6c4009
@cindex waiting for completion of child process
Packit 6c4009
@cindex testing exit status of child process
Packit 6c4009
Packit 6c4009
The functions described in this section are used to wait for a child
Packit 6c4009
process to terminate or stop, and determine its status.  These functions
Packit 6c4009
are declared in the header file @file{sys/wait.h}.
Packit 6c4009
@pindex sys/wait.h
Packit 6c4009
Packit 6c4009
@deftypefun pid_t waitpid (pid_t @var{pid}, int *@var{status-ptr}, int @var{options})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{waitpid} function is used to request status information from a
Packit 6c4009
child process whose process ID is @var{pid}.  Normally, the calling
Packit 6c4009
process is suspended until the child process makes status information
Packit 6c4009
available by terminating.
Packit 6c4009
Packit 6c4009
Other values for the @var{pid} argument have special interpretations.  A
Packit 6c4009
value of @code{-1} or @code{WAIT_ANY} requests status information for
Packit 6c4009
any child process; a value of @code{0} or @code{WAIT_MYPGRP} requests
Packit 6c4009
information for any child process in the same process group as the
Packit 6c4009
calling process; and any other negative value @minus{} @var{pgid}
Packit 6c4009
requests information for any child process whose process group ID is
Packit 6c4009
@var{pgid}.
Packit 6c4009
Packit 6c4009
If status information for a child process is available immediately, this
Packit 6c4009
function returns immediately without waiting.  If more than one eligible
Packit 6c4009
child process has status information available, one of them is chosen
Packit 6c4009
randomly, and its status is returned immediately.  To get the status
Packit 6c4009
from the other eligible child processes, you need to call @code{waitpid}
Packit 6c4009
again.
Packit 6c4009
Packit 6c4009
The @var{options} argument is a bit mask.  Its value should be the
Packit 6c4009
bitwise OR (that is, the @samp{|} operator) of zero or more of the
Packit 6c4009
@code{WNOHANG} and @code{WUNTRACED} flags.  You can use the
Packit 6c4009
@code{WNOHANG} flag to indicate that the parent process shouldn't wait;
Packit 6c4009
and the @code{WUNTRACED} flag to request status information from stopped
Packit 6c4009
processes as well as processes that have terminated.
Packit 6c4009
Packit 6c4009
The status information from the child process is stored in the object
Packit 6c4009
that @var{status-ptr} points to, unless @var{status-ptr} is a null pointer.
Packit 6c4009
Packit 6c4009
This function is a cancellation point in multi-threaded programs.  This
Packit 6c4009
is a problem if the thread allocates some resources (like memory, file
Packit 6c4009
descriptors, semaphores or whatever) at the time @code{waitpid} is
Packit 6c4009
called.  If the thread gets canceled these resources stay allocated
Packit 6c4009
until the program ends.  To avoid this calls to @code{waitpid} should be
Packit 6c4009
protected using cancellation handlers.
Packit 6c4009
@c ref pthread_cleanup_push / pthread_cleanup_pop
Packit 6c4009
Packit 6c4009
The return value is normally the process ID of the child process whose
Packit 6c4009
status is reported.  If there are child processes but none of them is
Packit 6c4009
waiting to be noticed, @code{waitpid} will block until one is.  However,
Packit 6c4009
if the @code{WNOHANG} option was specified, @code{waitpid} will return
Packit 6c4009
zero instead of blocking.
Packit 6c4009
Packit 6c4009
If a specific PID to wait for was given to @code{waitpid}, it will
Packit 6c4009
ignore all other children (if any).  Therefore if there are children
Packit 6c4009
waiting to be noticed but the child whose PID was specified is not one
Packit 6c4009
of them, @code{waitpid} will block or return zero as described above.
Packit 6c4009
Packit 6c4009
A value of @code{-1} is returned in case of error.  The following
Packit 6c4009
@code{errno} error conditions are defined for this function:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINTR
Packit 6c4009
The function was interrupted by delivery of a signal to the calling
Packit 6c4009
process.  @xref{Interrupted Primitives}.
Packit 6c4009
Packit 6c4009
@item ECHILD
Packit 6c4009
There are no child processes to wait for, or the specified @var{pid}
Packit 6c4009
is not a child of the calling process.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
An invalid value was provided for the @var{options} argument.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
These symbolic constants are defined as values for the @var{pid} argument
Packit 6c4009
to the @code{waitpid} function.
Packit 6c4009
Packit 6c4009
@comment Extra blank lines make it look better.
Packit 6c4009
@vtable @code
Packit 6c4009
@item WAIT_ANY
Packit 6c4009
Packit 6c4009
This constant macro (whose value is @code{-1}) specifies that
Packit 6c4009
@code{waitpid} should return status information about any child process.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@item WAIT_MYPGRP
Packit 6c4009
This constant (with value @code{0}) specifies that @code{waitpid} should
Packit 6c4009
return status information about any child process in the same process
Packit 6c4009
group as the calling process.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
These symbolic constants are defined as flags for the @var{options}
Packit 6c4009
argument to the @code{waitpid} function.  You can bitwise-OR the flags
Packit 6c4009
together to obtain a value to use as the argument.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item WNOHANG
Packit 6c4009
Packit 6c4009
This flag specifies that @code{waitpid} should return immediately
Packit 6c4009
instead of waiting, if there is no child process ready to be noticed.
Packit 6c4009
Packit 6c4009
@item WUNTRACED
Packit 6c4009
Packit 6c4009
This flag specifies that @code{waitpid} should report the status of any
Packit 6c4009
child processes that have been stopped as well as those that have
Packit 6c4009
terminated.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@deftypefun pid_t wait (int *@var{status-ptr})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is a simplified version of @code{waitpid}, and is used to wait
Packit 6c4009
until any one child process terminates.  The call:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
wait (&status)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
is exactly equivalent to:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
waitpid (-1, &status, 0)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function is a cancellation point in multi-threaded programs.  This
Packit 6c4009
is a problem if the thread allocates some resources (like memory, file
Packit 6c4009
descriptors, semaphores or whatever) at the time @code{wait} is
Packit 6c4009
called.  If the thread gets canceled these resources stay allocated
Packit 6c4009
until the program ends.  To avoid this calls to @code{wait} should be
Packit 6c4009
protected using cancellation handlers.
Packit 6c4009
@c ref pthread_cleanup_push / pthread_cleanup_pop
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun pid_t wait4 (pid_t @var{pid}, int *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
Packit 6c4009
@standards{BSD, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If @var{usage} is a null pointer, @code{wait4} is equivalent to
Packit 6c4009
@code{waitpid (@var{pid}, @var{status-ptr}, @var{options})}.
Packit 6c4009
Packit 6c4009
If @var{usage} is not null, @code{wait4} stores usage figures for the
Packit 6c4009
child process in @code{*@var{rusage}} (but only if the child has
Packit 6c4009
terminated, not if it has stopped).  @xref{Resource Usage}.
Packit 6c4009
Packit 6c4009
This function is a BSD extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here's an example of how to use @code{waitpid} to get the status from
Packit 6c4009
all child processes that have terminated, without ever waiting.  This
Packit 6c4009
function is designed to be a handler for @code{SIGCHLD}, the signal that
Packit 6c4009
indicates that at least one child process has terminated.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@group
Packit 6c4009
void
Packit 6c4009
sigchld_handler (int signum)
Packit 6c4009
@{
Packit 6c4009
  int pid, status, serrno;
Packit 6c4009
  serrno = errno;
Packit 6c4009
  while (1)
Packit 6c4009
    @{
Packit 6c4009
      pid = waitpid (WAIT_ANY, &status, WNOHANG);
Packit 6c4009
      if (pid < 0)
Packit 6c4009
        @{
Packit 6c4009
          perror ("waitpid");
Packit 6c4009
          break;
Packit 6c4009
        @}
Packit 6c4009
      if (pid == 0)
Packit 6c4009
        break;
Packit 6c4009
      notice_termination (pid, status);
Packit 6c4009
    @}
Packit 6c4009
  errno = serrno;
Packit 6c4009
@}
Packit 6c4009
@end group
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Process Completion Status
Packit 6c4009
@section Process Completion Status
Packit 6c4009
Packit 6c4009
If the exit status value (@pxref{Program Termination}) of the child
Packit 6c4009
process is zero, then the status value reported by @code{waitpid} or
Packit 6c4009
@code{wait} is also zero.  You can test for other kinds of information
Packit 6c4009
encoded in the returned status value using the following macros.
Packit 6c4009
These macros are defined in the header file @file{sys/wait.h}.
Packit 6c4009
@pindex sys/wait.h
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WIFEXITED (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This macro returns a nonzero value if the child process terminated
Packit 6c4009
normally with @code{exit} or @code{_exit}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WEXITSTATUS (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If @code{WIFEXITED} is true of @var{status}, this macro returns the
Packit 6c4009
low-order 8 bits of the exit status value from the child process.
Packit 6c4009
@xref{Exit Status}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WIFSIGNALED (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This macro returns a nonzero value if the child process terminated
Packit 6c4009
because it received a signal that was not handled.
Packit 6c4009
@xref{Signal Handling}.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WTERMSIG (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If @code{WIFSIGNALED} is true of @var{status}, this macro returns the
Packit 6c4009
signal number of the signal that terminated the child process.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WCOREDUMP (int @var{status})
Packit 6c4009
@standards{BSD, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This macro returns a nonzero value if the child process terminated
Packit 6c4009
and produced a core dump.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WIFSTOPPED (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This macro returns a nonzero value if the child process is stopped.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn Macro int WSTOPSIG (int @var{status})
Packit 6c4009
@standards{POSIX.1, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If @code{WIFSTOPPED} is true of @var{status}, this macro returns the
Packit 6c4009
signal number of the signal that caused the child process to stop.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node BSD Wait Functions
Packit 6c4009
@section BSD Process Wait Function
Packit 6c4009
Packit 6c4009
@Theglibc{} also provides the @code{wait3} function for compatibility
Packit 6c4009
with BSD.  This function is declared in @file{sys/wait.h}.  It is the
Packit 6c4009
predecessor to @code{wait4}, which is more flexible.  @code{wait3} is
Packit 6c4009
now obsolete.
Packit 6c4009
@pindex sys/wait.h
Packit 6c4009
Packit 6c4009
@deftypefun pid_t wait3 (int *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})
Packit 6c4009
@standards{BSD, sys/wait.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If @var{usage} is a null pointer, @code{wait3} is equivalent to
Packit 6c4009
@code{waitpid (-1, @var{status-ptr}, @var{options})}.
Packit 6c4009
Packit 6c4009
If @var{usage} is not null, @code{wait3} stores usage figures for the
Packit 6c4009
child process in @code{*@var{rusage}} (but only if the child has
Packit 6c4009
terminated, not if it has stopped).  @xref{Resource Usage}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Process Creation Example
Packit 6c4009
@section Process Creation Example
Packit 6c4009
Packit 6c4009
Here is an example program showing how you might write a function
Packit 6c4009
similar to the built-in @code{system}.  It executes its @var{command}
Packit 6c4009
argument using the equivalent of @samp{sh -c @var{command}}.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
/* @r{Execute the command using this shell program.}  */
Packit 6c4009
#define SHELL "/bin/sh"
Packit 6c4009
Packit 6c4009
@group
Packit 6c4009
int
Packit 6c4009
my_system (const char *command)
Packit 6c4009
@{
Packit 6c4009
  int status;
Packit 6c4009
  pid_t pid;
Packit 6c4009
@end group
Packit 6c4009
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    @{
Packit 6c4009
      /* @r{This is the child process.  Execute the shell command.} */
Packit 6c4009
      execl (SHELL, SHELL, "-c", command, NULL);
Packit 6c4009
      _exit (EXIT_FAILURE);
Packit 6c4009
    @}
Packit 6c4009
  else if (pid < 0)
Packit 6c4009
    /* @r{The fork failed.  Report failure.}  */
Packit 6c4009
    status = -1;
Packit 6c4009
  else
Packit 6c4009
    /* @r{This is the parent process.  Wait for the child to complete.}  */
Packit 6c4009
    if (waitpid (pid, &status, 0) != pid)
Packit 6c4009
      status = -1;
Packit 6c4009
  return status;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@comment Yes, this example has been tested.
Packit 6c4009
Packit 6c4009
There are a couple of things you should pay attention to in this
Packit 6c4009
example.
Packit 6c4009
Packit 6c4009
Remember that the first @code{argv} argument supplied to the program
Packit 6c4009
represents the name of the program being executed.  That is why, in the
Packit 6c4009
call to @code{execl}, @code{SHELL} is supplied once to name the program
Packit 6c4009
to execute and a second time to supply a value for @code{argv[0]}.
Packit 6c4009
Packit 6c4009
The @code{execl} call in the child process doesn't return if it is
Packit 6c4009
successful.  If it fails, you must do something to make the child
Packit 6c4009
process terminate.  Just returning a bad status code with @code{return}
Packit 6c4009
would leave two processes running the original program.  Instead, the
Packit 6c4009
right behavior is for the child process to report failure to its parent
Packit 6c4009
process.
Packit 6c4009
Packit 6c4009
Call @code{_exit} to accomplish this.  The reason for using @code{_exit}
Packit 6c4009
instead of @code{exit} is to avoid flushing fully buffered streams such
Packit 6c4009
as @code{stdout}.  The buffers of these streams probably contain data
Packit 6c4009
that was copied from the parent process by the @code{fork}, data that
Packit 6c4009
will be output eventually by the parent process.  Calling @code{exit} in
Packit 6c4009
the child would output the data twice.  @xref{Termination Internals}.