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