OSDN Git Service

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