]>
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() |
fea681da | 48 | .\" |
cc4615cc | 49 | .TH WAIT 2 2007-07-26 "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 | |
69 | .BR waitid (): | |
70 | _SVID_SOURCE || _XOPEN_SOURCE | |
fea681da | 71 | .SH DESCRIPTION |
f2351505 MK |
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 | |
c13182ef | 90 | and which has not yet been waited upon by one of these system |
d3b2ef5d | 91 | calls is termed |
f2351505 MK |
92 | .IR waitable . |
93 | .SS "wait() and waitpid()" | |
fea681da | 94 | The |
f2351505 MK |
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 | ||
2bc2f479 | 103 | waitpid(\-1, &status, 0); |
f2351505 | 104 | .fi |
fea681da MK |
105 | |
106 | The | |
f2351505 MK |
107 | .BR waitpid () |
108 | system call suspends execution of the current process until a | |
109 | child specified by | |
fea681da | 110 | .I pid |
f2351505 MK |
111 | argument has changed state. |
112 | By default, | |
113 | .BR waitpid () | |
d9bfdb9c | 114 | waits only for terminated children, but this behavior is modifiable |
f2351505 MK |
115 | via the |
116 | .I options | |
117 | argument, as described below. | |
fea681da MK |
118 | |
119 | The value of | |
120 | .I pid | |
f2351505 | 121 | can be: |
fea681da | 122 | .IP "< \-1" |
f2351505 | 123 | meaning wait for any child process whose process group ID is |
fea681da MK |
124 | equal to the absolute value of |
125 | .IR pid . | |
126 | .IP \-1 | |
f2351505 | 127 | meaning wait for any child process. |
fea681da | 128 | .IP 0 |
f2351505 | 129 | meaning wait for any child process whose process group ID is |
fea681da MK |
130 | equal to that of the calling process. |
131 | .IP "> 0" | |
f2351505 | 132 | meaning wait for the child whose process ID is equal to the |
fea681da MK |
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: | |
a325cc60 | 139 | .TP 12 |
fea681da | 140 | .B WNOHANG |
f2351505 | 141 | return immediately if no child has exited. |
fea681da MK |
142 | .TP |
143 | .B WUNTRACED | |
d3b2ef5d | 144 | also return if a child has stopped |
f2351505 | 145 | (but not traced via |
d3b2ef5d MK |
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. | |
f2351505 MK |
151 | .TP |
152 | .B WCONTINUED | |
153 | (Since Linux 2.6.10) | |
d3b2ef5d | 154 | also return if a stopped child has been resumed by delivery of |
f2351505 | 155 | .BR SIGCONT . |
fea681da MK |
156 | .PP |
157 | (For Linux-only options, see below.) | |
158 | .PP | |
c13182ef | 159 | The |
f2351505 MK |
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 | |
fea681da MK |
170 | If |
171 | .I status | |
8478ee02 | 172 | is not NULL, |
f2351505 MK |
173 | .BR wait () |
174 | and | |
175 | .BR waitpid () | |
a8d55537 | 176 | store status information in the \fIint\fP to which it points. |
f2351505 MK |
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 ()!): | |
fea681da MK |
183 | .TP |
184 | .BI WIFEXITED( status ) | |
185 | returns true if the child terminated normally, that is, | |
f2351505 MK |
186 | by calling |
187 | .BR exit (3) | |
188 | or | |
a5e0a0e4 | 189 | .BR _exit (2), |
f2351505 | 190 | or by returning from main(). |
fea681da MK |
191 | .TP |
192 | .BI WEXITSTATUS( status ) | |
f2351505 MK |
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 | |
0bfa087b | 197 | .BR exit (3) |
f2351505 | 198 | or |
0bfa087b | 199 | .BR _exit (2) |
f2351505 MK |
200 | or as the argument for a return statement in main(). |
201 | This macro should only be employed if | |
fea681da MK |
202 | .B WIFEXITED |
203 | returned true. | |
204 | .TP | |
205 | .BI WIFSIGNALED( status ) | |
f2351505 | 206 | returns true if the child process was terminated by a signal. |
fea681da MK |
207 | .TP |
208 | .BI WTERMSIG( status ) | |
209 | returns the number of the signal that caused the child process to | |
c13182ef MK |
210 | terminate. |
211 | This macro should only be employed if | |
f2351505 MK |
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 | |
fea681da | 218 | .B WIFSIGNALED |
f2351505 MK |
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. | |
fea681da MK |
223 | .TP |
224 | .BI WIFSTOPPED( status ) | |
f2351505 MK |
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 | |
0daa9e92 | 227 | .B WUNTRACED |
fea681da MK |
228 | or when the child is being traced (see |
229 | .BR ptrace (2)). | |
230 | .TP | |
231 | .BI WSTOPSIG( status ) | |
c13182ef MK |
232 | returns the number of the signal which caused the child to stop. |
233 | This macro should only be employed if | |
fea681da | 234 | .B WIFSTOPPED |
f2351505 MK |
235 | returned true. |
236 | .TP | |
237 | .BI WIFCONTINUED( status ) | |
238 | (Since Linux 2.6.10) | |
f2351505 MK |
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 : | |
a325cc60 | 266 | .TP 12 |
f2351505 MK |
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 : | |
a325cc60 | 280 | .TP 12 |
f2351505 MK |
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 : | |
a325cc60 MK |
295 | .TP 12 |
296 | \fIsi_pid\fP | |
f2351505 | 297 | The process ID of the child. |
a325cc60 MK |
298 | .TP |
299 | \fIsi_uid\fP | |
f2351505 | 300 | The real user ID of the child. |
04d6ea6b | 301 | (This field is not set on most other implementations.) |
a325cc60 MK |
302 | .TP |
303 | \fIsi_signo\fP | |
f2351505 MK |
304 | Always set to |
305 | .BR SIGCHLD . | |
a325cc60 MK |
306 | .TP |
307 | \fIsi_status\fP | |
f2351505 MK |
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. | |
c13182ef | 313 | The |
f2351505 MK |
314 | .I si_code |
315 | field can be used to determine how to interpret this field. | |
a325cc60 MK |
316 | .TP |
317 | \fIsi_code\fP | |
f2351505 MK |
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_STOPPED | |
325 | (child stopped by signal); or | |
326 | .B CLD_CONTINUED | |
327 | (child continued by | |
328 | .BR SIGCONT ). | |
329 | .PP | |
330 | If | |
331 | .B WNOHANG | |
332 | was specified in | |
333 | .I options | |
334 | and there were no children in a waitable state, then | |
335 | .BR waitid () | |
336 | returns 0 immediately and | |
337 | the state of the | |
338 | .I siginfo_t | |
339 | structure pointed to by | |
340 | .I infop | |
341 | is unspecified. | |
c13182ef | 342 | .\" POSIX.1-2001 leaves this possibility unspecified; most |
f2351505 MK |
343 | .\" implementations (including Linux) zero out the structure |
344 | .\" in this case, but at at least one implementation (AIX 5.1) | |
345 | .\" does not -- MTK Nov 04 | |
346 | To distinguish this case from that where a child was in a | |
347 | waitable state, zero out the | |
348 | .I si_pid | |
349 | field before the call and check for a non-zero value in this field | |
350 | after the call returns. | |
fea681da | 351 | .SH "RETURN VALUE" |
f2351505 | 352 | .BR wait (): |
d3b2ef5d | 353 | on success, returns the process ID of the terminated child; |
f2351505 MK |
354 | on error, \-1 is returned. |
355 | ||
a5e0a0e4 | 356 | .BR waitpid (): |
d3b2ef5d | 357 | on success, returns the process ID of the child whose state has changed; |
f2351505 MK |
358 | on error, \-1 is returned; |
359 | if | |
fea681da | 360 | .B WNOHANG |
f2351505 MK |
361 | was specified and no child(ren) specified by |
362 | .I pid | |
363 | has yet changed state, then 0 is returned. | |
364 | ||
a5e0a0e4 | 365 | .BR waitid (): |
c13182ef | 366 | returns 0 on success or |
f2351505 MK |
367 | if |
368 | .B WNOHANG | |
369 | was specified and no child(ren) specified by | |
370 | .I id | |
371 | has yet changed state; | |
372 | on error, \-1 is returned. | |
373 | ||
374 | Each of these calls sets | |
fea681da | 375 | .I errno |
f2351505 | 376 | to an appropriate value in the case of an error. |
fea681da MK |
377 | .SH ERRORS |
378 | .TP | |
0daa9e92 | 379 | .B ECHILD |
c13182ef | 380 | (for |
e1d6264d | 381 | .BR wait ()) |
fea681da MK |
382 | The calling process does not have any unwaited-for children. |
383 | .TP | |
0daa9e92 | 384 | .B ECHILD |
c13182ef MK |
385 | (for |
386 | .BR waitpid () | |
387 | or | |
e1d6264d | 388 | .BR waitid ()) |
f2351505 | 389 | The process specified by |
fea681da | 390 | .I pid |
f2351505 MK |
391 | .RB ( waitpid ()) |
392 | or | |
393 | .I idtype | |
394 | and | |
395 | .I id | |
396 | .RB ( waitid ()) | |
fea681da | 397 | does not exist or is not a child of the calling process. |
8bd58774 MK |
398 | (This can happen for one's own child if the action for |
399 | .B SIGCHLD | |
400 | is set to | |
401 | .BR SIG_IGN . | |
4fb31341 | 402 | See also the \fILinux Notes\fP section about threads.) |
fea681da MK |
403 | .TP |
404 | .B EINTR | |
405 | .B WNOHANG | |
406 | was not set and an unblocked signal or a | |
407 | .B SIGCHLD | |
408 | was caught. | |
409 | .TP | |
410 | .B EINVAL | |
411 | The | |
412 | .I options | |
413 | argument was invalid. | |
2dd578fd MK |
414 | .SH "CONFORMING TO" |
415 | SVr4, 4.3BSD, POSIX.1-2001. | |
fea681da | 416 | .SH NOTES |
f2351505 MK |
417 | A child that terminates, but has not been waited for becomes a "zombie". |
418 | The kernel maintains a minimal set of information about the zombie | |
419 | process (PID, termination status, resource usage information) | |
420 | in order to allow the parent to later perform a wait to obtain | |
421 | information about the child. | |
422 | As long as a zombie is not removed from the system via a wait, | |
423 | it will consume a slot in the kernel process table, and if | |
424 | this table fills, it will not be possible to create further processes. | |
425 | If a parent process terminates, then its "zombie" children (if any) | |
426 | are adopted by | |
427 | .BR init (8), | |
428 | which automatically performs a wait to remove the zombies. | |
429 | ||
430 | POSIX.1-2001 specifies that if the disposition of | |
431 | .B SIGCHLD | |
c13182ef | 432 | is set to |
f2351505 | 433 | .B SIG_IGN |
c13182ef | 434 | or the |
f2351505 MK |
435 | .B SA_NOCLDWAIT |
436 | flag is set for | |
0daa9e92 | 437 | .B SIGCHLD |
c13182ef | 438 | (see |
f2351505 MK |
439 | .BR sigaction (2)), |
440 | then children that terminate do not become zombies and a call to | |
fea681da MK |
441 | .BR wait () |
442 | or | |
443 | .BR waitpid () | |
f2351505 | 444 | will block until all children have terminated, and then fail with |
fea681da | 445 | .I errno |
f2351505 MK |
446 | set to |
447 | .BR ECHILD . | |
d9bfdb9c | 448 | (The original POSIX standard left the behavior of setting |
f2351505 MK |
449 | .B SIGCHLD |
450 | to | |
451 | .B SIG_IGN | |
452 | unspecified.) | |
453 | Linux 2.6 conforms to this specification. | |
454 | However, Linux 2.4 (and earlier) does not: | |
fea681da | 455 | if a |
c13182ef MK |
456 | .BR wait () |
457 | or | |
e1d6264d | 458 | .BR waitpid () |
f2351505 MK |
459 | call is made while |
460 | .B SIGCHLD | |
461 | is being ignored, the call behaves just as though | |
462 | .B SIGCHLD | |
704a18f0 | 463 | were not being ignored, that is, the call blocks until the next child |
d3b2ef5d | 464 | terminates and then returns the process ID and status of that child. |
4fb31341 | 465 | .SS Linux Notes |
fea681da | 466 | In the Linux kernel, a kernel-scheduled thread is not a distinct |
c13182ef MK |
467 | construct from a process. |
468 | Instead, a thread is simply a process | |
fea681da MK |
469 | that is created using the Linux-unique |
470 | .BR clone (2) | |
471 | system call; other routines such as the portable | |
472 | .BR pthread_create (3) | |
473 | call are implemented using | |
474 | .BR clone (2). | |
475 | Before Linux 2.4, a thread was just a special case of a process, | |
476 | and as a consequence one thread could not wait on the children | |
477 | of another thread, even when the latter belongs to the same thread group. | |
478 | However, POSIX prescribes such functionality, and since Linux 2.4 | |
479 | a thread can, and by default will, wait on children of other threads | |
480 | in the same thread group. | |
481 | .LP | |
8382f16d | 482 | The following Linux-specific |
fea681da MK |
483 | .I options |
484 | are for use with children created using | |
948fb4ed MK |
485 | .BR clone (2); |
486 | they cannot be used with | |
487 | .BR waitid (): | |
fea681da MK |
488 | .TP |
489 | .B __WCLONE | |
490 | .\" since 0.99pl10 | |
c13182ef MK |
491 | Wait for "clone" children only. |
492 | If omitted then wait for "non-clone" children only. | |
493 | (A "clone" child is one which delivers no signal, or a signal other than | |
fea681da MK |
494 | .B SIGCHLD |
495 | to its parent upon termination.) | |
496 | This option is ignored if | |
497 | .B __WALL | |
498 | is also specified. | |
499 | .TP | |
500 | .B __WALL | |
501 | .\" since patch-2.3.48 | |
502 | (Since Linux 2.4) Wait for all children, regardless of | |
503 | type ("clone" or "non-clone"). | |
504 | .TP | |
505 | .B __WNOTHREAD | |
506 | .\" since patch-2.4.0-test8 | |
507 | (Since Linux 2.4) Do not wait for children of other threads in | |
c13182ef MK |
508 | the same thread group. |
509 | This was the default before Linux 2.4. | |
1fa343d1 | 510 | .SH EXAMPLE |
cde9f44b | 511 | .\" fork.2 refers to this example program. |
c13182ef | 512 | The following program demonstrates the use of |
19dbfd0a | 513 | .BR fork (2) |
c13182ef | 514 | and |
2777b1ca | 515 | .BR waitpid (). |
1fa343d1 | 516 | The program creates a child process. |
c13182ef MK |
517 | If no command-line argument is supplied to the program, |
518 | then the child suspends its execution using | |
1fa343d1 MK |
519 | .BR pause (2), |
520 | to allow the user to send signals to the child. | |
521 | Otherwise, if a command-line argument is supplied, | |
c13182ef | 522 | then the child exits immediately, |
1fa343d1 MK |
523 | using the integer supplied on the command line as the exit status. |
524 | The parent process executes a loop that monitors the child using | |
2777b1ca | 525 | .BR waitpid (), |
d9bfdb9c | 526 | and uses the W*() macros described above to analyze the wait status value. |
1fa343d1 MK |
527 | |
528 | The following shell session demonstrates the use of the program: | |
529 | .nf | |
530 | ||
531 | $ ./a.out & | |
532 | Child PID is 32360 | |
533 | [1] 32359 | |
29059a65 | 534 | $ kill \-STOP 32360 |
1fa343d1 | 535 | stopped by signal 19 |
29059a65 | 536 | $ kill \-CONT 32360 |
1fa343d1 | 537 | continued |
29059a65 | 538 | $ kill \-TERM 32360 |
1fa343d1 MK |
539 | killed by signal 15 |
540 | [1]+ Done ./a.out | |
541 | $ | |
542 | ||
1fa343d1 MK |
543 | #include <sys/wait.h> |
544 | #include <stdlib.h> | |
545 | #include <unistd.h> | |
546 | #include <stdio.h> | |
547 | ||
548 | int | |
549 | main(int argc, char *argv[]) | |
550 | { | |
551 | pid_t cpid, w; | |
552 | int status; | |
553 | ||
554 | cpid = fork(); | |
29059a65 | 555 | if (cpid == \-1) { |
45949175 MK |
556 | perror("fork"); |
557 | exit(EXIT_FAILURE); | |
558 | } | |
1fa343d1 MK |
559 | |
560 | if (cpid == 0) { /* Code executed by child */ | |
561 | printf("Child PID is %ld\\n", (long) getpid()); | |
562 | if (argc == 1) | |
563 | pause(); /* Wait for signals */ | |
564 | _exit(atoi(argv[1])); | |
565 | ||
566 | } else { /* Code executed by parent */ | |
567 | do { | |
568 | w = waitpid(cpid, &status, WUNTRACED | WCONTINUED); | |
29059a65 | 569 | if (w == \-1) { |
45949175 MK |
570 | perror("waitpid"); |
571 | exit(EXIT_FAILURE); | |
572 | } | |
1fa343d1 MK |
573 | |
574 | if (WIFEXITED(status)) { | |
575 | printf("exited, status=%d\\n", WEXITSTATUS(status)); | |
576 | } else if (WIFSIGNALED(status)) { | |
577 | printf("killed by signal %d\\n", WTERMSIG(status)); | |
578 | } else if (WIFSTOPPED(status)) { | |
579 | printf("stopped by signal %d\\n", WSTOPSIG(status)); | |
580 | } else if (WIFCONTINUED(status)) { | |
581 | printf("continued\\n"); | |
582 | } | |
583 | } while (!WIFEXITED(status) && !WIFSIGNALED(status)); | |
584 | exit(EXIT_SUCCESS); | |
585 | } | |
586 | } | |
1fa343d1 | 587 | .fi |
fea681da | 588 | .SH "SEE ALSO" |
f2351505 | 589 | .BR _exit (2), |
fea681da | 590 | .BR clone (2), |
f2351505 MK |
591 | .BR fork (2), |
592 | .BR kill (2), | |
fea681da | 593 | .BR ptrace (2), |
f2351505 | 594 | .BR sigaction (2), |
fea681da MK |
595 | .BR signal (2), |
596 | .BR wait4 (2), | |
597 | .BR pthread_create (3), | |
53a1443c | 598 | .BR credentials (7), |
fea681da | 599 | .BR signal (7) |