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
|
'\" te
.\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright (c) 1980 Regents of the University of California. All rights reserved. The Berkeley software License Agreement specifies the terms and conditions for redistribution.
.TH WAIT 3UCB "Oct 30, 2007"
.SH NAME
wait, wait3, wait4, waitpid, WIFSTOPPED, WIFSIGNALED, WIFEXITED \- wait for
process to terminate or stop
.SH SYNOPSIS
.LP
.nf
\fB/usr/ucb/cc\fR [ \fIflag\fR ... ] \fIfile\fR ...
#include <sys/wait.h>
\fBint\fR \fBwait\fR(\fIstatusp\fR)
\fBint *\fR\fIstatusp\fR;
.fi
.LP
.nf
\fBint\fR \fBwaitpid\fR(\fIpid\fR, \fIstatusp\fR, \fIoptions\fR)
\fBint\fR \fIpid\fR;
\fBint *\fR\fIstatusp\fR;
\fBint\fR \fIoptions\fR;
.fi
.LP
.nf
#include <sys/time.h>
#include <sys/resource.h>
\fBint\fR \fBwait3\fR(\fIstatusp\fR, \fIoptions\fR, \fIrusage\fR)
\fBint *\fR\fIstatusp\fR;
\fBint\fR \fIoptions\fR;
\fBstruct rusage *\fR\fIrusage\fR;
.fi
.LP
.nf
\fBint\fR \fBwait4\fR(\fIpid\fR, \fIstatusp\fR, \fIoptions\fR, \fIrusage\fR)
\fBint\fR \fIpid\fR;
\fBint *\fR\fIstatusp\fR;
\fBint\fR \fIoptions\fR;
\fBstruct rusage *\fR\fIrusage\fR;
.fi
.LP
.nf
\fBWIFSTOPPED\fR(\fIstatus\fR)
\fBint\fR \fIstatus\fR;
.fi
.LP
.nf
\fBWIFSIGNALED\fR(\fIstatus\fR)
\fBint\fR \fIstatus\fR;
.fi
.LP
.nf
\fBWIFEXITED\fR(\fIstatus\fR)
\fBint\fR \fIstatus\fR;
.fi
.SH DESCRIPTION
.sp
.LP
The \fBwait()\fR function delays its caller until a signal is received or one
of its child processes terminates or stops due to tracing. If any child process
has died or stopped due to tracing and this has not been reported using
\fBwait()\fR, return is immediate, returning the process \fBID\fR and exit
status of one of those children. If that child process has died, it is
discarded. If there are no children, return is immediate with the value \(mi1
returned. If there are only running or stopped but reported children, the
calling process is blocked.
.sp
.LP
If \fIstatus\fR is not a \fINULL\fR pointer, then on return from a successful
\fBwait()\fR call the status of the child process whose process \fBID\fR is the
return value of \fBwait()\fR is stored in the \fBwait()\fR union pointed to by
\fIstatus\fR. The \fBw_status\fR member of that union is an \fBint\fR; it
indicates the cause of termination and other information about the terminated
process in the following manner:
.RS +4
.TP
.ie t \(bu
.el o
If the low-order 8 bits of \fBw_status\fR are equal to 0177, the child process
has stopped; the 8 bits higher up from the low-order 8 bits of \fBw_status\fR
contain the number of the signal that caused the process to stop.
See\fBptrace\fR(3C) and \fBsigvec\fR(3UCB).
.RE
.RS +4
.TP
.ie t \(bu
.el o
If the low-order 8 bits of \fBw_status\fR are non-zero and are not equal to
0177, the child process terminated due to a signal; the low-order 7 bits of
\fBw_status\fR contain the number of the signal that terminated the process. In
addition, if the low-order seventh bit of \fBw_status\fR (that is, bit 0200) is
set, a ``core image'' of the process was produced; see \fBsigvec\fR(3UCB).
.RE
.RS +4
.TP
.ie t \(bu
.el o
Otherwise, the child process terminated due to an \fBexit()\fR call; the 8 bits
higher up from the low-order 8 bits of \fBw_status\fR contain the low-order 8
bits of the argument that the child process passed to \fBexit()\fR; see
\fBexit\fR(2).
.RE
.sp
.LP
\fBwaitpid()\fR behaves identically to \fBwait()\fR if \fIpid\fR has a value
of \(mi1 and \fIoptions\fR has a value of zero. Otherwise, the behavior of
\fBwaitpid()\fR is modified by the values of \fIpid\fR and \fIoptions\fR as
follows:
.sp
.LP
\fIpid\fR specifies a set of child processes for which status is requested.
\fBwaitpid()\fR only returns the status of a child process from this set.
.RS +4
.TP
.ie t \(bu
.el o
If \fIpid\fR is equal to \(mi1, status is requested for any child process. In
this respect, \fBwaitpid()\fR is then equivalent to \fBwait()\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fIpid\fR is greater than zero, it specifies the process \fBID\fR of a
single child process for which status is requested.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fIpid\fR is equal to zero, status is requested for any child process whose
process group \fBID\fR is equal to that of the calling process.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fIpid\fR is less than \(mi1, status is requested for any child process
whose process group \fBID\fR is equal to the absolute value of \fIpid\fR.
.RE
.sp
.LP
\fIoptions\fR is constructed from the bitwise inclusive \fBOR\fR of zero or
more of the following flags, defined in the header \fB<sys/wait.h>\fR:
.sp
.ne 2
.na
\fB\fBWNOHANG\fR\fR
.ad
.RS 13n
\fBwaitpid()\fR does not suspend execution of the calling process if status is
not immediately available for one of the child processes specified by
\fIpid\fR.
.RE
.sp
.ne 2
.na
\fB\fBWUNTRACED\fR\fR
.ad
.RS 13n
The status of any child processes specified by \fIpid\fR that are stopped, and
whose status has not yet been reported since they stopped, are also reported to
the requesting process.
.RE
.sp
.LP
\fBwait3()\fR is an alternate interface that allows both non-blocking status
collection and the collection of the status of children stopped by any means.
The \fIstatus\fR parameter is defined as above. The \fIoptions\fR parameter is
used to indicate the call should not block if there are no processes that have
status to report (\fBWNOHANG\fR), and/or that children of the current process
that are stopped due to a \fBSIGTTIN\fR, \fBSIGTTOU\fR, \fBSIGTSTP\fR, or
\fBSIGSTOP\fR signal are eligible to have their status reported as well
(\fBWUNTRACED\fR). A terminated child is discarded after it reports status, and
a stopped process will not report its status more than once. If \fIrusage\fR is
not a \fINULL\fR pointer, a summary of the resources used by the terminated
process and all its children is returned. Only the user time used and the
system time used are currently available. They are returned in
\fBrusage.ru_utime\fR and \fBrusage.ru_stime\fR, respectively.
.sp
.LP
When the \fBWNOHANG\fR option is specified and no processes have status to
report, \fBwait3()\fR returns 0. The \fBWNOHANG\fR and \fBWUNTRACED\fR options
may be combined by \fBORing\fR the two values.
.sp
.LP
\fBwait4()\fR is another alternate interface. With a \fIpid\fR argument of 0,
it is equivalent to \fBwait3()\fR. If \fIpid\fR has a nonzero value, then
\fBwait4()\fR returns status only for the indicated process \fBID,\fR but not
for any other child processes.
.sp
.LP
\fBWIFSTOPPED\fR, \fBWIFSIGNALED\fR, \fBWIFEXITED\fR, are macros that take an
argument \fIstatus\fR, of type \fBint\fR, as returned by \fBwait()\fR, or
\fBwait3()\fR, or \fBwait4()\fR. \fBWIFSTOPPED\fR evaluates to true (1) when
the process for which the \fBwait()\fR call was made is stopped, or to false
(0) otherwise. \fBWIFSIGNALED\fR evaluates to true when the process was
terminated with a signal. \fBWIFEXITED\fR evaluates to true when the process
exited by using an \fBexit\fR(2) call.
.SH RETURN VALUES
.sp
.LP
If \fBwait()\fRor \fBwaitpid()\fR returns due to a stopped or terminated child
process, the process ID of the child is returned to the calling process.
Otherwise, a value of \fB\(mi1\fR is returned and \fBerrno\fR is set to
indicate the error.
.sp
.LP
If \fBwait()\fR or \fBwaitpid()\fR return due to the delivery of a signal to
the calling process, a value of \fB\(mi1\fR is returned and \fBerrno\fR is set
to \fBEINTR.\fR If \fBwaitpid()\fR function was invoked with \fBWNOHANG\fR set
in \fIoptions\fR, it has at least one child process specified by \fIpid\fR for
which status is not available, and status is not available for any process
specified by \fIpid\fR, a value of zero is returned. Otherwise, a value of
\fB\(mi1\fR is returned, and \fBerrno\fR is set to indicate the error.
.sp
.LP
\fBwait3(\|)\fR and \fBwait4(\|)\fR return 0 if \fBWNOHANG\fR is specified and
there are no stopped or exited children, and returns the process \fBID\fR of
the child process if it returns due to a stopped or terminated child process.
Otherwise, they returns a value of \fB\(mi1\fR and sets \fBerrno\fR to indicate
the error.
.SH ERRORS
.sp
.LP
The \fBwait()\fR, \fBwait3()\fR and \fBwait4()\fR functions will fail and
return immediately if:
.sp
.ne 2
.na
\fB\fBECHILD\fR\fR
.ad
.RS 10n
The calling process has no existing unwaited-for child processes.
.RE
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fIstatus\fR or \fIrusage\fR arguments point to an illegal address.
.RE
.sp
.LP
\fBwaitpid()\fR may set \fBerrno\fR to:
.sp
.ne 2
.na
\fB\fBECHILD\fR\fR
.ad
.RS 10n
The process or process group specified by \fIpid\fR does not exist or is not a
child of the calling process.
.RE
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 10n
The function was interrupted by a signal. The value of the location pointed to
by \fIstatusp\fR is undefined.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The value of \fIoptions\fR is not valid.
.RE
.sp
.LP
The \fBwait()\fR, \fBwait3()\fR, and \fBwait4()\fR functions will terminate
prematurely, return \(mi1, and set \fBerrno\fR to \fBEINTR\fR upon the arrival
of a signal whose \fBSV_INTERRUPT\fR bit in its flags field is set (see
\fBsigvec\fR(3UCB) and \fBsiginterrupt\fR(3UCB)). \fBsignal\fR(3UCB), sets this
bit for any signal it catches.
.SH SEE ALSO
.sp
.LP
\fBexit\fR(2), \fBgetrusage\fR(3C), \fBptrace\fR(3C), \fBsiginterrupt\fR(3UCB),
\fBsignal\fR(3C), \fBsignal\fR(3UCB), \fBsigvec\fR(3UCB), \fBwait\fR(3C),
\fBwaitpid\fR(3C)
.SH NOTES
.sp
.LP
Use of these interfaces should be restricted to only applications written on
BSD platforms. Use of these interfaces with any of the system libraries or in
multi-thread applications is unsupported.
.sp
.LP
If a parent process terminates without waiting on its children, the
initialization process (process \fBID\fR = 1) inherits the children.
.sp
.LP
The \fBwait()\fR, \fBwait3()\fR, and \fBwait4()\fR functions are automatically
restarted when a process receives a signal while awaiting termination of a
child process, unless the \fBSV_INTERRUPT\fR bit is set in the flags for that
signal.
.sp
.LP
Calls to \fBwait()\fR with an argument of \fB0\fR should be cast to type
`\fBint *\fR', as in:
.sp
.in +2
.nf
\fBwait((int *)0)\fR
.fi
.in -2
.sp
.sp
.LP
Previous SunOS releases used \fBunion\fR \fBwait\fR\fB*statusp\fR and
\fBunion\fR \fBwait\fR \fBstatus\fR in place of \fBint *statusp\fR and
\fBint\fR status. The union contained a member \fBw_status\fR that could be
treated in the same way as \fIstatus\fR.
.sp
.LP
Other members of the \fBwait\fR union could be used to extract this information
more conveniently:
.RS +4
.TP
.ie t \(bu
.el o
If the \fBw_stopval\fR member had the value \fB\fR\fBWSTOPPED\fR\fB, \fR the
child process had stopped; the value of the \fBw_stopsig\fR member was the
signal that stopped the process.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If the \fBw_termsig\fR member was non-zero, the child process terminated due to
a signal; the value of the \fBw_termsig\fR member was the number of the signal
that terminated the process. If the \fBw_coredump\fR member was non-zero, a
core dump was produced.
.RE
.RS +4
.TP
.ie t \(bu
.el o
Otherwise, the child process terminated due to a call to \fBexit()\fR. The
value of the \fBw_retcode\fR member was the low-order 8 bits of the argument
that the child process passed to \fBexit()\fR.
.RE
.sp
.LP
\fBunion\fR \fBwait\fR is obsolete in light of the new specifications provided
by \fIIEEE Std 1003.1-1988\fR and endorsed by \fISVID89\fR and \fIXPG3\fR.
SunOS Release 4.1 supports \fBunion\fR\fBwait\fR for backward compatibility,
but it will disappear in a future release.
|