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