WAIT(2)
π WAIT(2) π
okay bestie, welcome to the WAIT(2) page from the System Calls Manual. This is literally all about making your program wait for processes to change status, and it's giving main character energy for process management. π
β¨ NAME β¨
wait,waitid,waitpid,wait3,wait4,wait6β wait for processes to change status
π LIBRARY π
- Standard C Library (
libc,-lc)
πͺ SYNOPSIS πͺ
#include <sys/types.h>
#include <sys/wait.h>
pid_t
wait(int *status);
pid_t
waitpid(pid_t wpid, int *status, int options);
#include <signal.h>
int
waitid(idtype_t idtype, id_t id, siginfo_t *info, int options);
#include <sys/time.h>
#include <sys/resource.h>
pid_t
wait3(int *status, int options, struct rusage *rusage);
pid_t
wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
pid_t
wait6(idtype_t idtype, id_t id, int *status, int options,
struct __wrusage *wrusage, siginfo_t *infop);
πΈ DESCRIPTION πΈ
okay so wait() literally suspends execution of its calling thread until status information is available for a child process or a signal is received. On return from a successful wait() call, the status area contains information about the process that reported a status change, as defined below. π«Ά
The wait4() and wait6() system calls provide a more general interface for programs that need to wait for specific child processes, that need resource utilization statistics accumulated by child processes, or that require options. The other wait functions are implemented using either wait4() or wait6(). slay. β¨
The wait6() function is the most general function in this family, and its distinct features are:
- All of the desired process statuses to be waited on must be explicitly specified in
options. Thewait(),waitpid(),wait3(), andwait4()functions all implicitly wait for exited and trapped processes, but thewaitid()andwait6()functions require the correspondingWEXITEDandWTRAPPEDflags to be explicitly specified. This allows waiting for processes which have experienced other status changes without having to also handle the exit status from terminated processes. π - The
wait6()function accepts awrusageargument which points to a structure defined as:
struct __wrusage {
struct rusage wru_self;
struct rusage wru_children;
};
This allows the calling process to collect resource usage statistics from both its own child process as well as from its grand children. When no resource usage statistics are needed this pointer can be NULL.
- The last argument
infopmust be eitherNULLor a pointer to asiginfo_tstructure. If non-NULL, the structure is filled with the same data as for aSIGCHLDsignal delivered when the process changed state.
The set of child processes to be queried is specified by the arguments idtype and id. The separate idtype and id arguments support many other types of identifiers in addition to process IDs and process group IDs.
- If
idtypeisP_PID,waitid()andwait6()wait for the child process with a process ID equal to(pid_t)id. - If
idtypeisP_PGID,waitid()andwait6()wait for the child process with a process group ID equal to(pid_t)id. - If
idtypeisP_ALL,waitid()andwait6()wait for any child process and theidis ignored. - If
idtypeisP_PIDorP_PGIDand theidis zero,waitid()andwait6()wait for any child process in the same process group as the caller.
Non-standard identifier types supported by this implementation of waitid() and wait6() are:
P_UIDβ Wait for processes whose effective user ID is equal to(uid_t) id.P_GIDβ Wait for processes whose effective group ID is equal to(gid_t) id.P_SIDβ Wait for processes whose session ID is equal toid. If the child process started its own session, its session ID will be the same as its process ID. Otherwise the session ID of a child process will match the caller's session ID.P_JAILIDβ Waits for processes within a jail whose jail identifier is equal toid.
For the waitpid() and wait4() functions, the single wpid argument specifies the set of child processes for which to wait.
- If
wpidis-1, the call waits for any child process. - If
wpidis0, the call waits for any child process in the process group of the caller. - If
wpidis greater than zero, the call waits for the process with process IDwpid. - If
wpidis less than-1, the call waits for any process whose process group ID equals the absolute value ofwpid.
The status argument is defined below.
The options argument contains the bitwise OR of any of the following options.
WCONTINUEDβ Report the status of selected processes that have continued from a job control stop by receiving aSIGCONTsignal.WNOHANGβ Do not block when there are no processes wishing to report status.WUNTRACEDβ Report the status of selected processes which are stopped due to aSIGTTIN,SIGTTOU,SIGTSTP, orSIGSTOPsignal.WSTOPPEDβ An alias forWUNTRACED.WTRAPPEDβ Report the status of selected processes which are being traced viaptrace(2)and have trapped or reached a breakpoint. This flag is implicitly set for the functionswait(),waitpid(),wait3(), andwait4(). For thewaitid()andwait6()functions, the flag has to be explicitly included inoptionsif status reports from trapped processes are expected.WEXITEDβ Report the status of selected processes which have terminated. This flag is implicitly set for the functionswait(),waitpid(),wait3(), andwait4(). For thewaitid()andwait6()functions, the flag has to be explicitly included inoptionsif status reports from terminated processes are expected.WNOWAITβ Keep the process whose status is returned in a waitable state. The process may be waited for again after this call completes.
For thewaitid()andwait6()functions, at least one of the optionsWEXITED,WUNTRACED,WSTOPPED,WTRAPPED, orWCONTINUEDmust be specified. Otherwise there will be no events for the call to report. To avoid hanging indefinitely in such a case these functions return-1witherrnoset toEINVAL. π
If rusage is non-NULL, a summary of the resources used by the terminated process and all its children is returned.
If wrusage is non-NULL, separate summaries are returned for the resources used by the terminated process and the resources used by all its children.
If infop is non-NULL, a siginfo_t structure is returned with the si_signo field set to SIGCHLD and the si_pid field set to the process ID of the process reporting status. For the exited process, the si_status field of the siginfo_t structure contains the full 32 bit exit status passed to _exit(2); the status argument of other calls only returns 8 lowest bits of the exit status.
When the WNOHANG option is specified and no processes wish to report status, waitid() sets the si_signo and si_pid fields in infop to zero. Checking these fields is the only way to know if a status change was reported.
When the WNOHANG option is specified and no processes wish to report status, wait4() and wait6() return a process id of 0.
The wait() call is the same as wait4() with a wpid value of -1, with an options value of zero, and a rusage value of NULL. The waitpid() function is identical to wait4() with an rusage value of NULL. The older wait3() call is the same as wait4() with a wpid value of -1. The wait4() function is identical to wait6() with the flags WEXITED and WTRAPPED set in options and infop set to NULL.
The following macros may be used to test the current status of the process. Exactly one of the following four macros will evaluate to a non-zero (true) value:
WIFCONTINUED(status)β True if the process has not terminated, and has continued after a job control stop. This macro can be true only if the wait call specified theWCONTINUEDoption.WIFEXITED(status)β True if the process terminated normally by a call to_exit(2)orexit(3).WIFSIGNALED(status)β True if the process terminated due to receipt of a signal.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 theWUNTRACEDoption or if the child process is being traced (seeptrace(2)).
Depending on the values of those macros, the following macros produce the remaining status information about the child process:
WEXITSTATUS(status)β IfWIFEXITED(status)is true, evaluates to the low-order 8 bits of the argument passed to_exit(2)orexit(3)by the child.WTERMSIG(status)β IfWIFSIGNALED(status)is true, evaluates to the number of the signal that caused the termination of the process.WCOREDUMP(status)β IfWIFSIGNALED(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.WSTOPSIG(status)β IfWIFSTOPPED(status)is true, evaluates to the number of the signal that caused the process to stop.
π NOTES π
- See
sigaction(2)for a list of termination signals. A status of0indicates normal termination. - If a parent process terminates without waiting for all of its child processes to terminate, the remaining child processes are re-assigned to the reaper of the exiting process as the parent, see
procctl(2)PROC_REAP_ACQUIRE. If no specific reaper was assigned, the process with ID1, the init process, becomes the parent of the orphaned children by default. - If a signal is caught while any of the
wait()calls are pending, the call may be interrupted or restarted when the signal-catching routine returns, depending on the options in effect for the signal; see discussion ofSA_RESTARTinsigaction(2). - The implementation queues one
SIGCHLDsignal for each child process whose status has changed; ifwait()returns because the status of a child process is available, the pendingSIGCHLDsignal associated with the process ID of the child process will be discarded. Any other pendingSIGCHLDsignals remain pending. - If
SIGCHLDis blocked andwait()returns because the status of a child process is available, the pendingSIGCHLDsignal will be cleared unless another status of the child process is available.
π RETURN VALUES π
okay so here's what you actually get back, bestie, and it depends on which function you called. pay attention fr fr.
If wait() comes back because of a child that got stopped, continued, or terminated? You get the process ID of that child handed right back to the calling process. π«Ά That's the good outcome. Otherwise you get -1, and errno gets set to tell you what went wrong. π
If wait6(), wait4(), wait3(), or waitpid() returns because of a stopped, continued, or terminated child process, same deal β the process ID of the child comes back to the calling process. π
But! If there are no children not previously awaited, you get -1 with errno set to ECHILD. Otherwise, if WNOHANG is specified and there are no stopped, continued, or exited children, you get 0. And if an error is detected or a caught signal aborts the call, it's -1 again with errno set to indicate the error.
Now for waitid() β she's a little different, main character energy β¨. If it returns because one or more processes have a state change to report, you get 0. If an error is detected, -1 and errno gets set. If WNOHANG is specified and there are no stopped, continued, or exited children, 0 again. And listen, this part matters: the si_signo and si_pid fields of infop must be checked against zero to determine if a process reported status. π
And the whole wait() family? They will not return a child process created with pdfork(2) unless you specifically direct them to do so by specifying its process ID. iykyk. π¦
π ERRORS π
The wait() function will fail and return immediately if:
[ECHILD]β The calling process has no existing unwaited-for child processes. like... there's just nobody to wait for, babe π[ECHILD]β No status from the terminated child process is available because the calling process has asked the system to discard such status by ignoring the signalSIGCHLDor setting the flagSA_NOCLDWAITfor that signal. you threw it away, that's on you π«[EFAULT]β Thestatusorrusageargument points to an illegal address. (May not be detected before exit of a child process.) sneaky little thing π[EINTR]β The call was interrupted by a caught signal, or the signal did not have theSA_RESTARTflag set.[EINVAL]β An invalid value was specified foroptions, oridtypeandiddo not specify a valid set of processes. invalid, bestie, we don't stan π«
π SEE ALSO π
_exit(2), procctl(2), ptrace(2), sigaction(2), exit(3), siginfo(3)
go read those too, they're iconic π
π STANDARDS π
The wait(), waitpid(), and waitid() functions are defined by POSIX. β¨ wait6(), wait4(), and wait3() are not specified by POSIX β they're doing their own thing, and honestly? respect. π
The WCOREDUMP() macro is an extension to the POSIX interface.
Also, the ability to use the WNOWAIT flag with waitpid() is an extension β POSIX only permits this flag with waitid(). so like, don't @ POSIX about it π
π°οΈ HISTORY π°οΈ
The wait() function appeared in Version 1 AT&T UNIX. she's literally a founding mother of the whole operating system, no cap ππ
okay that's the whole page bestie, you're officially a wait(2) scholar now πβ¨ this was the Debian build, dated August 11, 2019 β shoutout to the Debian crew, absolute legends π«Άπ bye mwah ππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms