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