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