]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/wait.2
memccpy.3: Fix CONFORMING TO: s/C99/POSIX.1-2001/
[thirdparty/man-pages.git] / man2 / wait.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
1130df60 3.\" Copyright (c) 1993 by Thomas Koenig <ig25@rz.uni-karlsruhe.de>
c11b1abf 4.\" and Copyright (c) 2004 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
5.\"
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
c13182ef 14.\"
fea681da
MK
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
25.\" License.
26.\"
27.\" Modified Sat Jul 24 13:30:06 1993 by Rik Faith <faith@cs.unc.edu>
28.\" Modified Sun Aug 21 17:42:42 1994 by Rik Faith <faith@cs.unc.edu>
29.\" (Thanks to Koen Holtman <koen@win.tue.nl>)
30.\" Modified Wed May 17 15:54:12 1995 by Rik Faith <faith@cs.unc.edu>
31.\" To remove *'s from status in macros (Thanks to Michael Shields).
32.\" Modified as suggested by Nick Duffek <nsd@bbc.com>, aeb, 960426
33.\" Modified Mon Jun 23 14:09:52 1997 by aeb - add EINTR.
34.\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
35.\" Modified Mon Jul 24 21:37:38 2000 by David A. Wheeler
36.\" <dwheeler@dwheeler.com> - noted thread issues.
37.\" Modified 26 Jun 01 by Michael Kerrisk
38.\" Added __WCLONE, __WALL, and __WNOTHREAD descriptions
39.\" Modified 2001-09-25, aeb
c11b1abf 40.\" Modified 26 Jun 01 by Michael Kerrisk, <mtk.manpages@gmail.com>
fea681da 41.\" Updated notes on setting disposition of SIGCHLD to SIG_IGN
599be3ee
MK
42.\" 2004-11-11, mtk
43.\" Added waitid(2); added WCONTINUED and WIFCONTINUED()
f2351505 44.\" Added text on SA_NOCLDSTOP
d9bfdb9c 45.\" Updated discussion of SA_NOCLDWAIT to reflect 2.6 behavior
f2351505 46.\" Much other text rewritten
948fb4ed 47.\" 2005-05-10, mtk, __W* flags can't be used with waitid()
21399189 48.\" 2008-07-04, mtk, removed erroneous text about SA_NOCLDSTOP
fea681da 49.\"
21399189 50.TH WAIT 2 2008-07-04 "Linux" "Linux Programmer's Manual"
fea681da 51.SH NAME
0bfa087b 52wait, waitpid, waitid \- wait for process to change state
fea681da
MK
53.SH SYNOPSIS
54.B #include <sys/types.h>
55.br
56.B #include <sys/wait.h>
57.sp
58.BI "pid_t wait(int *" "status" );
5895e7eb 59
fea681da 60.BI "pid_t waitpid(pid_t " pid ", int *" status ", int " options );
5895e7eb 61
c10859eb
MK
62.BI "int waitid(idtype_t " idtype ", id_t " id \
63", siginfo_t *" infop ", int " options );
cc4615cc
MK
64.sp
65.in -4n
66Feature Test Macro Requirements for glibc (see
67.BR feature_test_macros (7)):
68.in
69.sp
70.BR waitid ():
71_SVID_SOURCE || _XOPEN_SOURCE
fea681da 72.SH DESCRIPTION
f2351505
MK
73All of these system calls are used to wait for state changes
74in a child of the calling process, and obtain information
75about the child whose state has changed.
76A state change is considered to be: the child terminated;
77the child was stopped by a signal; or the child was resumed by a signal.
78In the case of a terminated child, performing a wait allows
79the system to release the resources associated with the child;
f946c580 80if a wait is not performed, then the terminated child remains in
f2351505
MK
81a "zombie" state (see NOTES below).
82
83If a child has already changed state, then these calls return immediately.
84Otherwise they block until either a child changes state or
85a signal handler interrupts the call (assuming that system calls
86are not automatically restarted using the
87.B SA_RESTART
88flag of
89.BR sigaction (2)).
90In the remainder of this page, a child whose state has changed
c13182ef 91and which has not yet been waited upon by one of these system
d3b2ef5d 92calls is termed
f2351505
MK
93.IR waitable .
94.SS "wait() and waitpid()"
fea681da 95The
f2351505 96.BR wait ()
a1ffe9f5 97system call suspends execution of the calling process until one of its
f2351505
MK
98children terminates.
99The call
100.I wait(&status)
101is equivalent to:
102.nf
103
2bc2f479 104 waitpid(\-1, &status, 0);
f2351505 105.fi
fea681da
MK
106
107The
f2351505 108.BR waitpid ()
a1ffe9f5 109system call suspends execution of the calling process until a
f2351505 110child specified by
fea681da 111.I pid
f2351505
MK
112argument has changed state.
113By default,
114.BR waitpid ()
d9bfdb9c 115waits only for terminated children, but this behavior is modifiable
f2351505
MK
116via the
117.I options
118argument, as described below.
fea681da
MK
119
120The value of
121.I pid
f2351505 122can be:
fea681da 123.IP "< \-1"
f2351505 124meaning wait for any child process whose process group ID is
fea681da
MK
125equal to the absolute value of
126.IR pid .
127.IP \-1
f2351505 128meaning wait for any child process.
fea681da 129.IP 0
f2351505 130meaning wait for any child process whose process group ID is
fea681da
MK
131equal to that of the calling process.
132.IP "> 0"
f2351505 133meaning wait for the child whose process ID is equal to the
fea681da
MK
134value of
135.IR pid .
136.PP
137The value of
138.I options
139is an OR of zero or more of the following constants:
a325cc60 140.TP 12
fea681da 141.B WNOHANG
f2351505 142return immediately if no child has exited.
fea681da
MK
143.TP
144.B WUNTRACED
d3b2ef5d 145also return if a child has stopped
f2351505 146(but not traced via
d3b2ef5d
MK
147.BR ptrace (2)).
148Status for
149.I traced
150children which have stopped is provided
151even if this option is not specified.
f2351505 152.TP
31daf529 153.BR WCONTINUED " (since Linux 2.6.10)"
d3b2ef5d 154also return if a stopped child has been resumed by delivery of
f2351505 155.BR SIGCONT .
fea681da
MK
156.PP
157(For Linux-only options, see below.)
158.PP
159If
160.I status
8478ee02 161is not NULL,
f2351505
MK
162.BR wait ()
163and
164.BR waitpid ()
a8d55537 165store status information in the \fIint\fP to which it points.
f2351505
MK
166This integer can be inspected with the following macros (which
167take the integer itself as an argument, not a pointer to it,
168as is done in
169.BR wait ()
170and
171.BR waitpid ()!):
fea681da
MK
172.TP
173.BI WIFEXITED( status )
174returns true if the child terminated normally, that is,
f2351505
MK
175by calling
176.BR exit (3)
177or
a5e0a0e4 178.BR _exit (2),
f2351505 179or by returning from main().
fea681da
MK
180.TP
181.BI WEXITSTATUS( status )
f2351505
MK
182returns the exit status of the child.
183This consists of the least significant 8 bits of the
184.I status
185argument that the child specified in a call to
0bfa087b 186.BR exit (3)
f2351505 187or
0bfa087b 188.BR _exit (2)
f2351505
MK
189or as the argument for a return statement in main().
190This macro should only be employed if
fea681da
MK
191.B WIFEXITED
192returned true.
193.TP
194.BI WIFSIGNALED( status )
f2351505 195returns true if the child process was terminated by a signal.
fea681da
MK
196.TP
197.BI WTERMSIG( status )
198returns the number of the signal that caused the child process to
c13182ef
MK
199terminate.
200This macro should only be employed if
f2351505
MK
201.B WIFSIGNALED
202returned true.
203.TP
204.BI WCOREDUMP( status )
205returns true if the child produced a core dump.
206This macro should only be employed if
fea681da 207.B WIFSIGNALED
f2351505
MK
208returned true.
209This macro is not specified in POSIX.1-2001 and is not available on
210some Unix implementations (e.g., AIX, SunOS).
211Only use this enclosed in #ifdef WCOREDUMP ... #endif.
fea681da
MK
212.TP
213.BI WIFSTOPPED( status )
f2351505
MK
214returns true if the child process was stopped by delivery of a signal;
215this is only possible if the call was done using
0daa9e92 216.B WUNTRACED
fea681da
MK
217or when the child is being traced (see
218.BR ptrace (2)).
219.TP
220.BI WSTOPSIG( status )
c13182ef
MK
221returns the number of the signal which caused the child to stop.
222This macro should only be employed if
fea681da 223.B WIFSTOPPED
f2351505
MK
224returned true.
225.TP
226.BI WIFCONTINUED( status )
31daf529 227(since Linux 2.6.10)
f2351505
MK
228returns true if the child process was resumed by delivery of
229.BR SIGCONT .
230.SS "waitid()"
231The
232.BR waitid ()
233system call (available since Linux 2.6.9) provides more precise
234control over which child state changes to wait for.
235
236The
237.I idtype
238and
239.I id
240arguments select the child(ren) to wait for, as follows:
241.IP "\fIidtype\fP == \fBP_PID\fP"
242Wait for the child whose process ID matches
243.IR id .
244.IP "\fIidtype\fP == \fBP_PGID\fP"
245Wait for any child whose process group ID matches
246.IR id .
247.IP "\fIidtype\fP == \fBP_ALL\fP"
248Wait for any child;
249.I id
250is ignored.
251.PP
252The child state changes to wait for are specified by ORing
253one or more of the following flags in
254.IR options :
a325cc60 255.TP 12
f2351505
MK
256.B WEXITED
257Wait for children that have terminated.
258.TP
259.B WSTOPPED
260Wait for children that have been stopped by delivery of a signal.
261.TP
262.B WCONTINUED
263Wait for (previously stopped) children that have been
264resumed by delivery of
265.BR SIGCONT .
266.PP
267The following flags may additionally be ORed in
268.IR options :
a325cc60 269.TP 12
f2351505
MK
270.B WNOHANG
271As for
272.BR waitpid ().
273.TP
274.B WNOWAIT
275Leave the child in a waitable state; a later wait call
276can be used to again retrieve the child status information.
277.PP
278Upon successful return,
279.BR waitid ()
280fills in the following fields of the
281.I siginfo_t
282structure pointed to by
283.IR infop :
a325cc60
MK
284.TP 12
285\fIsi_pid\fP
f2351505 286The process ID of the child.
a325cc60
MK
287.TP
288\fIsi_uid\fP
f2351505 289The real user ID of the child.
04d6ea6b 290(This field is not set on most other implementations.)
a325cc60
MK
291.TP
292\fIsi_signo\fP
f2351505
MK
293Always set to
294.BR SIGCHLD .
a325cc60
MK
295.TP
296\fIsi_status\fP
f2351505
MK
297Either the exit status of the child, as given to
298.BR _exit (2)
299(or
300.BR exit (3)),
301or the signal that caused the child to terminate, stop, or continue.
c13182ef 302The
f2351505
MK
303.I si_code
304field can be used to determine how to interpret this field.
a325cc60
MK
305.TP
306\fIsi_code\fP
f2351505
MK
307Set to one of:
308.B CLD_EXITED
309(child called
310.BR _exit (2));
311.B CLD_KILLED
312(child killed by signal);
313.B CLD_STOPPED
314(child stopped by signal); or
315.B CLD_CONTINUED
316(child continued by
317.BR SIGCONT ).
318.PP
319If
320.B WNOHANG
321was specified in
322.I options
323and there were no children in a waitable state, then
324.BR waitid ()
325returns 0 immediately and
326the state of the
327.I siginfo_t
328structure pointed to by
329.I infop
330is unspecified.
c13182ef 331.\" POSIX.1-2001 leaves this possibility unspecified; most
f2351505
MK
332.\" implementations (including Linux) zero out the structure
333.\" in this case, but at at least one implementation (AIX 5.1)
334.\" does not -- MTK Nov 04
335To distinguish this case from that where a child was in a
336waitable state, zero out the
337.I si_pid
eba72288 338field before the call and check for a non-zero value in this field
f2351505 339after the call returns.
fea681da 340.SH "RETURN VALUE"
f2351505 341.BR wait ():
d3b2ef5d 342on success, returns the process ID of the terminated child;
f2351505
MK
343on error, \-1 is returned.
344
a5e0a0e4 345.BR waitpid ():
d3b2ef5d 346on success, returns the process ID of the child whose state has changed;
f2351505 347if
fea681da 348.B WNOHANG
55ae3c86 349was specified and one or more child(ren) specified by
f2351505 350.I pid
55ae3c86
MK
351exist, but have not yet changed state, then 0 is returned.
352On error, \-1 is returned.
f2351505 353
a5e0a0e4 354.BR waitid ():
c13182ef 355returns 0 on success or
f2351505
MK
356if
357.B WNOHANG
358was specified and no child(ren) specified by
359.I id
360has yet changed state;
361on error, \-1 is returned.
362
363Each of these calls sets
fea681da 364.I errno
f2351505 365to an appropriate value in the case of an error.
fea681da
MK
366.SH ERRORS
367.TP
0daa9e92 368.B ECHILD
c13182ef 369(for
e1d6264d 370.BR wait ())
fea681da
MK
371The calling process does not have any unwaited-for children.
372.TP
0daa9e92 373.B ECHILD
c13182ef
MK
374(for
375.BR waitpid ()
376or
e1d6264d 377.BR waitid ())
f2351505 378The process specified by
fea681da 379.I pid
f2351505
MK
380.RB ( waitpid ())
381or
382.I idtype
383and
384.I id
385.RB ( waitid ())
fea681da 386does not exist or is not a child of the calling process.
8bd58774
MK
387(This can happen for one's own child if the action for
388.B SIGCHLD
389is set to
390.BR SIG_IGN .
4fb31341 391See also the \fILinux Notes\fP section about threads.)
fea681da
MK
392.TP
393.B EINTR
394.B WNOHANG
395was not set and an unblocked signal or a
396.B SIGCHLD
01538d0d
MK
397was caught; see
398.BR signal (7).
fea681da
MK
399.TP
400.B EINVAL
401The
402.I options
403argument was invalid.
2dd578fd
MK
404.SH "CONFORMING TO"
405SVr4, 4.3BSD, POSIX.1-2001.
fea681da 406.SH NOTES
f2351505
MK
407A child that terminates, but has not been waited for becomes a "zombie".
408The kernel maintains a minimal set of information about the zombie
409process (PID, termination status, resource usage information)
410in order to allow the parent to later perform a wait to obtain
411information about the child.
412As long as a zombie is not removed from the system via a wait,
413it will consume a slot in the kernel process table, and if
414this table fills, it will not be possible to create further processes.
415If a parent process terminates, then its "zombie" children (if any)
416are adopted by
417.BR init (8),
418which automatically performs a wait to remove the zombies.
419
420POSIX.1-2001 specifies that if the disposition of
421.B SIGCHLD
c13182ef 422is set to
f2351505 423.B SIG_IGN
c13182ef 424or the
f2351505
MK
425.B SA_NOCLDWAIT
426flag is set for
0daa9e92 427.B SIGCHLD
c13182ef 428(see
f2351505
MK
429.BR sigaction (2)),
430then children that terminate do not become zombies and a call to
fea681da
MK
431.BR wait ()
432or
433.BR waitpid ()
f2351505 434will block until all children have terminated, and then fail with
fea681da 435.I errno
f2351505
MK
436set to
437.BR ECHILD .
d9bfdb9c 438(The original POSIX standard left the behavior of setting
f2351505
MK
439.B SIGCHLD
440to
441.B SIG_IGN
0e464c2f
MK
442unspecified.
443Note that even though the default disposition of
444.B SIGCHLD
445is "ignore", explicitly setting the disposition to
446.B SIG_IGN
447results in different treatment of zombie process children.)
f2351505
MK
448Linux 2.6 conforms to this specification.
449However, Linux 2.4 (and earlier) does not:
fea681da 450if a
c13182ef
MK
451.BR wait ()
452or
e1d6264d 453.BR waitpid ()
f2351505
MK
454call is made while
455.B SIGCHLD
456is being ignored, the call behaves just as though
457.B SIGCHLD
704a18f0 458were not being ignored, that is, the call blocks until the next child
d3b2ef5d 459terminates and then returns the process ID and status of that child.
4fb31341 460.SS Linux Notes
fea681da 461In the Linux kernel, a kernel-scheduled thread is not a distinct
c13182ef
MK
462construct from a process.
463Instead, a thread is simply a process
fea681da
MK
464that is created using the Linux-unique
465.BR clone (2)
466system call; other routines such as the portable
467.BR pthread_create (3)
468call are implemented using
469.BR clone (2).
470Before Linux 2.4, a thread was just a special case of a process,
471and as a consequence one thread could not wait on the children
472of another thread, even when the latter belongs to the same thread group.
473However, POSIX prescribes such functionality, and since Linux 2.4
474a thread can, and by default will, wait on children of other threads
475in the same thread group.
476.LP
8382f16d 477The following Linux-specific
fea681da
MK
478.I options
479are for use with children created using
948fb4ed
MK
480.BR clone (2);
481they cannot be used with
482.BR waitid ():
fea681da
MK
483.TP
484.B __WCLONE
485.\" since 0.99pl10
c13182ef
MK
486Wait for "clone" children only.
487If omitted then wait for "non-clone" children only.
488(A "clone" child is one which delivers no signal, or a signal other than
fea681da
MK
489.B SIGCHLD
490to its parent upon termination.)
491This option is ignored if
492.B __WALL
493is also specified.
494.TP
31daf529 495.BR __WALL " (since Linux 2.4)"
fea681da 496.\" since patch-2.3.48
31daf529 497Wait for all children, regardless of
fea681da
MK
498type ("clone" or "non-clone").
499.TP
31daf529 500.BR __WNOTHREAD " (since Linux 2.4)"
fea681da 501.\" since patch-2.4.0-test8
31daf529 502Do not wait for children of other threads in
c13182ef
MK
503the same thread group.
504This was the default before Linux 2.4.
1fa343d1 505.SH EXAMPLE
cde9f44b 506.\" fork.2 refers to this example program.
c13182ef 507The following program demonstrates the use of
19dbfd0a 508.BR fork (2)
c13182ef 509and
2777b1ca 510.BR waitpid ().
1fa343d1 511The program creates a child process.
c13182ef
MK
512If no command-line argument is supplied to the program,
513then the child suspends its execution using
1fa343d1
MK
514.BR pause (2),
515to allow the user to send signals to the child.
516Otherwise, if a command-line argument is supplied,
c13182ef 517then the child exits immediately,
1fa343d1
MK
518using the integer supplied on the command line as the exit status.
519The parent process executes a loop that monitors the child using
2777b1ca 520.BR waitpid (),
d9bfdb9c 521and uses the W*() macros described above to analyze the wait status value.
1fa343d1
MK
522
523The following shell session demonstrates the use of the program:
9d0cc711 524.in +4n
1fa343d1
MK
525.nf
526
b43a3b30 527.RB "$" " ./a.out &"
1fa343d1
MK
528Child PID is 32360
529[1] 32359
b43a3b30 530.RB "$" " kill \-STOP 32360"
1fa343d1 531stopped by signal 19
b43a3b30 532.RB "$" " kill \-CONT 32360"
1fa343d1 533continued
b43a3b30 534.RB "$" " kill \-TERM 32360"
1fa343d1
MK
535killed by signal 15
536[1]+ Done ./a.out
537$
9d0cc711
MK
538.fi
539.in
9c330504 540.SS Program source
d84d0300 541\&
9d0cc711 542.nf
1fa343d1
MK
543#include <sys/wait.h>
544#include <stdlib.h>
545#include <unistd.h>
546#include <stdio.h>
547
548int
549main(int argc, char *argv[])
550{
551 pid_t cpid, w;
552 int status;
553
554 cpid = fork();
29059a65 555 if (cpid == \-1) {
45949175
MK
556 perror("fork");
557 exit(EXIT_FAILURE);
558 }
1fa343d1
MK
559
560 if (cpid == 0) { /* Code executed by child */
561 printf("Child PID is %ld\\n", (long) getpid());
562 if (argc == 1)
563 pause(); /* Wait for signals */
564 _exit(atoi(argv[1]));
565
566 } else { /* Code executed by parent */
567 do {
568 w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
29059a65 569 if (w == \-1) {
45949175
MK
570 perror("waitpid");
571 exit(EXIT_FAILURE);
572 }
1fa343d1
MK
573
574 if (WIFEXITED(status)) {
575 printf("exited, status=%d\\n", WEXITSTATUS(status));
576 } else if (WIFSIGNALED(status)) {
577 printf("killed by signal %d\\n", WTERMSIG(status));
578 } else if (WIFSTOPPED(status)) {
579 printf("stopped by signal %d\\n", WSTOPSIG(status));
580 } else if (WIFCONTINUED(status)) {
581 printf("continued\\n");
582 }
583 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
584 exit(EXIT_SUCCESS);
585 }
586}
1fa343d1 587.fi
fea681da 588.SH "SEE ALSO"
f2351505 589.BR _exit (2),
fea681da 590.BR clone (2),
f2351505
MK
591.BR fork (2),
592.BR kill (2),
fea681da 593.BR ptrace (2),
f2351505 594.BR sigaction (2),
fea681da
MK
595.BR signal (2),
596.BR wait4 (2),
597.BR pthread_create (3),
53a1443c 598.BR credentials (7),
fea681da 599.BR signal (7)