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