1.\" Copyright (c) 1980, 1991, 1993, 1994 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 4. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)wait.2 8.2 (Berkeley) 4/19/94 29.\" $FreeBSD$ 30.\" 31.Dd November 12, 2005 32.Dt WAIT 2 33.Os 34.Sh NAME 35.Nm wait , 36.Nm waitpid , 37.Nm wait4 , 38.Nm wait3 39.Nd wait for process termination 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In sys/types.h 44.In sys/wait.h 45.Ft pid_t 46.Fn wait "int *status" 47.In sys/time.h 48.In sys/resource.h 49.Ft pid_t 50.Fn waitpid "pid_t wpid" "int *status" "int options" 51.Ft pid_t 52.Fn wait3 "int *status" "int options" "struct rusage *rusage" 53.Ft pid_t 54.Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage" 55.Sh DESCRIPTION 56The 57.Fn wait 58function suspends execution of its calling process until 59.Fa status 60information is available for a terminated child process, 61or a signal is received. 62On return from a successful 63.Fn wait 64call, 65the 66.Fa status 67area contains termination information about the process that exited 68as defined below. 69.Pp 70The 71.Fn wait4 72system call provides a more general interface for programs 73that need to wait for certain child processes, 74that need resource utilization statistics accumulated by child processes, 75or that require options. 76The other wait functions are implemented using 77.Fn wait4 . 78.Pp 79The 80.Fa wpid 81argument specifies the set of child processes for which to wait. 82If 83.Fa wpid 84is -1, the call waits for any child process. 85If 86.Fa wpid 87is 0, 88the call waits for any child process in the process group of the caller. 89If 90.Fa wpid 91is greater than zero, the call waits for the process with process id 92.Fa wpid . 93If 94.Fa wpid 95is less than -1, the call waits for any process whose process group id 96equals the absolute value of 97.Fa wpid . 98.Pp 99The 100.Fa status 101argument is defined below. 102The 103.Fa options 104argument contains the bitwise OR of any of the following options. 105The 106.Dv WCONTINUED 107option indicates that children of the current process that 108have continued from a job control stop, by receiving a 109.Dv SIGCONT 110signal, should also have their status reported. 111The 112.Dv WNOHANG 113option 114is used to indicate that the call should not block if 115there are no processes that wish to report status. 116If the 117.Dv WUNTRACED 118option is set, 119children of the current process that are stopped 120due to a 121.Dv SIGTTIN , SIGTTOU , SIGTSTP , 122or 123.Dv SIGSTOP 124signal also have 125their status reported. 126.Pp 127If 128.Fa rusage 129is non-zero, a summary of the resources used by the terminated 130process and all its 131children is returned (this information is currently not available 132for stopped or continued processes). 133.Pp 134When the 135.Dv WNOHANG 136option is specified and no processes 137wish to report status, 138.Fn wait4 139returns a 140process id 141of 0. 142.Pp 143The 144.Fn waitpid 145function is identical to 146.Fn wait4 147with an 148.Fa rusage 149value of zero. 150The older 151.Fn wait3 152call is the same as 153.Fn wait4 154with a 155.Fa wpid 156value of -1. 157.Pp 158The following macros may be used to test the manner of exit of the process. 159One of the first three macros will evaluate to a non-zero (true) value: 160.Bl -tag -width Ds 161.It Fn WIFCONTINUED status 162True if the process has not terminated, and 163has continued after a job control stop. 164This macro can be true only if the wait call specified the 165.Dv WCONTINUED 166option). 167.It Fn WIFEXITED status 168True if the process terminated normally by a call to 169.Xr _exit 2 170or 171.Xr exit 3 . 172.It Fn WIFSIGNALED status 173True if the process terminated due to receipt of a signal. 174.It Fn WIFSTOPPED status 175True if the process has not terminated, but has stopped and can be restarted. 176This macro can be true only if the wait call specified the 177.Dv WUNTRACED 178option 179or if the child process is being traced (see 180.Xr ptrace 2 ) . 181.El 182.Pp 183Depending on the values of those macros, the following macros 184produce the remaining status information about the child process: 185.Bl -tag -width Ds 186.It Fn WEXITSTATUS status 187If 188.Fn WIFEXITED status 189is true, evaluates to the low-order 8 bits 190of the argument passed to 191.Xr _exit 2 192or 193.Xr exit 3 194by the child. 195.It Fn WTERMSIG status 196If 197.Fn WIFSIGNALED status 198is true, evaluates to the number of the signal 199that caused the termination of the process. 200.It Fn WCOREDUMP status 201If 202.Fn WIFSIGNALED status 203is true, evaluates as true if the termination 204of the process was accompanied by the creation of a core file 205containing an image of the process when the signal was received. 206.It Fn WSTOPSIG status 207If 208.Fn WIFSTOPPED status 209is true, evaluates to the number of the signal 210that caused the process to stop. 211.El 212.Sh NOTES 213See 214.Xr sigaction 2 215for a list of termination signals. 216A status of 0 indicates normal termination. 217.Pp 218If a parent process terminates without 219waiting for all of its child processes to terminate, 220the remaining child processes are assigned the parent 221process 1 ID (the init process ID). 222.Pp 223If a signal is caught while any of the 224.Fn wait 225calls are pending, 226the call may be interrupted or restarted when the signal-catching routine 227returns, 228depending on the options in effect for the signal; 229see discussion of 230.Dv SA_RESTART 231in 232.Xr sigaction 2 . 233.Pp 234The implementation queues one 235.Dv SIGCHLD 236signal for each child process whose 237status has changed, if 238.Fn wait 239returns because the status of a child process is available, the pending 240SIGCHLD signal associated with the process ID of the child process will 241be discarded. 242Any other pending 243.Dv SIGCHLD 244signals remain pending. 245.Pp 246If 247.Dv SIGCHLD 248is blocked, 249.Fn wait 250returns because the status of a child process is available, the pending 251.Dv SIGCHLD 252signal will be cleared unless another status of the child process 253is available. 254.Sh RETURN VALUES 255If 256.Fn wait 257returns due to a stopped, continued, 258or terminated child process, the process ID of the child 259is returned to the calling process. 260Otherwise, a value of \-1 261is returned and 262.Va errno 263is set to indicate the error. 264.Pp 265If 266.Fn wait4 , 267.Fn wait3 , 268or 269.Fn waitpid 270returns due to a stopped, continued, 271or terminated child process, the process ID of the child 272is returned to the calling process. 273If there are no children not previously awaited, 274-1 is returned with 275.Va errno 276set to 277.Er ECHILD . 278Otherwise, if 279.Dv WNOHANG 280is specified and there are 281no stopped, continued or exited children, 2820 is returned. 283If an error is detected or a caught signal aborts the call, 284a value of -1 285is returned and 286.Va errno 287is set to indicate the error. 288.Sh ERRORS 289The 290.Fn wait 291function 292will fail and return immediately if: 293.Bl -tag -width Er 294.It Bq Er ECHILD 295The calling process has no existing unwaited-for 296child processes. 297.It Bq Er ECHILD 298No status from the terminated child process is available 299because the calling process has asked the system to discard 300such status by ignoring the signal 301.Dv SIGCHLD 302or setting the flag 303.Dv SA_NOCLDWAIT 304for that signal. 305.It Bq Er EFAULT 306The 307.Fa status 308or 309.Fa rusage 310argument points to an illegal address. 311(May not be detected before exit of a child process.) 312.It Bq Er EINTR 313The call was interrupted by a caught signal, 314or the signal did not have the 315.Dv SA_RESTART 316flag set. 317.El 318.Sh SEE ALSO 319.Xr _exit 2 , 320.Xr ptrace 2 , 321.Xr sigaction 2 , 322.Xr exit 3 , 323.Xr siginfo 3 324.Sh STANDARDS 325The 326.Fn wait 327and 328.Fn waitpid 329functions are defined by POSIX; 330.Fn wait4 331and 332.Fn wait3 333are not specified by POSIX. 334The 335.Fn WCOREDUMP 336macro 337and the ability to restart a pending 338.Fn wait 339call are extensions to the POSIX interface. 340.Sh HISTORY 341The 342.Fn wait 343function appeared in 344.At v6 . 345