Changes.old: tfix
[man-pages.git] / man2 / wait.2
blob938a882fed346cd91eebfb2b987ea6a70019255a
1 .\" Copyright (c) 1993 by Thomas Koenig <ig25@rz.uni-karlsruhe.de>
2 .\" and Copyright (c) 2004 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Sat Jul 24 13:30:06 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Sun Aug 21 17:42:42 1994 by Rik Faith <faith@cs.unc.edu>
28 .\"          (Thanks to Koen Holtman <koen@win.tue.nl>)
29 .\" Modified Wed May 17 15:54:12 1995 by Rik Faith <faith@cs.unc.edu>
30 .\"           To remove *'s from status in macros (Thanks to Michael Shields).
31 .\" Modified as suggested by Nick Duffek <nsd@bbc.com>, aeb, 960426
32 .\" Modified Mon Jun 23 14:09:52 1997 by aeb - add EINTR.
33 .\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
34 .\" Modified Mon Jul 24 21:37:38 2000 by David A. Wheeler
35 .\"          <dwheeler@dwheeler.com> - noted thread issues.
36 .\" Modified 26 Jun 01 by Michael Kerrisk
37 .\"          Added __WCLONE, __WALL, and __WNOTHREAD descriptions
38 .\" Modified 2001-09-25, aeb
39 .\" Modified 26 Jun 01 by Michael Kerrisk, <mtk.manpages@gmail.com>
40 .\"     Updated notes on setting disposition of SIGCHLD to SIG_IGN
41 .\" 2004-11-11, mtk
42 .\"     Added waitid(2); added WCONTINUED and WIFCONTINUED()
43 .\"     Added text on SA_NOCLDSTOP
44 .\"     Updated discussion of SA_NOCLDWAIT to reflect 2.6 behavior
45 .\"     Much other text rewritten
46 .\" 2005-05-10, mtk, __W* flags can't be used with waitid()
47 .\" 2008-07-04, mtk, removed erroneous text about SA_NOCLDSTOP
48 .\"
49 .TH WAIT 2 2017-05-03 "Linux" "Linux Programmer's Manual"
50 .SH NAME
51 wait, waitpid, waitid \- wait for process to change state
52 .SH SYNOPSIS
53 .B #include <sys/types.h>
54 .br
55 .B #include <sys/wait.h>
56 .sp
57 .BI "pid_t wait(int *" "wstatus" );
59 .BI "pid_t waitpid(pid_t " pid ", int *" wstatus ", int " options );
61 .BI "int waitid(idtype_t " idtype ", id_t " id \
62 ", siginfo_t *" infop ", int " options );
63                 /* This is the glibc and POSIX interface; see
64                    NOTES for information on the raw system call. */
65 .sp
66 .in -4n
67 Feature Test Macro Requirements for glibc (see
68 .BR feature_test_macros (7)):
69 .in
70 .sp
71 .ad l
72 .PD 0
73 .BR waitid ():
74 .RS 4
75 Since glibc 2.26:
76 _XOPEN_SOURCE >= 500 ||
77 .\"    (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
78     _POSIX_C_SOURCE\ >=\ 200809L
79 .br
80 Glibc 2.25 and earlier:
81     _XOPEN_SOURCE
82         || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
83         || /* Glibc versions <= 2.19: */ _BSD_SOURCE
84 .RE
85 .PD
86 .ad
87 .SH DESCRIPTION
88 All of these system calls are used to wait for state changes
89 in a child of the calling process, and obtain information
90 about the child whose state has changed.
91 A state change is considered to be: the child terminated;
92 the child was stopped by a signal; or the child was resumed by a signal.
93 In the case of a terminated child, performing a wait allows
94 the system to release the resources associated with the child;
95 if a wait is not performed, then the terminated child remains in
96 a "zombie" state (see NOTES below).
98 If a child has already changed state, then these calls return immediately.
99 Otherwise, they block until either a child changes state or
100 a signal handler interrupts the call (assuming that system calls
101 are not automatically restarted using the
102 .B SA_RESTART
103 flag of
104 .BR sigaction (2)).
105 In the remainder of this page, a child whose state has changed
106 and which has not yet been waited upon by one of these system
107 calls is termed
108 .IR waitable .
109 .SS wait() and waitpid()
111 .BR wait ()
112 system call suspends execution of the calling process until one of its
113 children terminates.
114 The call
115 .I wait(&wstatus)
116 is equivalent to:
119     waitpid(\-1, &wstatus, 0);
123 .BR waitpid ()
124 system call suspends execution of the calling process until a
125 child specified by
126 .I pid
127 argument has changed state.
128 By default,
129 .BR waitpid ()
130 waits only for terminated children, but this behavior is modifiable
131 via the
132 .I options
133 argument, as described below.
135 The value of
136 .I pid
137 can be:
138 .IP "< \-1"
139 meaning wait for any child process whose process group ID is
140 equal to the absolute value of
141 .IR pid .
142 .IP \-1
143 meaning wait for any child process.
144 .IP 0
145 meaning wait for any child process whose process group ID is
146 equal to that of the calling process.
147 .IP "> 0"
148 meaning wait for the child whose process ID is equal to the
149 value of
150 .IR pid .
152 The value of
153 .I options
154 is an OR of zero or more of the following constants:
155 .TP 12
156 .B WNOHANG
157 return immediately if no child has exited.
159 .B WUNTRACED
160 also return if a child has stopped
161 (but not traced via
162 .BR ptrace (2)).
163 Status for
164 .I traced
165 children which have stopped is provided
166 even if this option is not specified.
168 .BR WCONTINUED " (since Linux 2.6.10)"
169 also return if a stopped child has been resumed by delivery of
170 .BR SIGCONT .
172 (For Linux-only options, see below.)
175 .I wstatus
176 is not NULL,
177 .BR wait ()
179 .BR waitpid ()
180 store status information in the \fIint\fP to which it points.
181 This integer can be inspected with the following macros (which
182 take the integer itself as an argument, not a pointer to it,
183 as is done in
184 .BR wait ()
186 .BR waitpid ()!):
188 .BI WIFEXITED( wstatus )
189 returns true if the child terminated normally, that is,
190 by calling
191 .BR exit (3)
193 .BR _exit (2),
194 or by returning from main().
196 .BI WEXITSTATUS( wstatus )
197 returns the exit status of the child.
198 This consists of the least significant 8 bits of the
199 .I status
200 argument that the child specified in a call to
201 .BR exit (3)
203 .BR _exit (2)
204 or as the argument for a return statement in main().
205 This macro should be employed only if
206 .B WIFEXITED
207 returned true.
209 .BI WIFSIGNALED( wstatus )
210 returns true if the child process was terminated by a signal.
212 .BI WTERMSIG( wstatus )
213 returns the number of the signal that caused the child process to
214 terminate.
215 This macro should be employed only if
216 .B WIFSIGNALED
217 returned true.
219 .BI WCOREDUMP( wstatus )
220 returns true if the child produced a core dump.
221 This macro should be employed only if
222 .B WIFSIGNALED
223 returned true.
225 This macro is not specified in POSIX.1-2001 and is not available on
226 some UNIX implementations (e.g., AIX, SunOS).
227 Therefore, enclose its use inside
228 .IR "#ifdef WCOREDUMP ... #endif" .
230 .BI WIFSTOPPED( wstatus )
231 returns true if the child process was stopped by delivery of a signal;
232 this is possible only if the call was done using
233 .B WUNTRACED
234 or when the child is being traced (see
235 .BR ptrace (2)).
237 .BI WSTOPSIG( wstatus )
238 returns the number of the signal which caused the child to stop.
239 This macro should be employed only if
240 .B WIFSTOPPED
241 returned true.
243 .BI WIFCONTINUED( wstatus )
244 (since Linux 2.6.10)
245 returns true if the child process was resumed by delivery of
246 .BR SIGCONT .
247 .SS waitid()
249 .BR waitid ()
250 system call (available since Linux 2.6.9) provides more precise
251 control over which child state changes to wait for.
254 .I idtype
256 .I id
257 arguments select the child(ren) to wait for, as follows:
258 .IP "\fIidtype\fP == \fBP_PID\fP"
259 Wait for the child whose process ID matches
260 .IR id .
261 .IP "\fIidtype\fP == \fBP_PGID\fP"
262 Wait for any child whose process group ID matches
263 .IR id .
264 .IP "\fIidtype\fP == \fBP_ALL\fP"
265 Wait for any child;
266 .I id
267 is ignored.
269 The child state changes to wait for are specified by ORing
270 one or more of the following flags in
271 .IR options :
272 .TP 12
273 .B WEXITED
274 Wait for children that have terminated.
276 .B WSTOPPED
277 Wait for children that have been stopped by delivery of a signal.
279 .B WCONTINUED
280 Wait for (previously stopped) children that have been
281 resumed by delivery of
282 .BR SIGCONT .
284 The following flags may additionally be ORed in
285 .IR options :
286 .TP 12
287 .B WNOHANG
288 As for
289 .BR waitpid ().
291 .B WNOWAIT
292 Leave the child in a waitable state; a later wait call
293 can be used to again retrieve the child status information.
295 Upon successful return,
296 .BR waitid ()
297 fills in the following fields of the
298 .I siginfo_t
299 structure pointed to by
300 .IR infop :
301 .TP 12
302 \fIsi_pid\fP
303 The process ID of the child.
305 \fIsi_uid\fP
306 The real user ID of the child.
307 (This field is not set on most other implementations.)
309 \fIsi_signo\fP
310 Always set to
311 .BR SIGCHLD .
313 \fIsi_status\fP
314 Either the exit status of the child, as given to
315 .BR _exit (2)
317 .BR exit (3)),
318 or the signal that caused the child to terminate, stop, or continue.
320 .I si_code
321 field can be used to determine how to interpret this field.
323 \fIsi_code\fP
324 Set to one of:
325 .B CLD_EXITED
326 (child called
327 .BR _exit (2));
328 .B CLD_KILLED
329 (child killed by signal);
330 .B CLD_DUMPED
331 (child killed by signal, and dumped core);
332 .B CLD_STOPPED
333 (child stopped by signal);
334 .B CLD_TRAPPED
335 (traced child has trapped); or
336 .B CLD_CONTINUED
337 (child continued by
338 .BR SIGCONT ).
341 .B WNOHANG
342 was specified in
343 .I options
344 and there were no children in a waitable state, then
345 .BR waitid ()
346 returns 0 immediately and
347 the state of the
348 .I siginfo_t
349 structure pointed to by
350 .I infop
351 is unspecified.
352 .\" POSIX.1-2001 leaves this possibility unspecified; most
353 .\" implementations (including Linux) zero out the structure
354 .\" in this case, but at least one implementation (AIX 5.1)
355 .\" does not -- MTK Nov 04
356 To distinguish this case from that where a child was in a
357 waitable state, zero out the
358 .I si_pid
359 field before the call and check for a nonzero value in this field
360 after the call returns.
361 .SH RETURN VALUE
362 .BR wait ():
363 on success, returns the process ID of the terminated child;
364 on error, \-1 is returned.
366 .BR waitpid ():
367 on success, returns the process ID of the child whose state has changed;
369 .B WNOHANG
370 was specified and one or more child(ren) specified by
371 .I pid
372 exist, but have not yet changed state, then 0 is returned.
373 On error, \-1 is returned.
375 .BR waitid ():
376 returns 0 on success or
378 .B WNOHANG
379 was specified and no child(ren) specified by
380 .I id
381 has yet changed state;
382 on error, \-1 is returned.
383 .\" FIXME As reported by Vegard Nossum, if infop is NULL, then waitid()
384 .\" returns the PID of the child.  Either this is a bug, or it is intended
385 .\" behavior that needs to be documented.  See my Jan 2009 LKML mail
386 .\" "waitid() return value strangeness when infop is NULL".
388 Each of these calls sets
389 .I errno
390 to an appropriate value in the case of an error.
391 .SH ERRORS
393 .B ECHILD
394 (for
395 .BR wait ())
396 The calling process does not have any unwaited-for children.
398 .B ECHILD
399 (for
400 .BR waitpid ()
402 .BR waitid ())
403 The process specified by
404 .I pid
405 .RB ( waitpid ())
407 .I idtype
409 .I id
410 .RB ( waitid ())
411 does not exist or is not a child of the calling process.
412 (This can happen for one's own child if the action for
413 .B SIGCHLD
414 is set to
415 .BR SIG_IGN .
416 See also the \fILinux Notes\fP section about threads.)
418 .B EINTR
419 .B WNOHANG
420 was not set and an unblocked signal or a
421 .B SIGCHLD
422 was caught; see
423 .BR signal (7).
425 .B EINVAL
427 .I options
428 argument was invalid.
429 .SH CONFORMING TO
430 SVr4, 4.3BSD, POSIX.1-2001.
431 .SH NOTES
432 A child that terminates, but has not been waited for becomes a "zombie".
433 The kernel maintains a minimal set of information about the zombie
434 process (PID, termination status, resource usage information)
435 in order to allow the parent to later perform a wait to obtain
436 information about the child.
437 As long as a zombie is not removed from the system via a wait,
438 it will consume a slot in the kernel process table, and if
439 this table fills, it will not be possible to create further processes.
440 If a parent process terminates, then its "zombie" children (if any)
441 are adopted by
442 .BR init (1),
443 (or by the nearest "subreaper" process as defined through the use of the
444 .BR prctl (2)
445 .B PR_SET_CHILD_SUBREAPER
446 operation);
447 .BR init (1)
448 automatically performs a wait to remove the zombies.
450 POSIX.1-2001 specifies that if the disposition of
451 .B SIGCHLD
452 is set to
453 .B SIG_IGN
454 or the
455 .B SA_NOCLDWAIT
456 flag is set for
457 .B SIGCHLD
458 (see
459 .BR sigaction (2)),
460 then children that terminate do not become zombies and a call to
461 .BR wait ()
463 .BR waitpid ()
464 will block until all children have terminated, and then fail with
465 .I errno
466 set to
467 .BR ECHILD .
468 (The original POSIX standard left the behavior of setting
469 .B SIGCHLD
471 .B SIG_IGN
472 unspecified.
473 Note that even though the default disposition of
474 .B SIGCHLD
475 is "ignore", explicitly setting the disposition to
476 .B SIG_IGN
477 results in different treatment of zombie process children.)
479 Linux 2.6 conforms to the POSIX requirements.
480 However, Linux 2.4 (and earlier) does not:
481 if a
482 .BR wait ()
484 .BR waitpid ()
485 call is made while
486 .B SIGCHLD
487 is being ignored, the call behaves just as though
488 .B SIGCHLD
489 were not being ignored, that is, the call blocks until the next child
490 terminates and then returns the process ID and status of that child.
491 .SS Linux notes
492 In the Linux kernel, a kernel-scheduled thread is not a distinct
493 construct from a process.
494 Instead, a thread is simply a process
495 that is created using the Linux-unique
496 .BR clone (2)
497 system call; other routines such as the portable
498 .BR pthread_create (3)
499 call are implemented using
500 .BR clone (2).
501 Before Linux 2.4, a thread was just a special case of a process,
502 and as a consequence one thread could not wait on the children
503 of another thread, even when the latter belongs to the same thread group.
504 However, POSIX prescribes such functionality, and since Linux 2.4
505 a thread can, and by default will, wait on children of other threads
506 in the same thread group.
508 The following Linux-specific
509 .I options
510 are for use with children created using
511 .BR clone (2);
512 they can also, since Linux 4.7,
513 .\" commit 91c4e8ea8f05916df0c8a6f383508ac7c9e10dba
514 be used with
515 .BR waitid ():
517 .B __WCLONE
518 .\" since 0.99pl10
519 Wait for "clone" children only.
520 If omitted, then wait for "non-clone" children only.
521 (A "clone" child is one which delivers no signal, or a signal other than
522 .B SIGCHLD
523 to its parent upon termination.)
524 This option is ignored if
525 .B __WALL
526 is also specified.
528 .BR __WALL " (since Linux 2.4)"
529 .\" since patch-2.3.48
530 Wait for all children, regardless of
531 type ("clone" or "non-clone").
533 .BR __WNOTHREAD " (since Linux 2.4)"
534 .\" since patch-2.4.0-test8
535 Do not wait for children of other threads in
536 the same thread group.
537 This was the default before Linux 2.4.
539 Since Linux 4.7,
540 .\" commit bf959931ddb88c4e4366e96dd22e68fa0db9527c
541 .\" prevents cases where an unreapable zombie is created if
542 .\" /sbin/init doesn't use __WALL.
544 .B __WALL
545 flag is automatically implied if the child is being ptraced.
546 .SS C library/kernel differences
547 .BR wait ()
548 is actually a library function that (in glibc) is implemented as a call to
549 .BR wait4 (2).
551 On some architectures, there is no
552 .BR waitpid ()
553 system call;
554 .\" e.g., i386 has the system call, but not x86-64
555 instead, this interface is implemented via a C library
556 wrapper function that calls
557 .BR wait4 (2).
559 The raw
560 .BR waitid ()
561 system call takes a fifth argument, of type
562 .IR "struct rusage\ *" .
563 If this argument is non-NULL,
564 then it is used to return resource usage information about the child,
565 in the same manner as
566 .BR wait4 (2).
568 .BR getrusage (2)
569 for details.
570 .SH BUGS
571 According to POSIX.1-2008, an application calling
572 .BR waitid ()
573 must ensure that
574 .I infop
575 points to a
576 .I siginfo_t
577 structure (i.e., that it is a non-null pointer).
578 On Linux, if
579 .I infop
580 is NULL,
581 .BR waitid ()
582 succeeds, and returns the process ID of the waited-for child.
583 Applications should avoid relying on this inconsistent,
584 nonstandard, and unnecessary feature.
585 .SH EXAMPLE
586 .\" fork.2 refers to this example program.
587 The following program demonstrates the use of
588 .BR fork (2)
590 .BR waitpid ().
591 The program creates a child process.
592 If no command-line argument is supplied to the program,
593 then the child suspends its execution using
594 .BR pause (2),
595 to allow the user to send signals to the child.
596 Otherwise, if a command-line argument is supplied,
597 then the child exits immediately,
598 using the integer supplied on the command line as the exit status.
599 The parent process executes a loop that monitors the child using
600 .BR waitpid (),
601 and uses the W*() macros described above to analyze the wait status value.
603 The following shell session demonstrates the use of the program:
604 .in +4n
607 .RB "$" " ./a.out &"
608 Child PID is 32360
609 [1] 32359
610 .RB "$" " kill \-STOP 32360"
611 stopped by signal 19
612 .RB "$" " kill \-CONT 32360"
613 continued
614 .RB "$" " kill \-TERM 32360"
615 killed by signal 15
616 [1]+  Done                    ./a.out
620 .SS Program source
623 #include <sys/wait.h>
624 #include <stdlib.h>
625 #include <unistd.h>
626 #include <stdio.h>
629 main(int argc, char *argv[])
631     pid_t cpid, w;
632     int wstatus;
634     cpid = fork();
635     if (cpid == \-1) {
636         perror("fork");
637         exit(EXIT_FAILURE);
638     }
640     if (cpid == 0) {            /* Code executed by child */
641         printf("Child PID is %ld\\n", (long) getpid());
642         if (argc == 1)
643             pause();                    /* Wait for signals */
644         _exit(atoi(argv[1]));
646     } else {                    /* Code executed by parent */
647         do {
648             w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
649             if (w == \-1) {
650                 perror("waitpid");
651                 exit(EXIT_FAILURE);
652             }
654             if (WIFEXITED(wstatus)) {
655                 printf("exited, status=%d\\n", WEXITSTATUS(wstatus));
656             } else if (WIFSIGNALED(wstatus)) {
657                 printf("killed by signal %d\\n", WTERMSIG(wstatus));
658             } else if (WIFSTOPPED(wstatus)) {
659                 printf("stopped by signal %d\\n", WSTOPSIG(wstatus));
660             } else if (WIFCONTINUED(wstatus)) {
661                 printf("continued\\n");
662             }
663         } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
664         exit(EXIT_SUCCESS);
665     }
668 .SH SEE ALSO
669 .BR _exit (2),
670 .BR clone (2),
671 .BR fork (2),
672 .BR kill (2),
673 .BR ptrace (2),
674 .BR sigaction (2),
675 .BR signal (2),
676 .BR wait4 (2),
677 .BR pthread_create (3),
678 .BR credentials (7),
679 .BR signal (7)