C Reference ยท POSIX and Systems
C wait() and waitpid() functions
Reference for wait() and waitpid(): wait for child processes, decode the status with WIFEXITED and WEXITSTATUS, avoid zombies, and use WNOHANG for polling.
wait() and waitpid() let a parent process wait for a child to finish and collect how it ended. Until the parent does this, a finished child remains in the process table as a zombie: it uses no CPU or memory, but its exit status and process ID are still held for the parent to read.
wait(&status) waits for any child. waitpid(pid, &status, options) is more flexible: you choose which child, and with WNOHANG the call returns immediately instead of blocking. The status is an opaque integer that you decode with macros such as WIFEXITED and WEXITSTATUS, never by hand.
#include <sys/wait.h> int status; pid_t done = waitpid(child, &status, 0); if (done > 0 && WIFEXITED(status)) { int code = WEXITSTATUS(status); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);
| Name | Description |
|---|---|
status |
Where to store the status, or NULL if you do not care. |
pid |
> 0: that child. -1: any child. 0: any child in the caller's process group. < -1: any child in the group -pid. |
options |
0 to block, or flags: WNOHANG (do not block), WUNTRACED (also report stopped children), WCONTINUED (also report resumed children). |
Returns: the process ID of the child whose state changed. 0 if WNOHANG was given and no child has changed yet. -1 on error.
Errors: ECHILD (no such child, or no children at all), EINTR (interrupted by a signal), EINVAL (bad options).
Decoding the status
| Macro | Meaning |
|---|---|
WIFEXITED(s) |
The child called exit or returned from main |
WEXITSTATUS(s) |
The low 8 bits of the exit code (only if WIFEXITED) |
WIFSIGNALED(s) |
The child was terminated by a signal |
WTERMSIG(s) |
Which signal (only if WIFSIGNALED) |
WIFSTOPPED(s) |
The child is stopped (only with WUNTRACED) |
Usage notes
wait(&s)is the same aswaitpid(-1, &s, 0).- Exit codes are 8 bits:
exit(256)looks like0to the parent, andexit(-1)looks like255. - A shell reports death by signal as
128 + signal; that is a shell convention. The raw status distinguishes the two cases throughWIFEXITEDandWIFSIGNALED. - To reap every child, loop until
waitreturns-1witherrno == ECHILD. In aSIGCHLDhandler usewhile (waitpid(-1, NULL, WNOHANG) > 0) {}because several children may end while one signal is delivered. - If a signal interrupts a blocking wait, it fails with
EINTR; retry in a loop when appropriate. - A parent that exits first leaves its children to be adopted by another process (often
init), which reaps them. waitidoffers finer control (it can peek without reaping). POSIX threads share one process, so any thread of the parent can wait for the children.
Related
- fork, exec family, pipe
- signal.h, sigaction
- Tutorial: Signals, System calls