readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man2 / wait.2
blobb6cb326c090ee6babe606fe59d1c83e4b9d4af03
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 2021-03-22 "Linux" "Linux Programmer's Manual"
50 .SH NAME
51 wait, waitpid, waitid \- wait for process to change state
52 .SH SYNOPSIS
53 .nf
54 .B #include <sys/wait.h>
55 .PP
56 .BI "pid_t wait(int *" "wstatus" );
57 .BI "pid_t waitpid(pid_t " pid ", int *" wstatus ", int " options );
58 .PP
59 .BI "int waitid(idtype_t " idtype ", id_t " id \
60 ", siginfo_t *" infop ", int " options );
61                 /* This is the glibc and POSIX interface; see
62                    NOTES for information on the raw system call. */
63 .fi
64 .PP
65 .RS -4
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
68 .RE
69 .PP
70 .BR waitid ():
71 .nf
72     Since glibc 2.26:
73         _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L
74 .\"    (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
75     Glibc 2.25 and earlier:
76         _XOPEN_SOURCE
77             || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
78             || /* Glibc <= 2.19: */ _BSD_SOURCE
79 .fi
80 .SH DESCRIPTION
81 All of these system calls are used to wait for state changes
82 in a child of the calling process, and obtain information
83 about the child whose state has changed.
84 A state change is considered to be: the child terminated;
85 the child was stopped by a signal; or the child was resumed by a signal.
86 In the case of a terminated child, performing a wait allows
87 the system to release the resources associated with the child;
88 if a wait is not performed, then the terminated child remains in
89 a "zombie" state (see NOTES below).
90 .PP
91 If a child has already changed state, then these calls return immediately.
92 Otherwise, they block until either a child changes state or
93 a signal handler interrupts the call (assuming that system calls
94 are not automatically restarted using the
95 .B SA_RESTART
96 flag of
97 .BR sigaction (2)).
98 In the remainder of this page, a child whose state has changed
99 and which has not yet been waited upon by one of these system
100 calls is termed
101 .IR waitable .
102 .SS wait() and waitpid()
104 .BR wait ()
105 system call suspends execution of the calling thread until one of its
106 children terminates.
107 The call
108 .I wait(&wstatus)
109 is equivalent to:
111 .in +4n
113 waitpid(\-1, &wstatus, 0);
118 .BR waitpid ()
119 system call suspends execution of the calling thread until a
120 child specified by
121 .I pid
122 argument has changed state.
123 By default,
124 .BR waitpid ()
125 waits only for terminated children, but this behavior is modifiable
126 via the
127 .I options
128 argument, as described below.
130 The value of
131 .I pid
132 can be:
133 .IP "< \-1"
134 meaning wait for any child process whose process group ID is
135 equal to the absolute value of
136 .IR pid .
137 .IP \-1
138 meaning wait for any child process.
139 .IP 0
140 meaning wait for any child process whose process group ID is
141 equal to that of the calling process at the time of the call to
142 .BR waitpid ().
143 .IP "> 0"
144 meaning wait for the child whose process ID is equal to the
145 value of
146 .IR pid .
148 The value of
149 .I options
150 is an OR of zero or more of the following constants:
152 .B WNOHANG
153 return immediately if no child has exited.
155 .B WUNTRACED
156 also return if a child has stopped
157 (but not traced via
158 .BR ptrace (2)).
159 Status for
160 .I traced
161 children which have stopped is provided
162 even if this option is not specified.
164 .BR WCONTINUED " (since Linux 2.6.10)"
165 also return if a stopped child has been resumed by delivery of
166 .BR SIGCONT .
168 (For Linux-only options, see below.)
171 .I wstatus
172 is not NULL,
173 .BR wait ()
175 .BR waitpid ()
176 store status information in the \fIint\fP to which it points.
177 This integer can be inspected with the following macros (which
178 take the integer itself as an argument, not a pointer to it,
179 as is done in
180 .BR wait ()
182 .BR waitpid ()!):
184 .BI WIFEXITED( wstatus )
185 returns true if the child terminated normally, that is,
186 by calling
187 .BR exit (3)
189 .BR _exit (2),
190 or by returning from main().
192 .BI WEXITSTATUS( wstatus )
193 returns the exit status of the child.
194 This consists of the least significant 8 bits of the
195 .I status
196 argument that the child specified in a call to
197 .BR exit (3)
199 .BR _exit (2)
200 or as the argument for a return statement in main().
201 This macro should be employed only if
202 .B WIFEXITED
203 returned true.
205 .BI WIFSIGNALED( wstatus )
206 returns true if the child process was terminated by a signal.
208 .BI WTERMSIG( wstatus )
209 returns the number of the signal that caused the child process to
210 terminate.
211 This macro should be employed only if
212 .B WIFSIGNALED
213 returned true.
215 .BI WCOREDUMP( wstatus )
216 returns true if the child produced a core dump (see
217 .BR core (5)).
218 This macro should be employed only if
219 .B WIFSIGNALED
220 returned true.
222 This macro is not specified in POSIX.1-2001 and is not available on
223 some UNIX implementations (e.g., AIX, SunOS).
224 Therefore, enclose its use inside
225 .IR "#ifdef WCOREDUMP ... #endif" .
227 .BI WIFSTOPPED( wstatus )
228 returns true if the child process was stopped by delivery of a signal;
229 this is possible only if the call was done using
230 .B WUNTRACED
231 or when the child is being traced (see
232 .BR ptrace (2)).
234 .BI WSTOPSIG( wstatus )
235 returns the number of the signal which caused the child to stop.
236 This macro should be employed only if
237 .B WIFSTOPPED
238 returned true.
240 .BI WIFCONTINUED( wstatus )
241 (since Linux 2.6.10)
242 returns true if the child process was resumed by delivery of
243 .BR SIGCONT .
244 .SS waitid()
246 .BR waitid ()
247 system call (available since Linux 2.6.9) provides more precise
248 control over which child state changes to wait for.
251 .I idtype
253 .I id
254 arguments select the child(ren) to wait for, as follows:
255 .IP "\fIidtype\fP == \fBP_PID\fP"
256 Wait for the child whose process ID matches
257 .IR id .
258 .IP "\fIidtype\fP == \fBP_PIDFD\fP (since Linux 5.4)"
259 .\" commit 3695eae5fee0605f316fbaad0b9e3de791d7dfaf
260 Wait for the child referred to by the PID file descriptor specified in
261 .IR id .
262 (See
263 .BR pidfd_open (2)
264 for further information on PID file descriptors.)
265 .IP "\fIidtype\fP == \fBP_PGID\fP"
266 Wait for any child whose process group ID matches
267 .IR id .
268 Since Linux 5.4,
269 .\" commit 821cc7b0b205c0df64cce59aacc330af251fa8f7
271 .I id
272 is zero, then wait for any child that is in the same process group
273 as the caller's process group at the time of the call.
274 .IP "\fIidtype\fP == \fBP_ALL\fP"
275 Wait for any child;
276 .I id
277 is ignored.
279 The child state changes to wait for are specified by ORing
280 one or more of the following flags in
281 .IR options :
283 .B WEXITED
284 Wait for children that have terminated.
286 .B WSTOPPED
287 Wait for children that have been stopped by delivery of a signal.
289 .B WCONTINUED
290 Wait for (previously stopped) children that have been
291 resumed by delivery of
292 .BR SIGCONT .
294 The following flags may additionally be ORed in
295 .IR options :
297 .B WNOHANG
298 As for
299 .BR waitpid ().
301 .B WNOWAIT
302 Leave the child in a waitable state; a later wait call
303 can be used to again retrieve the child status information.
305 Upon successful return,
306 .BR waitid ()
307 fills in the following fields of the
308 .I siginfo_t
309 structure pointed to by
310 .IR infop :
312 \fIsi_pid\fP
313 The process ID of the child.
315 \fIsi_uid\fP
316 The real user ID of the child.
317 (This field is not set on most other implementations.)
319 \fIsi_signo\fP
320 Always set to
321 .BR SIGCHLD .
323 \fIsi_status\fP
324 Either the exit status of the child, as given to
325 .BR _exit (2)
327 .BR exit (3)),
328 or the signal that caused the child to terminate, stop, or continue.
330 .I si_code
331 field can be used to determine how to interpret this field.
333 \fIsi_code\fP
334 Set to one of:
335 .B CLD_EXITED
336 (child called
337 .BR _exit (2));
338 .B CLD_KILLED
339 (child killed by signal);
340 .B CLD_DUMPED
341 (child killed by signal, and dumped core);
342 .B CLD_STOPPED
343 (child stopped by signal);
344 .B CLD_TRAPPED
345 (traced child has trapped); or
346 .B CLD_CONTINUED
347 (child continued by
348 .BR SIGCONT ).
351 .B WNOHANG
352 was specified in
353 .I options
354 and there were no children in a waitable state, then
355 .BR waitid ()
356 returns 0 immediately and
357 the state of the
358 .I siginfo_t
359 structure pointed to by
360 .I infop
361 depends on the implementation.
362 To (portably) distinguish this case from that where a child was in a
363 waitable state, zero out the
364 .I si_pid
365 field before the call and check for a nonzero value in this field
366 after the call returns.
368 POSIX.1-2008 Technical Corrigendum 1 (2013) adds the requirement that when
369 .B WNOHANG
370 is specified in
371 .I options
372 and there were no children in a waitable state, then
373 .BR waitid ()
374 should zero out the
375 .I si_pid
377 .I si_signo
378 fields of the structure.
379 On Linux and other implementations that adhere to this requirement,
380 it is not necessary to zero out the
381 .I si_pid
382 field before calling
383 .BR waitid ().
384 However,
385 not all implementations follow the POSIX.1 specification on this point.
386 .\" POSIX.1-2001 leaves this possibility unspecified; most
387 .\" implementations (including Linux) zero out the structure
388 .\" in this case, but at least one implementation (AIX 5.1)
389 .\" does not -- MTK Nov 04
390 .SH RETURN VALUE
391 .BR wait ():
392 on success, returns the process ID of the terminated child;
393 on failure, \-1 is returned.
395 .BR waitpid ():
396 on success, returns the process ID of the child whose state has changed;
398 .B WNOHANG
399 was specified and one or more child(ren) specified by
400 .I pid
401 exist, but have not yet changed state, then 0 is returned.
402 On failure, \-1 is returned.
404 .BR waitid ():
405 returns 0 on success or
407 .B WNOHANG
408 was specified and no child(ren) specified by
409 .I id
410 has yet changed state;
411 on failure, \-1 is returned.
412 .\" FIXME As reported by Vegard Nossum, if infop is NULL, then waitid()
413 .\" returns the PID of the child.  Either this is a bug, or it is intended
414 .\" behavior that needs to be documented.  See my Jan 2009 LKML mail
415 .\" "waitid() return value strangeness when infop is NULL".
417 On failure, each of these calls sets
418 .I errno
419 to indicate the error.
420 .SH ERRORS
422 .B ECHILD
423 (for
424 .BR wait ())
425 The calling process does not have any unwaited-for children.
427 .B ECHILD
428 (for
429 .BR waitpid ()
431 .BR waitid ())
432 The process specified by
433 .I pid
434 .RB ( waitpid ())
436 .I idtype
438 .I id
439 .RB ( waitid ())
440 does not exist or is not a child of the calling process.
441 (This can happen for one's own child if the action for
442 .B SIGCHLD
443 is set to
444 .BR SIG_IGN .
445 See also the \fILinux Notes\fP section about threads.)
447 .B EINTR
448 .B WNOHANG
449 was not set and an unblocked signal or a
450 .B SIGCHLD
451 was caught; see
452 .BR signal (7).
454 .B EINVAL
456 .I options
457 argument was invalid.
459 .B ESRCH
460 (for
461 .BR wait ()
463 .BR waitpid ())
464 .I pid
465 is equal to
466 .BR INT_MIN .
467 .SH CONFORMING TO
468 SVr4, 4.3BSD, POSIX.1-2001.
469 .SH NOTES
470 A child that terminates, but has not been waited for becomes a "zombie".
471 The kernel maintains a minimal set of information about the zombie
472 process (PID, termination status, resource usage information)
473 in order to allow the parent to later perform a wait to obtain
474 information about the child.
475 As long as a zombie is not removed from the system via a wait,
476 it will consume a slot in the kernel process table, and if
477 this table fills, it will not be possible to create further processes.
478 If a parent process terminates, then its "zombie" children (if any)
479 are adopted by
480 .BR init (1),
481 (or by the nearest "subreaper" process as defined through the use of the
482 .BR prctl (2)
483 .B PR_SET_CHILD_SUBREAPER
484 operation);
485 .BR init (1)
486 automatically performs a wait to remove the zombies.
488 POSIX.1-2001 specifies that if the disposition of
489 .B SIGCHLD
490 is set to
491 .B SIG_IGN
492 or the
493 .B SA_NOCLDWAIT
494 flag is set for
495 .B SIGCHLD
496 (see
497 .BR sigaction (2)),
498 then children that terminate do not become zombies and a call to
499 .BR wait ()
501 .BR waitpid ()
502 will block until all children have terminated, and then fail with
503 .I errno
504 set to
505 .BR ECHILD .
506 (The original POSIX standard left the behavior of setting
507 .B SIGCHLD
509 .B SIG_IGN
510 unspecified.
511 Note that even though the default disposition of
512 .B SIGCHLD
513 is "ignore", explicitly setting the disposition to
514 .B SIG_IGN
515 results in different treatment of zombie process children.)
517 Linux 2.6 conforms to the POSIX requirements.
518 However, Linux 2.4 (and earlier) does not:
519 if a
520 .BR wait ()
522 .BR waitpid ()
523 call is made while
524 .B SIGCHLD
525 is being ignored, the call behaves just as though
526 .B SIGCHLD
527 were not being ignored, that is, the call blocks until the next child
528 terminates and then returns the process ID and status of that child.
529 .SS Linux notes
530 In the Linux kernel, a kernel-scheduled thread is not a distinct
531 construct from a process.
532 Instead, a thread is simply a process
533 that is created using the Linux-unique
534 .BR clone (2)
535 system call; other routines such as the portable
536 .BR pthread_create (3)
537 call are implemented using
538 .BR clone (2).
539 Before Linux 2.4, a thread was just a special case of a process,
540 and as a consequence one thread could not wait on the children
541 of another thread, even when the latter belongs to the same thread group.
542 However, POSIX prescribes such functionality, and since Linux 2.4
543 a thread can, and by default will, wait on children of other threads
544 in the same thread group.
546 The following Linux-specific
547 .I options
548 are for use with children created using
549 .BR clone (2);
550 they can also, since Linux 4.7,
551 .\" commit 91c4e8ea8f05916df0c8a6f383508ac7c9e10dba
552 be used with
553 .BR waitid ():
555 .B __WCLONE
556 .\" since 0.99pl10
557 Wait for "clone" children only.
558 If omitted, then wait for "non-clone" children only.
559 (A "clone" child is one which delivers no signal, or a signal other than
560 .B SIGCHLD
561 to its parent upon termination.)
562 This option is ignored if
563 .B __WALL
564 is also specified.
566 .BR __WALL " (since Linux 2.4)"
567 .\" since patch-2.3.48
568 Wait for all children, regardless of
569 type ("clone" or "non-clone").
571 .BR __WNOTHREAD " (since Linux 2.4)"
572 .\" since patch-2.4.0-test8
573 Do not wait for children of other threads in
574 the same thread group.
575 This was the default before Linux 2.4.
577 Since Linux 4.7,
578 .\" commit bf959931ddb88c4e4366e96dd22e68fa0db9527c
579 .\" prevents cases where an unreapable zombie is created if
580 .\" /sbin/init doesn't use __WALL.
582 .B __WALL
583 flag is automatically implied if the child is being ptraced.
584 .SS C library/kernel differences
585 .BR wait ()
586 is actually a library function that (in glibc) is implemented as a call to
587 .BR wait4 (2).
589 On some architectures, there is no
590 .BR waitpid ()
591 system call;
592 .\" e.g., i386 has the system call, but not x86-64
593 instead, this interface is implemented via a C library
594 wrapper function that calls
595 .BR wait4 (2).
597 The raw
598 .BR waitid ()
599 system call takes a fifth argument, of type
600 .IR "struct rusage\ *" .
601 If this argument is non-NULL,
602 then it is used to return resource usage information about the child,
603 in the same manner as
604 .BR wait4 (2).
606 .BR getrusage (2)
607 for details.
608 .SH BUGS
609 According to POSIX.1-2008, an application calling
610 .BR waitid ()
611 must ensure that
612 .I infop
613 points to a
614 .I siginfo_t
615 structure (i.e., that it is a non-null pointer).
616 On Linux, if
617 .I infop
618 is NULL,
619 .BR waitid ()
620 succeeds, and returns the process ID of the waited-for child.
621 Applications should avoid relying on this inconsistent,
622 nonstandard, and unnecessary feature.
623 .SH EXAMPLES
624 .\" fork.2 refers to this example program.
625 The following program demonstrates the use of
626 .BR fork (2)
628 .BR waitpid ().
629 The program creates a child process.
630 If no command-line argument is supplied to the program,
631 then the child suspends its execution using
632 .BR pause (2),
633 to allow the user to send signals to the child.
634 Otherwise, if a command-line argument is supplied,
635 then the child exits immediately,
636 using the integer supplied on the command line as the exit status.
637 The parent process executes a loop that monitors the child using
638 .BR waitpid (),
639 and uses the W*() macros described above to analyze the wait status value.
641 The following shell session demonstrates the use of the program:
643 .in +4n
645 .RB "$" " ./a.out &"
646 Child PID is 32360
647 [1] 32359
648 .RB "$" " kill \-STOP 32360"
649 stopped by signal 19
650 .RB "$" " kill \-CONT 32360"
651 continued
652 .RB "$" " kill \-TERM 32360"
653 killed by signal 15
654 [1]+  Done                    ./a.out
658 .SS Program source
661 #include <sys/wait.h>
662 #include <stdint.h>
663 #include <stdlib.h>
664 #include <unistd.h>
665 #include <stdio.h>
668 main(int argc, char *argv[])
670     pid_t cpid, w;
671     int wstatus;
673     cpid = fork();
674     if (cpid == \-1) {
675         perror("fork");
676         exit(EXIT_FAILURE);
677     }
679     if (cpid == 0) {            /* Code executed by child */
680         printf("Child PID is %jd\en", (intmax_t) getpid());
681         if (argc == 1)
682             pause();                    /* Wait for signals */
683         _exit(atoi(argv[1]));
685     } else {                    /* Code executed by parent */
686         do {
687             w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
688             if (w == \-1) {
689                 perror("waitpid");
690                 exit(EXIT_FAILURE);
691             }
693             if (WIFEXITED(wstatus)) {
694                 printf("exited, status=%d\en", WEXITSTATUS(wstatus));
695             } else if (WIFSIGNALED(wstatus)) {
696                 printf("killed by signal %d\en", WTERMSIG(wstatus));
697             } else if (WIFSTOPPED(wstatus)) {
698                 printf("stopped by signal %d\en", WSTOPSIG(wstatus));
699             } else if (WIFCONTINUED(wstatus)) {
700                 printf("continued\en");
701             }
702         } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
703         exit(EXIT_SUCCESS);
704     }
707 .SH SEE ALSO
708 .BR _exit (2),
709 .BR clone (2),
710 .BR fork (2),
711 .BR kill (2),
712 .BR ptrace (2),
713 .BR sigaction (2),
714 .BR signal (2),
715 .BR wait4 (2),
716 .BR pthread_create (3),
717 .BR core (5),
718 .BR credentials (7),
719 .BR signal (7)