OSDN Git Service

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