WAIT(2) — SYSTEM CALLS
NAME
wait, wait3 − wait for process to terminate or stop
SYNOPSIS
#include <sys/wait.h>
pid = wait(status)
int pid;
union wait ∗status;
pid = wait(0)
int pid;
#include <sys/time.h>
#include <sys/resource.h>
pid = wait3(status, options, rusage)
int pid;
union wait ∗status;
int options;
struct rusage ∗rusage;
DESCRIPTION
wait causes its caller to delay until a signal is received or one of its child processes terminates or stops due to tracing. If any child has died or stopped due to tracing and this has not been reported via wait, return is immediate, returning the process ID and exit status of one of those children. If that child had died, it is discarded. If there are no children, return is immediate with the value −1 returned. If there are only running or stopped but reported children, the calling processes is blocked.
On return from a successful wait call, status is nonzero, and the high byte of status contains the low byte of the argument to exit supplied by the child process; the low byte of status contains the termination status of the process. A more precise definition of the status word is given in <sys/wait.h>.
wait3 is an alternate interface that allows both non-blocking status collection and the collection of the status of children stopped by any means. The status parameter is defined as above. The options parameter is used to indicate the call should not block if there are no processes that have status to report (WNOHANG), and/or that children of the current process that are stopped due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal are eligible to have their status reported as well (WUNTRACED). A terminated child is discarded after it reports status, and a stopped process will not report its status more than once. If 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.)
When the WNOHANG option is specified and no processes have status to report, wait3 returns a pid of 0. The WNOHANG and WUNTRACED options may be combined by or’ing the two values.
NOTES
See sigvec(2) for a list of termination statuses (signals); 0 status indicates normal termination. A special status (0177) is returned for a stopped process that has not terminated and can be restarted; see ptrace(2) and sigvec(2). If the 0200 bit of the termination status is set, a core image of the process was produced by the system.
If the parent process terminates without waiting on its children, the initialization process (process ID = 1) inherits the children.
wait and wait3 are automatically restarted when a process receives a signal while awaiting termination of a child process.
RETURN VALUE
If wait 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 errno is set to indicate the error.
wait3 returns −1 if there are no children not previously waited for; 0 is returned if WNOHANG is specified and there are no stopped or exited children.
ERRORS
wait will fail and return immediately if one or more of the following are true:
ECHILD The calling process has no existing unwaited-for child processes.
EFAULT The status or rusage arguments point to an illegal address.
The call is forced to terminate prematurely due to the arrival of a signal whose.SM SV_INTERRUPT
bit in sv_flags is set (see sigvec(2)). signal(3V), in the System V compatibility library, sets this bit for any signal it catches.
SEE ALSO
Sun Release 3.2 — Last change: 16 July 1986