4.3BSD-Reno/src/lib/libc/sys/wait.2

Compare this file to the similar file:
Show the results in this format:

.\" Copyright (c) 1980 Regents of the University of California.
.\" All rights reserved.  The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\"
.\"	@(#)wait.2	6.3 (Berkeley) 8/14/89
.\"
.TH WAIT 2 "August 14, 1989"
.UC 4
.SH NAME
wait, waitpid, wait4, wait3 \- wait for process to terminate
.SH SYNOPSIS
.ft B
.nf
#include <sys/types.h>
#include <sys/wait.h>
.PP
.ft B
pid = wait(status)
pid_t pid;
int *status;
.PP
.ft B
pid = waitpid(wpid, status, options)
pid_t pid, wpid;
int *status;
int options;
.PP
.ft B
#include <sys/time.h>
#include <sys/resource.h>
.PP
.ft B
pid = wait4(wpid, status, options, rusage)
pid_t pid, wpid;
int *status;
int options;
struct rusage *rusage;
.PP
.ft B
pid = wait3(status, options, rusage)
pid_t pid;
int *status;
int options;
struct rusage *rusage;
.fi
.SH DESCRIPTION
.I Wait
causes its caller to delay until a signal is received or
one of its child
processes terminates.
If any child has died since the last
.IR wait ,
return is immediate, returning the process id and
exit status of one of the terminated
children.
If there are no children, return is immediate with
the value \-1 returned.
.PP
On return from a successful 
.I wait
call, 
the
.I status
area contains termination information about the process that exited
as defined below.
.PP
The
.I wait4
call provides a more general interface for programs
that wish to wait for certain child processes,
that wish resource utilization statistics accummulated by child processes,
or that require options.
The other wait functions are implemented using
.IR wait4 .
.PP
The
.I wpid
parameter specifies the set of child processes for which to wait.
If
.I wpid
is \-1, the call waits for any child process.
If
.I wpid
is 0,
the call waits for any child process in the process group of the caller.
If
.I wpid
is greater than zero, the call waits for the process with process id
.IR wpid .
If
.I wpid
is less than \-1, the call waits for any process whose process group id
equals the absolute value of
.IR wpid .
.PP
The
.I status
parameter is defined below.  The
.I options
parameter contains the bitwise OR of any of the following options.
The WNOHANG option
is used to indicate that the call should not block if
there are no processes that wish to report status.
If the WUNTRACED option is set,
children of the current process that are stopped
due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal also have
their status reported.
.PP
If
.I rusage
is non-zero, a summary of the resources used by the terminated
process and all its
children is returned (this information is currently not available
for stopped processes).
.PP
When the WNOHANG option is specified and no processes
wish to report status, 
.I wait4
returns a 
.I pid
of 0.
.PP
The
.I waitpid
call is identical to
.I wait4
with an
.I rusage
value of zero.
The older
.I wait3
call is the same as
.I wait4
with a
.I wpid
value of \-1.
.PP
The following macros may be used to test the manner of exit of the process.
One of the first three macros will evaluate to a non-zero (true) value:
.IP WIFEXITED(status)
True if the process terminated normally by a call to
.IR _exit (2)
or
.IR exit (2).
.IP WIFSIGNALED(status)
True if the process terminated due to receipt of a signal.
.IP WIFSTOPPED(status)
True if the process has not terminated, but has stopped and can be restarted.
This macro can be true only if the wait call specified the
WUNTRACED
option
or if the child process is being traced (see
.IR ptrace (2)).
.LP
Depending on the values of those macros, the following macros
produce the remaining status information about the child process:
.IP WEXITSTATUS(status)
If WIFEXITED(status) is true, evaluates to the low-order 8 bits
of the argument passed to
.IR _exit (2)
or
.IR exit (2)
by the child.
.IP WTERMSIG(status)
If WIFSIGNALED(status) is true, evaluates to the number of the signal
that caused the termination of the process.
.IP WCOREDUMP(status)
If WIFSIGNALED(status) is true, evaluates as true if the termination
of the process was accompanied by the creation of a core file
containing an image of the process when the signal was received.
.IP WSTOPSIG(status)
If WIFSTOPPED(status) is true, evaluates to the number of the signal
that caused the process to stop.
.SH NOTES
See
.IR sigaction (2)
for a list of termination signals.
A status of 0 indicates normal termination.
.PP
If the parent process terminates without
waiting on its children,
the initialization process
(process ID = 1)
inherits the children.
.PP
If a signal is caught while any of the
.I wait
calls is pending,
the call may be interrupted or restarted when the signal-catching routine
returns,
depending on the options in effect for the signal;
see
.IR intro (2),
System call restart.
.SH "RETURN VALUE
If \fIwait\fP returns due to a stopped
or terminated child process, the process ID of the child
is returned to the calling process.  Otherwise, a value of \-1
is returned and \fIerrno\fP is set to indicate the error.
.PP
If
.IR wait4 ,
.I wait3
or
.I waitpid
returns due to a stopped
or terminated child process, the process ID of the child
is returned to the calling process.
If there are no children not previously awaited,
\-1 is returned with
.I errno
set to [ECHILD].
Otherwise, if WNOHANG is specified and there are
no stopped or exited children,
0 is returned.
If an error is detected or a caught signal aborts the call,
a value of \-1
is returned and \fIerrno\fP is set to indicate the error.
.SH ERRORS
.I Wait
will fail and return immediately if one or more of the following
are true:
.TP 15
[ECHILD]
The calling process has no existing unwaited-for
child processes.
.TP 15
[EFAULT]
The \fIstatus\fP or \fIrusage\fP arguments point to an illegal address.
(May not be detected before exit of a child process.)
.TP 15
[EINTR]
The call was interrupted by a caught signal,
or the signal did not have the SA_RESTART flag set.
.SH STANDARDS
The
.I wait
and
.I waitpid
functions are defined by POSIX;
.I wait4
and
.I wait3
are not specified by POSIX.
The WCOREDUMP macro
and the ability to restart a pending
.I wait
call are extensions to the POSIX interface.
.SH "SEE ALSO"
exit(2), sigaction(2)