don't use hash mark for comments
[unix-history] / usr / src / lib / libc / sys / wait.2
CommitLineData
775a8be6
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
84d15ab6 5.\" @(#)wait.2 6.3 (Berkeley) %G%
775a8be6 6.\"
60384211 7.TH WAIT 2 ""
775a8be6
KM
8.UC 4
9.SH NAME
84d15ab6 10wait, waitpid, wait4, wait3 \- wait for process to terminate
775a8be6 11.SH SYNOPSIS
28494293 12.ft B
775a8be6 13.nf
84d15ab6 14#include <sys/types.h>
28494293
KM
15#include <sys/wait.h>
16.PP
17.ft B
18pid = wait(status)
84d15ab6
MK
19pid_t pid;
20int *status;
28494293
KM
21.PP
22.ft B
84d15ab6
MK
23pid = waitpid(wpid, status, options)
24pid_t pid, wpid;
25int *status;
26int options;
28494293
KM
27.PP
28.ft B
29#include <sys/time.h>
30#include <sys/resource.h>
775a8be6 31.PP
28494293 32.ft B
84d15ab6
MK
33pid = wait4(wpid, status, options, rusage)
34pid_t pid, wpid;
35int *status;
36int options;
37struct rusage *rusage;
38.PP
39.ft B
28494293 40pid = wait3(status, options, rusage)
84d15ab6
MK
41pid_t pid;
42int *status;
28494293
KM
43int options;
44struct rusage *rusage;
45.fi
775a8be6
KM
46.SH DESCRIPTION
47.I Wait
48causes its caller to delay until a signal is received or
49one of its child
50processes terminates.
51If any child has died since the last
28494293
KM
52.IR wait ,
53return is immediate, returning the process id and
54exit status of one of the terminated
55children.
56If there are no children, return is immediate with
57the value \-1 returned.
775a8be6 58.PP
28494293
KM
59On return from a successful
60.I wait
61call,
84d15ab6 62the
28494293 63.I status
84d15ab6
MK
64area contains termination information about the process that exited
65as defined below.
28494293 66.PP
84d15ab6
MK
67The
68.I wait4
69call provides a more general interface for programs
70that wish to wait for certain child processes,
71that wish resource utilization statistics accummulated by child processes,
72or that require options.
73The other wait functions are implemented using
74.IR wait4 .
75.PP
76The
77.I wpid
78parameter specifies the set of child processes for which to wait.
79If
80.I wpid
81is \-1, the call waits for any child process.
82If
83.I wpid
84is 0,
85the call waits for any child process in the process group of the caller.
86If
87.I wpid
88is greater than zero, the call waits for the process with process id
89.IR wpid .
90If
91.I wpid
92is less than \-1, the call waits for any process whose process group id
93equals the absolute value of
94.IR wpid .
95.PP
96The
28494293 97.I status
84d15ab6 98parameter is defined below. The
28494293 99.I options
84d15ab6
MK
100parameter contains the bitwise OR of any of the following options.
101The WNOHANG option
102is used to indicate that the call should not block if
103there are no processes that wish to report status.
104If the WUNTRACED option is set,
105children of the current process that are stopped
106due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal also have
107their status reported.
108.PP
109If
28494293
KM
110.I rusage
111is non-zero, a summary of the resources used by the terminated
112process and all its
113children is returned (this information is currently not available
114for stopped processes).
115.PP
116When the WNOHANG option is specified and no processes
117wish to report status,
84d15ab6 118.I wait4
28494293
KM
119returns a
120.I pid
84d15ab6
MK
121of 0.
122.PP
123The
124.I waitpid
125call is identical to
126.I wait4
127with an
128.I rusage
129value of zero.
130The older
131.I wait3
132call is the same as
133.I wait4
134with a
135.I wpid
136value of \-1.
137.PP
138The following macros may be used to test the manner of exit of the process.
139One of the first three macros will evaluate to a non-zero (true) value:
140.IP WIFEXITED(status)
141True if the process terminated normally by a call to
142.IR _exit (2)
143or
144.IR exit (2).
145.IP WIFSIGNALED(status)
146True if the process terminated due to receipt of a signal.
147.IP WIFSTOPPED(status)
148True if the process has not terminated, but has stopped and can be restarted.
149This macro can be true only if the wait call specified the
150WUNTRACED
151option
152or if the child process is being traced (see
153.IR ptrace (2)).
154.LP
155Depending on the values of those macros, the following macros
156produce the remaining status information about the child process:
157.IP WEXITSTATUS(status)
158If WIFEXITED(status) is true, evaluates to the low-order 8 bits
159of the argument passed to
160.IR _exit (2)
161or
162.IR exit (2)
163by the child.
164.IP WTERMSIG(status)
165If WIFSIGNALED(status) is true, evaluates to the number of the signal
166that caused the termination of the process.
167.IP WCOREDUMP(status)
168If WIFSIGNALED(status) is true, evaluates as true if the termination
169of the process was accompanied by the creation of a core file
170containing an image of the process when the signal was received.
171.IP WSTOPSIG(status)
172If WIFSTOPPED(status) is true, evaluates to the number of the signal
173that caused the process to stop.
28494293 174.SH NOTES
775a8be6 175See
84d15ab6
MK
176.IR sigaction (2)
177for a list of termination signals.
178A status of 0 indicates normal termination.
775a8be6
KM
179.PP
180If the parent process terminates without
181waiting on its children,
182the initialization process
183(process ID = 1)
184inherits the children.
185.PP
84d15ab6
MK
186If a signal is caught while any of the
187.I wait
188calls is pending,
189the call may be interrupted or restarted when the signal-catching routine
190returns,
191depending on the options in effect for the signal;
192see
193.IR intro (2),
194System call restart.
28494293
KM
195.SH "RETURN VALUE
196If \fIwait\fP returns due to a stopped
197or terminated child process, the process ID of the child
198is returned to the calling process. Otherwise, a value of \-1
199is returned and \fIerrno\fP is set to indicate the error.
775a8be6 200.PP
84d15ab6
MK
201If
202.IR wait4 ,
203.I wait3
204or
205.I waitpid
206returns due to a stopped
207or terminated child process, the process ID of the child
208is returned to the calling process.
209If there are no children not previously awaited,
210\-1 is returned with
211.I errno
212set to [ECHILD].
213Otherwise, if WNOHANG is specified and there are
214no stopped or exited children,
2150 is returned.
216If an error is detected or a caught signal aborts the call,
217a value of \-1
218is returned and \fIerrno\fP is set to indicate the error.
28494293
KM
219.SH ERRORS
220.I Wait
221will fail and return immediately if one or more of the following
222are true:
223.TP 15
224[ECHILD]
225The calling process has no existing unwaited-for
226child processes.
227.TP 15
228[EFAULT]
229The \fIstatus\fP or \fIrusage\fP arguments point to an illegal address.
84d15ab6
MK
230(May not be detected before exit of a child process.)
231.TP 15
232[EINTR]
233The call was interrupted by a caught signal,
234or the signal did not have the SA_RESTART flag set.
235.SH STANDARDS
236The
237.I wait
238and
239.I waitpid
240functions are defined by POSIX;
241.I wait4
242and
243.I wait3
244are not specified by POSIX.
245The WCOREDUMP macro
246and the ability to restart a pending
247.I wait
248call are extensions to the POSIX interface.
28494293 249.SH "SEE ALSO"
84d15ab6 250exit(2), sigaction(2)