-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitpid.html
824 lines (621 loc) · 24.5 KB
/
waitpid.html
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:47 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>WAIT</title>
</head>
<body>
<h1 align="center">WAIT</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">wait, waitpid,
waitid - wait for process to change state</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/types.h> <br>
#include <sys/wait.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>pid_t
wait(int *</b><i>wstatus</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>pid_t
waitpid(pid_t</b> <i>pid</i><b>, int *</b><i>wstatus</i><b>,
int</b> <i>options</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
waitid(idtype_t</b> <i>idtype</i><b>, id_t</b> <i>id</i><b>,
siginfo_t *</b><i>infop</i><b>, int</b>
<i>options</i><b>);</b> <br>
/* This is the glibc and POSIX interface; see <br>
NOTES for information on the raw system call. */</p>
<p style="margin-left:5%; margin-top: 1em">Feature Test
Macro Requirements for glibc (see
<b>feature_test_macros</b>(7)):</p>
<p style="margin-left:11%; margin-top: 1em"><b>waitid</b>():</p>
<p style="margin-left:17%;">Since glibc 2.26: _XOPEN_SOURCE
>= 500 || <br>
_POSIX_C_SOURCE >= 200809L <br>
Glibc 2.25 and earlier: <br>
_XOPEN_SOURCE <br>
|| /* Since glibc 2.12: */
_POSIX_C_SOURCE >= 200809L <br>
|| /* Glibc versions <= 2.19: */ _BSD_SOURCE</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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).</p>
<p style="margin-left:11%; margin-top: 1em">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</b> flag of <b>sigaction</b>(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 <i>waitable</i>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>wait() and
waitpid()</b> <br>
The <b>wait</b>() system call suspends execution of the
calling thread until one of its children terminates. The
call <i>wait(&wstatus)</i> is equivalent to:</p>
<p style="margin-left:17%; margin-top: 1em">waitpid(-1,
&wstatus, 0);</p>
<p style="margin-left:11%; margin-top: 1em">The
<b>waitpid</b>() system call suspends execution of the
calling thread until a child specified by <i>pid</i>
argument has changed state. By default, <b>waitpid</b>()
waits only for terminated children, but this behavior is
modifiable via the <i>options</i> argument, as described
below.</p>
<p style="margin-left:11%; margin-top: 1em">The value of
<i>pid</i> can be:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="6%">
<p>< -1</p></td>
<td width="5%"></td>
<td width="78%">
<p>meaning wait for any child process whose process group
ID is equal to the absolute value of <i>pid</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="6%">
<p>-1</p></td>
<td width="5%"></td>
<td width="78%">
<p>meaning wait for any child process.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="6%">
<p>0</p></td>
<td width="5%"></td>
<td width="78%">
<p>meaning wait for any child process whose process group
ID is equal to that of the calling process.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="6%">
<p>> 0</p></td>
<td width="5%"></td>
<td width="78%">
<p>meaning wait for the child whose process ID is equal to
the value of <i>pid</i>.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The value of
<i>options</i> is an OR of zero or more of the following
constants:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p style="margin-top: 1em"><b>WNOHANG</b></p></td>
<td width="4%"></td>
<td width="71%">
<p style="margin-top: 1em">return immediately if no child
has exited.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p><b>WUNTRACED</b></p></td>
<td width="4%"></td>
<td width="71%">
<p>also return if a child has stopped (but not traced via
<b>ptrace</b>(2)). Status for <i>traced</i> children which
have stopped is provided even if this option is not
specified.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>WCONTINUED</b> (since Linux
2.6.10)</p>
<p style="margin-left:29%;">also return if a stopped child
has been resumed by delivery of <b>SIGCONT</b>.</p>
<p style="margin-left:11%; margin-top: 1em">(For Linux-only
options, see below.)</p>
<p style="margin-left:11%; margin-top: 1em">If
<i>wstatus</i> is not NULL, <b>wait</b>() and
<b>waitpid</b>() store status information in the <i>int</i>
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 <b>wait</b>()
and <b>waitpid</b>()!): <b><br>
WIFEXITED(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns true if the child
terminated normally, that is, by calling <b>exit</b>(3) or
<b>_exit</b>(2), or by returning from main().</p>
<p style="margin-left:11%;"><b>WEXITSTATUS(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns the exit status of the
child. This consists of the least significant 8 bits of the
<i>status</i> argument that the child specified in a call to
<b>exit</b>(3) or <b>_exit</b>(2) or as the argument for a
return statement in main(). This macro should be employed
only if <b>WIFEXITED</b> returned true.</p>
<p style="margin-left:11%;"><b>WIFSIGNALED(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns true if the child
process was terminated by a signal.</p>
<p style="margin-left:11%;"><b>WTERMSIG(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns the number of the
signal that caused the child process to terminate. This
macro should be employed only if <b>WIFSIGNALED</b> returned
true.</p>
<p style="margin-left:11%;"><b>WCOREDUMP(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns true if the child
produced a core dump (see <b>core</b>(5)). This macro should
be employed only if <b>WIFSIGNALED</b> returned true.</p>
<p style="margin-left:22%; margin-top: 1em">This macro is
not specified in POSIX.1-2001 and is not available on some
UNIX implementations (e.g., AIX, SunOS). Therefore, enclose
its use inside <i>#ifdef WCOREDUMP ... #endif</i>.</p>
<p style="margin-left:11%;"><b>WIFSTOPPED(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns true if the child
process was stopped by delivery of a signal; this is
possible only if the call was done using <b>WUNTRACED</b> or
when the child is being traced (see <b>ptrace</b>(2)).</p>
<p style="margin-left:11%;"><b>WSTOPSIG(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">returns the number of the
signal which caused the child to stop. This macro should be
employed only if <b>WIFSTOPPED</b> returned true.</p>
<p style="margin-left:11%;"><b>WIFCONTINUED(</b><i>wstatus</i><b>)</b></p>
<p style="margin-left:22%;">(since Linux 2.6.10) returns
true if the child process was resumed by delivery of
<b>SIGCONT</b>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>waitid()</b>
<br>
The <b>waitid</b>() system call (available since Linux
2.6.9) provides more precise control over which child state
changes to wait for.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>idtype</i> and <i>id</i> arguments select the child(ren)
to wait for, as follows: <i><br>
idtype</i> == <b>P_PID</b></p>
<p style="margin-left:22%;">Wait for the child whose
process ID matches <i>id</i>.</p>
<p style="margin-left:11%;"><i>idtype</i> ==
<b>P_PGID</b></p>
<p style="margin-left:22%;">Wait for any child whose
process group ID matches <i>id</i>.</p>
<p style="margin-left:11%;"><i>idtype</i> ==
<b>P_ALL</b></p>
<p style="margin-left:22%;">Wait for any child; <i>id</i>
is ignored.</p>
<p style="margin-left:11%; margin-top: 1em">The child state
changes to wait for are specified by ORing one or more of
the following flags in <i>options</i>:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="15%">
<p><b>WEXITED</b></p></td>
<td width="3%"></td>
<td width="71%">
<p>Wait for children that have terminated.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="15%">
<p><b>WSTOPPED</b></p></td>
<td width="3%"></td>
<td width="71%">
<p>Wait for children that have been stopped by delivery of
a signal.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="15%">
<p><b>WCONTINUED</b></p></td>
<td width="3%"></td>
<td width="71%">
<p>Wait for (previously stopped) children that have been
resumed by delivery of <b>SIGCONT</b>.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The following
flags may additionally be ORed in <i>options</i>:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="11%">
<p style="margin-top: 1em"><b>WNOHANG</b></p></td>
<td width="7%"></td>
<td width="71%">
<p style="margin-top: 1em">As for <b>waitpid</b>().</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="11%">
<p><b>WNOWAIT</b></p></td>
<td width="7%"></td>
<td width="71%">
<p>Leave the child in a waitable state; a later wait call
can be used to again retrieve the child status
information.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">Upon successful
return, <b>waitid</b>() fills in the following fields of the
<i>siginfo_t</i> structure pointed to by <i>infop</i>:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p style="margin-top: 1em"><i>si_pid</i></p></td>
<td width="4%"></td>
<td width="71%">
<p style="margin-top: 1em">The process ID of the child.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p><i>si_uid</i></p></td>
<td width="4%"></td>
<td width="71%">
<p>The real user ID of the child. (This field is not set on
most other implementations.)</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p><i>si_signo</i></p></td>
<td width="4%"></td>
<td width="71%">
<p>Always set to <b>SIGCHLD</b>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p><i>si_status</i></p></td>
<td width="4%"></td>
<td width="71%">
<p>Either the exit status of the child, as given to
<b>_exit</b>(2) (or <b>exit</b>(3)), or the signal that
caused the child to terminate, stop, or continue. The
<i>si_code</i> field can be used to determine how to
interpret this field.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="14%">
<p><i>si_code</i></p></td>
<td width="4%"></td>
<td width="71%">
<p>Set to one of: <b>CLD_EXITED</b> (child called
<b>_exit</b>(2)); <b>CLD_KILLED</b> (child killed by
signal); <b>CLD_DUMPED</b> (child killed by signal, and
dumped core); <b>CLD_STOPPED</b> (child stopped by signal);
<b>CLD_TRAPPED</b> (traced child has trapped); or
<b>CLD_CONTINUED</b> (child continued by
<b>SIGCONT</b>).</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">If
<b>WNOHANG</b> was specified in <i>options</i> and there
were no children in a waitable state, then <b>waitid</b>()
returns 0 immediately and the state of the <i>siginfo_t</i>
structure pointed to by <i>infop</i> depends on the
implementation. To (portably) distinguish this case from
that where a child was in a waitable state, zero out the
<i>si_pid</i> field before the call and check for a nonzero
value in this field after the call returns.</p>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2008
Technical Corrigendum 1 (2013) adds the requirement that
when <b>WNOHANG</b> is specified in <i>options</i> and there
were no children in a waitable state, then <b>waitid</b>()
should zero out the <i>si_pid</i> and <i>si_signo</i> fields
of the structure. On Linux and other implementations that
adhere to this requirement, it is not necessary to zero out
the <i>si_pid</i> field before calling <b>waitid</b>().
However, not all implementations follow the POSIX.1
specification on this point.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>wait</b>():
on success, returns the process ID of the terminated child;
on error, -1 is returned.</p>
<p style="margin-left:11%; margin-top: 1em"><b>waitpid</b>():
on success, returns the process ID of the child whose state
has changed; if <b>WNOHANG</b> was specified and one or more
child(ren) specified by <i>pid</i> exist, but have not yet
changed state, then 0 is returned. On error, -1 is
returned.</p>
<p style="margin-left:11%; margin-top: 1em"><b>waitid</b>():
returns 0 on success or if <b>WNOHANG</b> was specified and
no child(ren) specified by <i>id</i> has yet changed state;
on error, -1 is returned.</p>
<p style="margin-left:11%; margin-top: 1em">Each of these
calls sets <i>errno</i> to an appropriate value in the case
of an error.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>ECHILD</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">(for <b>wait</b>()) The calling
process does not have any unwaited-for children.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ECHILD</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>(for <b>waitpid</b>() or <b>waitid</b>()) The process
specified by <i>pid</i> (<b>waitpid</b>()) or <i>idtype</i>
and <i>id</i> (<b>waitid</b>()) 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</b> is
set to <b>SIG_IGN</b>. See also the <i>Linux Notes</i>
section about threads.)</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINTR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><b>WNOHANG</b> was not set and an unblocked signal or a
<b>SIGCHLD</b> was caught; see <b>signal</b>(7).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The <i>options</i> argument was invalid.</p></td></tr>
</table>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">SVr4, 4.3BSD,
POSIX.1-2001.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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 <b>init</b>(1), (or by the nearest
"subreaper" process as defined through the use of
the <b>prctl</b>(2) <b>PR_SET_CHILD_SUBREAPER</b>
operation); <b>init</b>(1) automatically performs a wait to
remove the zombies.</p>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2001
specifies that if the disposition of <b>SIGCHLD</b> is set
to <b>SIG_IGN</b> or the <b>SA_NOCLDWAIT</b> flag is set for
<b>SIGCHLD</b> (see <b>sigaction</b>(2)), then children that
terminate do not become zombies and a call to <b>wait</b>()
or <b>waitpid</b>() will block until all children have
terminated, and then fail with <i>errno</i> set to
<b>ECHILD</b>. (The original POSIX standard left the
behavior of setting <b>SIGCHLD</b> to <b>SIG_IGN</b>
unspecified. Note that even though the default disposition
of <b>SIGCHLD</b> is "ignore", explicitly setting
the disposition to <b>SIG_IGN</b> results in different
treatment of zombie process children.)</p>
<p style="margin-left:11%; margin-top: 1em">Linux 2.6
conforms to the POSIX requirements. However, Linux 2.4 (and
earlier) does not: if a <b>wait</b>() or <b>waitpid</b>()
call is made while <b>SIGCHLD</b> is being ignored, the call
behaves just as though <b>SIGCHLD</b> 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.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Linux
notes</b> <br>
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
<b>clone</b>(2) system call; other routines such as the
portable <b>pthread_create</b>(3) call are implemented using
<b>clone</b>(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.</p>
<p style="margin-left:11%; margin-top: 1em">The following
Linux-specific <i>options</i> are for use with children
created using <b>clone</b>(2); they can also, since Linux
4.7, be used with <b>waitid</b>(): <b><br>
__WCLONE</b></p>
<p style="margin-left:22%;">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</b> to its parent upon termination.) This
option is ignored if <b>__WALL</b> is also specified.</p>
<p style="margin-left:11%;"><b>__WALL</b> (since Linux
2.4)</p>
<p style="margin-left:22%;">Wait for all children,
regardless of type ("clone" or
"non-clone").</p>
<p style="margin-left:11%;"><b>__WNOTHREAD</b> (since Linux
2.4)</p>
<p style="margin-left:22%;">Do not wait for children of
other threads in the same thread group. This was the default
before Linux 2.4.</p>
<p style="margin-left:11%; margin-top: 1em">Since Linux
4.7, the <b>__WALL</b> flag is automatically implied if the
child is being ptraced.</p>
<p style="margin-left:11%; margin-top: 1em"><b>C
library/kernel differences <br>
wait</b>() is actually a library function that (in glibc) is
implemented as a call to <b>wait4</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em">On some
architectures, there is no <b>waitpid</b>() system call;
instead, this interface is implemented via a C library
wrapper function that calls <b>wait4</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em">The raw
<b>waitid</b>() system call takes a fifth argument, of type
<i>struct rusage *</i>. If this argument is non-NULL,
then it is used to return resource usage information about
the child, in the same manner as <b>wait4</b>(2). See
<b>getrusage</b>(2) for details.</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">According to
POSIX.1-2008, an application calling <b>waitid</b>() must
ensure that <i>infop</i> points to a <i>siginfo_t</i>
structure (i.e., that it is a non-null pointer). On Linux,
if <i>infop</i> is NULL, <b>waitid</b>() succeeds, and
returns the process ID of the waited-for child. Applications
should avoid relying on this inconsistent, nonstandard, and
unnecessary feature.</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The following
program demonstrates the use of <b>fork</b>(2) and
<b>waitpid</b>(). The program creates a child process. If no
command-line argument is supplied to the program, then the
child suspends its execution using <b>pause</b>(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 <b>waitpid</b>(), and uses the W*()
macros described above to analyze the wait status value.</p>
<p style="margin-left:11%; margin-top: 1em">The following
shell session demonstrates the use of the program:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>./a.out
&</b> <br>
Child PID is 32360 <br>
[1] 32359 <br>
$ <b>kill -STOP 32360</b> <br>
stopped by signal 19 <br>
$ <b>kill -CONT 32360</b> <br>
continued <br>
$ <b>kill -TERM 32360</b> <br>
killed by signal 15 <br>
[1]+ Done ./a.out <br>
$</p>
<p style="margin-left:11%; margin-top: 1em"><b>Program
source</b> <br>
#include <sys/wait.h> <br>
#include <stdlib.h> <br>
#include <unistd.h> <br>
#include <stdio.h></p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
pid_t cpid, w; <br>
int wstatus;</p>
<p style="margin-left:11%; margin-top: 1em">cpid = fork();
<br>
if (cpid == -1) { <br>
perror("fork"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if (cpid == 0)
{ /* Code executed by child */ <br>
printf("Child PID is %ld\n", (long) getpid());
<br>
if (argc == 1) <br>
pause(); /* Wait for signals */ <br>
_exit(atoi(argv[1]));</p>
<p style="margin-left:11%; margin-top: 1em">} else { /*
Code executed by parent */ <br>
do { <br>
w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
<br>
if (w == -1) { <br>
perror("waitpid"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(WIFEXITED(wstatus)) { <br>
printf("exited, status=%d\n",
WEXITSTATUS(wstatus)); <br>
} else if (WIFSIGNALED(wstatus)) { <br>
printf("killed by signal %d\n",
WTERMSIG(wstatus)); <br>
} else if (WIFSTOPPED(wstatus)) { <br>
printf("stopped by signal %d\n",
WSTOPSIG(wstatus)); <br>
} else if (WIFCONTINUED(wstatus)) { <br>
printf("continued\n"); <br>
} <br>
} while (!WIFEXITED(wstatus) &&
!WIFSIGNALED(wstatus)); <br>
exit(EXIT_SUCCESS); <br>
} <br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>_exit</b>(2),
<b>clone</b>(2), <b>fork</b>(2), <b>kill</b>(2),
<b>ptrace</b>(2), <b>sigaction</b>(2), <b>signal</b>(2),
<b>wait4</b>(2), <b>pthread_create</b>(3), <b>core</b>(5),
<b>credentials</b>(7), <b>signal</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>