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