]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/wait.2
1c0c57769375aff951601828254a2d89b89f6625
[thirdparty/man-pages.git] / man2 / wait.2
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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
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
21 .\" 2004-11-11, mtk
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
28 .\"
29 .TH WAIT 2 2021-08-27 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
30 .SH NAME
31 wait, waitpid, waitid \- wait for process to change state
32 .SH LIBRARY
33 Standard C library
34 .RI ( libc ", " \-lc )
35 .SH SYNOPSIS
36 .nf
37 .B #include <sys/wait.h>
38 .PP
39 .BI "pid_t wait(int *" "wstatus" );
40 .BI "pid_t waitpid(pid_t " pid ", int *" wstatus ", int " options );
41 .PP
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. */
46 .fi
47 .PP
48 .RS -4
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .RE
52 .PP
53 .BR waitid ():
54 .nf
55 Since glibc 2.26:
56 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
57 .\" (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
58 Glibc 2.25 and earlier:
59 _XOPEN_SOURCE
60 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
61 || /* Glibc <= 2.19: */ _BSD_SOURCE
62 .fi
63 .SH DESCRIPTION
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).
73 .PP
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
78 .B SA_RESTART
79 flag of
80 .BR sigaction (2)).
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
83 calls is termed
84 .IR waitable .
85 .SS wait() and waitpid()
86 The
87 .BR wait ()
88 system call suspends execution of the calling thread until one of its
89 children terminates.
90 The call
91 .I wait(&wstatus)
92 is equivalent to:
93 .PP
94 .in +4n
95 .EX
96 waitpid(\-1, &wstatus, 0);
97 .EE
98 .in
99 .PP
100 The
101 .BR waitpid ()
102 system call suspends execution of the calling thread until a
103 child specified by
104 .I pid
105 argument has changed state.
106 By default,
107 .BR waitpid ()
108 waits only for terminated children, but this behavior is modifiable
109 via the
110 .I options
111 argument, as described below.
112 .PP
113 The value of
114 .I pid
115 can be:
116 .IP "< \-1"
117 meaning wait for any child process whose process group ID is
118 equal to the absolute value of
119 .IR pid .
120 .IP \-1
121 meaning wait for any child process.
122 .IP 0
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
125 .BR waitpid ().
126 .IP "> 0"
127 meaning wait for the child whose process ID is equal to the
128 value of
129 .IR pid .
130 .PP
131 The value of
132 .I options
133 is an OR of zero or more of the following constants:
134 .TP
135 .B WNOHANG
136 return immediately if no child has exited.
137 .TP
138 .B WUNTRACED
139 also return if a child has stopped
140 (but not traced via
141 .BR ptrace (2)).
142 Status for
143 .I traced
144 children which have stopped is provided
145 even if this option is not specified.
146 .TP
147 .BR WCONTINUED " (since Linux 2.6.10)"
148 also return if a stopped child has been resumed by delivery of
149 .BR SIGCONT .
150 .PP
151 (For Linux-only options, see below.)
152 .PP
153 If
154 .I wstatus
155 is not NULL,
156 .BR wait ()
157 and
158 .BR waitpid ()
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,
162 as is done in
163 .BR wait ()
164 and
165 .BR waitpid ()!):
166 .TP
167 .BI WIFEXITED( wstatus )
168 returns true if the child terminated normally, that is,
169 by calling
170 .BR exit (3)
171 or
172 .BR _exit (2),
173 or by returning from main().
174 .TP
175 .BI WEXITSTATUS( wstatus )
176 returns the exit status of the child.
177 This consists of the least significant 8 bits of the
178 .I status
179 argument that the child specified in a call to
180 .BR exit (3)
181 or
182 .BR _exit (2)
183 or as the argument for a return statement in main().
184 This macro should be employed only if
185 .B WIFEXITED
186 returned true.
187 .TP
188 .BI WIFSIGNALED( wstatus )
189 returns true if the child process was terminated by a signal.
190 .TP
191 .BI WTERMSIG( wstatus )
192 returns the number of the signal that caused the child process to
193 terminate.
194 This macro should be employed only if
195 .B WIFSIGNALED
196 returned true.
197 .TP
198 .BI WCOREDUMP( wstatus )
199 returns true if the child produced a core dump (see
200 .BR core (5)).
201 This macro should be employed only if
202 .B WIFSIGNALED
203 returned true.
204 .IP
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" .
209 .TP
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
213 .B WUNTRACED
214 or when the child is being traced (see
215 .BR ptrace (2)).
216 .TP
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
220 .B WIFSTOPPED
221 returned true.
222 .TP
223 .BI WIFCONTINUED( wstatus )
224 (since Linux 2.6.10)
225 returns true if the child process was resumed by delivery of
226 .BR SIGCONT .
227 .SS waitid()
228 The
229 .BR waitid ()
230 system call (available since Linux 2.6.9) provides more precise
231 control over which child state changes to wait for.
232 .PP
233 The
234 .I idtype
235 and
236 .I id
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
240 .IR id .
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
244 .IR id .
245 (See
246 .BR pidfd_open (2)
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
250 .IR id .
251 Since Linux 5.4,
252 .\" commit 821cc7b0b205c0df64cce59aacc330af251fa8f7
253 if
254 .I id
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"
258 Wait for any child;
259 .I id
260 is ignored.
261 .PP
262 The child state changes to wait for are specified by ORing
263 one or more of the following flags in
264 .IR options :
265 .TP
266 .B WEXITED
267 Wait for children that have terminated.
268 .TP
269 .B WSTOPPED
270 Wait for children that have been stopped by delivery of a signal.
271 .TP
272 .B WCONTINUED
273 Wait for (previously stopped) children that have been
274 resumed by delivery of
275 .BR SIGCONT .
276 .PP
277 The following flags may additionally be ORed in
278 .IR options :
279 .TP
280 .B WNOHANG
281 As for
282 .BR waitpid ().
283 .TP
284 .B WNOWAIT
285 Leave the child in a waitable state; a later wait call
286 can be used to again retrieve the child status information.
287 .PP
288 Upon successful return,
289 .BR waitid ()
290 fills in the following fields of the
291 .I siginfo_t
292 structure pointed to by
293 .IR infop :
294 .TP
295 \fIsi_pid\fP
296 The process ID of the child.
297 .TP
298 \fIsi_uid\fP
299 The real user ID of the child.
300 (This field is not set on most other implementations.)
301 .TP
302 \fIsi_signo\fP
303 Always set to
304 .BR SIGCHLD .
305 .TP
306 \fIsi_status\fP
307 Either the exit status of the child, as given to
308 .BR _exit (2)
309 (or
310 .BR exit (3)),
311 or the signal that caused the child to terminate, stop, or continue.
312 The
313 .I si_code
314 field can be used to determine how to interpret this field.
315 .TP
316 \fIsi_code\fP
317 Set to one of:
318 .B CLD_EXITED
319 (child called
320 .BR _exit (2));
321 .B CLD_KILLED
322 (child killed by signal);
323 .B CLD_DUMPED
324 (child killed by signal, and dumped core);
325 .B CLD_STOPPED
326 (child stopped by signal);
327 .B CLD_TRAPPED
328 (traced child has trapped); or
329 .B CLD_CONTINUED
330 (child continued by
331 .BR SIGCONT ).
332 .PP
333 If
334 .B WNOHANG
335 was specified in
336 .I options
337 and there were no children in a waitable state, then
338 .BR waitid ()
339 returns 0 immediately and
340 the state of the
341 .I siginfo_t
342 structure pointed to by
343 .I infop
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
347 .I si_pid
348 field before the call and check for a nonzero value in this field
349 after the call returns.
350 .PP
351 POSIX.1-2008 Technical Corrigendum 1 (2013) adds the requirement that when
352 .B WNOHANG
353 is specified in
354 .I options
355 and there were no children in a waitable state, then
356 .BR waitid ()
357 should zero out the
358 .I si_pid
359 and
360 .I si_signo
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
364 .I si_pid
365 field before calling
366 .BR waitid ().
367 However,
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
373 .SH RETURN VALUE
374 .BR wait ():
375 on success, returns the process ID of the terminated child;
376 on failure, \-1 is returned.
377 .PP
378 .BR waitpid ():
379 on success, returns the process ID of the child whose state has changed;
380 if
381 .B WNOHANG
382 was specified and one or more child(ren) specified by
383 .I pid
384 exist, but have not yet changed state, then 0 is returned.
385 On failure, \-1 is returned.
386 .PP
387 .BR waitid ():
388 returns 0 on success or
389 if
390 .B WNOHANG
391 was specified and no child(ren) specified by
392 .I id
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".
399 .PP
400 On failure, each of these calls sets
401 .I errno
402 to indicate the error.
403 .SH ERRORS
404 .TP
405 .B EAGAIN
406 The PID file descriptor specified in
407 .I id
408 is nonblocking and the process that it refers to has not terminated.
409 .TP
410 .B ECHILD
411 (for
412 .BR wait ())
413 The calling process does not have any unwaited-for children.
414 .TP
415 .B ECHILD
416 (for
417 .BR waitpid ()
418 or
419 .BR waitid ())
420 The process specified by
421 .I pid
422 .RB ( waitpid ())
423 or
424 .I idtype
425 and
426 .I id
427 .RB ( waitid ())
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
430 .B SIGCHLD
431 is set to
432 .BR SIG_IGN .
433 See also the \fILinux Notes\fP section about threads.)
434 .TP
435 .B EINTR
436 .B WNOHANG
437 was not set and an unblocked signal or a
438 .B SIGCHLD
439 was caught; see
440 .BR signal (7).
441 .TP
442 .B EINVAL
443 The
444 .I options
445 argument was invalid.
446 .TP
447 .B ESRCH
448 (for
449 .BR wait ()
450 or
451 .BR waitpid ())
452 .I pid
453 is equal to
454 .BR INT_MIN .
455 .SH STANDARDS
456 SVr4, 4.3BSD, POSIX.1-2001.
457 .SH NOTES
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)
467 are adopted by
468 .BR init (1),
469 (or by the nearest "subreaper" process as defined through the use of the
470 .BR prctl (2)
471 .B PR_SET_CHILD_SUBREAPER
472 operation);
473 .BR init (1)
474 automatically performs a wait to remove the zombies.
475 .PP
476 POSIX.1-2001 specifies that if the disposition of
477 .B SIGCHLD
478 is set to
479 .B SIG_IGN
480 or the
481 .B SA_NOCLDWAIT
482 flag is set for
483 .B SIGCHLD
484 (see
485 .BR sigaction (2)),
486 then children that terminate do not become zombies and a call to
487 .BR wait ()
488 or
489 .BR waitpid ()
490 will block until all children have terminated, and then fail with
491 .I errno
492 set to
493 .BR ECHILD .
494 (The original POSIX standard left the behavior of setting
495 .B SIGCHLD
496 to
497 .B SIG_IGN
498 unspecified.
499 Note that even though the default disposition of
500 .B SIGCHLD
501 is "ignore", explicitly setting the disposition to
502 .B SIG_IGN
503 results in different treatment of zombie process children.)
504 .PP
505 Linux 2.6 conforms to the POSIX requirements.
506 However, Linux 2.4 (and earlier) does not:
507 if a
508 .BR wait ()
509 or
510 .BR waitpid ()
511 call is made while
512 .B SIGCHLD
513 is being ignored, the call behaves just as though
514 .B SIGCHLD
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.
517 .SS Linux notes
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
522 .BR clone (2)
523 system call; other routines such as the portable
524 .BR pthread_create (3)
525 call are implemented using
526 .BR clone (2).
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.
533 .PP
534 The following Linux-specific
535 .I options
536 are for use with children created using
537 .BR clone (2);
538 they can also, since Linux 4.7,
539 .\" commit 91c4e8ea8f05916df0c8a6f383508ac7c9e10dba
540 be used with
541 .BR waitid ():
542 .TP
543 .B __WCLONE
544 .\" since 0.99pl10
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
548 .B SIGCHLD
549 to its parent upon termination.)
550 This option is ignored if
551 .B __WALL
552 is also specified.
553 .TP
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").
558 .TP
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.
564 .PP
565 Since Linux 4.7,
566 .\" commit bf959931ddb88c4e4366e96dd22e68fa0db9527c
567 .\" prevents cases where an unreapable zombie is created if
568 .\" /sbin/init doesn't use __WALL.
569 the
570 .B __WALL
571 flag is automatically implied if the child is being ptraced.
572 .SS C library/kernel differences
573 .BR wait ()
574 is actually a library function that (in glibc) is implemented as a call to
575 .BR wait4 (2).
576 .PP
577 On some architectures, there is no
578 .BR waitpid ()
579 system call;
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
583 .BR wait4 (2).
584 .PP
585 The raw
586 .BR waitid ()
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
592 .BR wait4 (2).
593 See
594 .BR getrusage (2)
595 for details.
596 .SH BUGS
597 According to POSIX.1-2008, an application calling
598 .BR waitid ()
599 must ensure that
600 .I infop
601 points to a
602 .I siginfo_t
603 structure (i.e., that it is a non-null pointer).
604 On Linux, if
605 .I infop
606 is NULL,
607 .BR waitid ()
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.
611 .SH EXAMPLES
612 .\" fork.2 refers to this example program.
613 The following program demonstrates the use of
614 .BR fork (2)
615 and
616 .BR waitpid ().
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
620 .BR pause (2),
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
626 .BR waitpid (),
627 and uses the W*() macros described above to analyze the wait status value.
628 .PP
629 The following shell session demonstrates the use of the program:
630 .PP
631 .in +4n
632 .EX
633 .RB "$" " ./a.out &"
634 Child PID is 32360
635 [1] 32359
636 .RB "$" " kill \-STOP 32360"
637 stopped by signal 19
638 .RB "$" " kill \-CONT 32360"
639 continued
640 .RB "$" " kill \-TERM 32360"
641 killed by signal 15
642 [1]+ Done ./a.out
643 $
644 .EE
645 .in
646 .SS Program source
647 \&
648 .\" SRC BEGIN (wait.c)
649 .EX
650 #include <stdint.h>
651 #include <stdio.h>
652 #include <stdlib.h>
653 #include <sys/wait.h>
654 #include <unistd.h>
655
656 int
657 main(int argc, char *argv[])
658 {
659 pid_t cpid, w;
660 int wstatus;
661
662 cpid = fork();
663 if (cpid == \-1) {
664 perror("fork");
665 exit(EXIT_FAILURE);
666 }
667
668 if (cpid == 0) { /* Code executed by child */
669 printf("Child PID is %jd\en", (intmax_t) getpid());
670 if (argc == 1)
671 pause(); /* Wait for signals */
672 _exit(atoi(argv[1]));
673
674 } else { /* Code executed by parent */
675 do {
676 w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
677 if (w == \-1) {
678 perror("waitpid");
679 exit(EXIT_FAILURE);
680 }
681
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");
690 }
691 } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
692 exit(EXIT_SUCCESS);
693 }
694 }
695 .EE
696 .\" SRC END
697 .SH SEE ALSO
698 .BR _exit (2),
699 .BR clone (2),
700 .BR fork (2),
701 .BR kill (2),
702 .BR ptrace (2),
703 .BR sigaction (2),
704 .BR signal (2),
705 .BR wait4 (2),
706 .BR pthread_create (3),
707 .BR core (5),
708 .BR credentials (7),
709 .BR signal (7)