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