1 .\" Copyright (c) 1993 by Thomas Koenig <ig25@rz.uni-karlsruhe.de>
2 .\" and Copyright (c) 2004 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Sat Jul 24 13:30:06 1993 by Rik Faith <faith@cs.unc.edu>
7 .\" Modified Sun Aug 21 17:42:42 1994 by Rik Faith <faith@cs.unc.edu>
8 .\" (Thanks to Koen Holtman <koen@win.tue.nl>)
9 .\" Modified Wed May 17 15:54:12 1995 by Rik Faith <faith@cs.unc.edu>
10 .\" To remove *'s from status in macros (Thanks to Michael Shields).
11 .\" Modified as suggested by Nick Duffek <nsd@bbc.com>, aeb, 960426
12 .\" Modified Mon Jun 23 14:09:52 1997 by aeb - add EINTR.
13 .\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
14 .\" Modified Mon Jul 24 21:37:38 2000 by David A. Wheeler
15 .\" <dwheeler@dwheeler.com> - noted thread issues.
16 .\" Modified 26 Jun 01 by Michael Kerrisk
17 .\" Added __WCLONE, __WALL, and __WNOTHREAD descriptions
18 .\" Modified 2001-09-25, aeb
19 .\" Modified 26 Jun 01 by Michael Kerrisk, <mtk.manpages@gmail.com>
20 .\" Updated notes on setting disposition of SIGCHLD to SIG_IGN
22 .\" Added waitid(2); added WCONTINUED and WIFCONTINUED()
23 .\" Added text on SA_NOCLDSTOP
24 .\" Updated discussion of SA_NOCLDWAIT to reflect 2.6 behavior
25 .\" Much other text rewritten
26 .\" 2005-05-10, mtk, __W* flags can't be used with waitid()
27 .\" 2008-07-04, mtk, removed erroneous text about SA_NOCLDSTOP
29 .TH WAIT 2 2022-09-17 "Linux man-pages (unreleased)"
31 wait, waitpid, waitid \- wait for process to change state
34 .RI ( libc ", " \-lc )
37 .B #include <sys/wait.h>
39 .BI "pid_t wait(int *" "wstatus" );
40 .BI "pid_t waitpid(pid_t " pid ", int *" wstatus ", int " options );
42 .BI "int waitid(idtype_t " idtype ", id_t " id \
43 ", siginfo_t *" infop ", int " options );
44 /* This is the glibc and POSIX interface; see
45 NOTES for information on the raw system call. */
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
56 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
57 .\" (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
58 Glibc 2.25 and earlier:
60 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
61 || /* Glibc <= 2.19: */ _BSD_SOURCE
64 All of these system calls are used to wait for state changes
65 in a child of the calling process, and obtain information
66 about the child whose state has changed.
67 A state change is considered to be: the child terminated;
68 the child was stopped by a signal; or the child was resumed by a signal.
69 In the case of a terminated child, performing a wait allows
70 the system to release the resources associated with the child;
71 if a wait is not performed, then the terminated child remains in
72 a "zombie" state (see NOTES below).
74 If a child has already changed state, then these calls return immediately.
75 Otherwise, they block until either a child changes state or
76 a signal handler interrupts the call (assuming that system calls
77 are not automatically restarted using the
81 In the remainder of this page, a child whose state has changed
82 and which has not yet been waited upon by one of these system
85 .SS wait() and waitpid()
88 system call suspends execution of the calling thread until one of its
96 waitpid(\-1, &wstatus, 0);
102 system call suspends execution of the calling thread until a
105 argument has changed state.
108 waits only for terminated children, but this behavior is modifiable
111 argument, as described below.
117 meaning wait for any child process whose process group ID is
118 equal to the absolute value of
121 meaning wait for any child process.
123 meaning wait for any child process whose process group ID is
124 equal to that of the calling process at the time of the call to
127 meaning wait for the child whose process ID is equal to the
133 is an OR of zero or more of the following constants:
136 return immediately if no child has exited.
139 also return if a child has stopped
144 children which have stopped is provided
145 even if this option is not specified.
147 .BR WCONTINUED " (since Linux 2.6.10)"
148 also return if a stopped child has been resumed by delivery of
151 (For Linux-only options, see below.)
159 store status information in the \fIint\fP to which it points.
160 This integer can be inspected with the following macros (which
161 take the integer itself as an argument, not a pointer to it,
167 .BI WIFEXITED( wstatus )
168 returns true if the child terminated normally, that is,
173 or by returning from main().
175 .BI WEXITSTATUS( wstatus )
176 returns the exit status of the child.
177 This consists of the least significant 8 bits of the
179 argument that the child specified in a call to
183 or as the argument for a return statement in main().
184 This macro should be employed only if
188 .BI WIFSIGNALED( wstatus )
189 returns true if the child process was terminated by a signal.
191 .BI WTERMSIG( wstatus )
192 returns the number of the signal that caused the child process to
194 This macro should be employed only if
198 .BI WCOREDUMP( wstatus )
199 returns true if the child produced a core dump (see
201 This macro should be employed only if
205 This macro is not specified in POSIX.1-2001 and is not available on
206 some UNIX implementations (e.g., AIX, SunOS).
207 Therefore, enclose its use inside
208 .IR "#ifdef WCOREDUMP ... #endif" .
210 .BI WIFSTOPPED( wstatus )
211 returns true if the child process was stopped by delivery of a signal;
212 this is possible only if the call was done using
214 or when the child is being traced (see
217 .BI WSTOPSIG( wstatus )
218 returns the number of the signal which caused the child to stop.
219 This macro should be employed only if
223 .BI WIFCONTINUED( wstatus )
225 returns true if the child process was resumed by delivery of
230 system call (available since Linux 2.6.9) provides more precise
231 control over which child state changes to wait for.
237 arguments select the child(ren) to wait for, as follows:
238 .IP "\fIidtype\fP == \fBP_PID\fP"
239 Wait for the child whose process ID matches
241 .IP "\fIidtype\fP == \fBP_PIDFD\fP (since Linux 5.4)"
242 .\" commit 3695eae5fee0605f316fbaad0b9e3de791d7dfaf
243 Wait for the child referred to by the PID file descriptor specified in
247 for further information on PID file descriptors.)
248 .IP "\fIidtype\fP == \fBP_PGID\fP"
249 Wait for any child whose process group ID matches
252 .\" commit 821cc7b0b205c0df64cce59aacc330af251fa8f7
255 is zero, then wait for any child that is in the same process group
256 as the caller's process group at the time of the call.
257 .IP "\fIidtype\fP == \fBP_ALL\fP"
262 The child state changes to wait for are specified by ORing
263 one or more of the following flags in
267 Wait for children that have terminated.
270 Wait for children that have been stopped by delivery of a signal.
273 Wait for (previously stopped) children that have been
274 resumed by delivery of
277 The following flags may additionally be ORed in
285 Leave the child in a waitable state; a later wait call
286 can be used to again retrieve the child status information.
288 Upon successful return,
290 fills in the following fields of the
292 structure pointed to by
296 The process ID of the child.
299 The real user ID of the child.
300 (This field is not set on most other implementations.)
307 Either the exit status of the child, as given to
311 or the signal that caused the child to terminate, stop, or continue.
314 field can be used to determine how to interpret this field.
322 (child killed by signal);
324 (child killed by signal, and dumped core);
326 (child stopped by signal);
328 (traced child has trapped); or
337 and there were no children in a waitable state, then
339 returns 0 immediately and
342 structure pointed to by
344 depends on the implementation.
345 To (portably) distinguish this case from that where a child was in a
346 waitable state, zero out the
348 field before the call and check for a nonzero value in this field
349 after the call returns.
351 POSIX.1-2008 Technical Corrigendum 1 (2013) adds the requirement that when
355 and there were no children in a waitable state, then
361 fields of the structure.
362 On Linux and other implementations that adhere to this requirement,
363 it is not necessary to zero out the
368 not all implementations follow the POSIX.1 specification on this point.
369 .\" POSIX.1-2001 leaves this possibility unspecified; most
370 .\" implementations (including Linux) zero out the structure
371 .\" in this case, but at least one implementation (AIX 5.1)
372 .\" does not -- MTK Nov 04
375 on success, returns the process ID of the terminated child;
376 on failure, \-1 is returned.
379 on success, returns the process ID of the child whose state has changed;
382 was specified and one or more child(ren) specified by
384 exist, but have not yet changed state, then 0 is returned.
385 On failure, \-1 is returned.
388 returns 0 on success or
391 was specified and no child(ren) specified by
393 has yet changed state;
394 on failure, \-1 is returned.
395 .\" FIXME As reported by Vegard Nossum, if infop is NULL, then waitid()
396 .\" returns the PID of the child. Either this is a bug, or it is intended
397 .\" behavior that needs to be documented. See my Jan 2009 LKML mail
398 .\" "waitid() return value strangeness when infop is NULL".
400 On failure, each of these calls sets
402 to indicate the error.
406 The PID file descriptor specified in
408 is nonblocking and the process that it refers to has not terminated.
413 The calling process does not have any unwaited-for children.
420 The process specified by
428 does not exist or is not a child of the calling process.
429 (This can happen for one's own child if the action for
433 See also the \fILinux Notes\fP section about threads.)
437 was not set and an unblocked signal or a
445 argument was invalid.
456 SVr4, 4.3BSD, POSIX.1-2001.
458 A child that terminates, but has not been waited for becomes a "zombie".
459 The kernel maintains a minimal set of information about the zombie
460 process (PID, termination status, resource usage information)
461 in order to allow the parent to later perform a wait to obtain
462 information about the child.
463 As long as a zombie is not removed from the system via a wait,
464 it will consume a slot in the kernel process table, and if
465 this table fills, it will not be possible to create further processes.
466 If a parent process terminates, then its "zombie" children (if any)
469 (or by the nearest "subreaper" process as defined through the use of the
471 .B PR_SET_CHILD_SUBREAPER
474 automatically performs a wait to remove the zombies.
476 POSIX.1-2001 specifies that if the disposition of
486 then children that terminate do not become zombies and a call to
490 will block until all children have terminated, and then fail with
494 (The original POSIX standard left the behavior of setting
499 Note that even though the default disposition of
501 is "ignore", explicitly setting the disposition to
503 results in different treatment of zombie process children.)
505 Linux 2.6 conforms to the POSIX requirements.
506 However, Linux 2.4 (and earlier) does not:
513 is being ignored, the call behaves just as though
515 were not being ignored, that is, the call blocks until the next child
516 terminates and then returns the process ID and status of that child.
518 In the Linux kernel, a kernel-scheduled thread is not a distinct
519 construct from a process.
520 Instead, a thread is simply a process
521 that is created using the Linux-unique
523 system call; other routines such as the portable
524 .BR pthread_create (3)
525 call are implemented using
527 Before Linux 2.4, a thread was just a special case of a process,
528 and as a consequence one thread could not wait on the children
529 of another thread, even when the latter belongs to the same thread group.
530 However, POSIX prescribes such functionality, and since Linux 2.4
531 a thread can, and by default will, wait on children of other threads
532 in the same thread group.
534 The following Linux-specific
536 are for use with children created using
538 they can also, since Linux 4.7,
539 .\" commit 91c4e8ea8f05916df0c8a6f383508ac7c9e10dba
545 Wait for "clone" children only.
546 If omitted, then wait for "non-clone" children only.
547 (A "clone" child is one which delivers no signal, or a signal other than
549 to its parent upon termination.)
550 This option is ignored if
554 .BR __WALL " (since Linux 2.4)"
555 .\" since patch-2.3.48
556 Wait for all children, regardless of
557 type ("clone" or "non-clone").
559 .BR __WNOTHREAD " (since Linux 2.4)"
560 .\" since patch-2.4.0-test8
561 Do not wait for children of other threads in
562 the same thread group.
563 This was the default before Linux 2.4.
566 .\" commit bf959931ddb88c4e4366e96dd22e68fa0db9527c
567 .\" prevents cases where an unreapable zombie is created if
568 .\" /sbin/init doesn't use __WALL.
571 flag is automatically implied if the child is being ptraced.
572 .SS C library/kernel differences
574 is actually a library function that (in glibc) is implemented as a call to
577 On some architectures, there is no
580 .\" e.g., i386 has the system call, but not x86-64
581 instead, this interface is implemented via a C library
582 wrapper function that calls
587 system call takes a fifth argument, of type
588 .IR "struct rusage\ *" .
589 If this argument is non-NULL,
590 then it is used to return resource usage information about the child,
591 in the same manner as
597 According to POSIX.1-2008, an application calling
603 structure (i.e., that it is a non-null pointer).
608 succeeds, and returns the process ID of the waited-for child.
609 Applications should avoid relying on this inconsistent,
610 nonstandard, and unnecessary feature.
612 .\" fork.2 refers to this example program.
613 The following program demonstrates the use of
617 The program creates a child process.
618 If no command-line argument is supplied to the program,
619 then the child suspends its execution using
621 to allow the user to send signals to the child.
622 Otherwise, if a command-line argument is supplied,
623 then the child exits immediately,
624 using the integer supplied on the command line as the exit status.
625 The parent process executes a loop that monitors the child using
627 and uses the W*() macros described above to analyze the wait status value.
629 The following shell session demonstrates the use of the program:
636 .RB "$" " kill \-STOP 32360"
638 .RB "$" " kill \-CONT 32360"
640 .RB "$" " kill \-TERM 32360"
648 .\" SRC BEGIN (wait.c)
653 #include <sys/wait.h>
657 main(int argc, char *argv[])
668 if (cpid == 0) { /* Code executed by child */
669 printf("Child PID is %jd\en", (intmax_t) getpid());
671 pause(); /* Wait for signals */
672 _exit(atoi(argv[1]));
674 } else { /* Code executed by parent */
676 w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
682 if (WIFEXITED(wstatus)) {
683 printf("exited, status=%d\en", WEXITSTATUS(wstatus));
684 } else if (WIFSIGNALED(wstatus)) {
685 printf("killed by signal %d\en", WTERMSIG(wstatus));
686 } else if (WIFSTOPPED(wstatus)) {
687 printf("stopped by signal %d\en", WSTOPSIG(wstatus));
688 } else if (WIFCONTINUED(wstatus)) {
689 printf("continued\en");
691 } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
706 .BR pthread_create (3),