1*66936Sbostic.\" Copyright (c) 1980, 1991, 1993, 1994 261186Sbostic.\" The Regents of the University of California. All rights reserved. 320101Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520101Smckusick.\" 6*66936Sbostic.\" @(#)wait.2 8.2 (Berkeley) 04/19/94 747208Scael.\" 847208Scael.Dd 947208Scael.Dt WAIT 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm wait , 1347208Scael.Nm waitpid , 1447208Scael.Nm wait4 , 1547208Scael.Nm wait3 16*66936Sbostic.Nd wait for process termination 1747208Scael.Sh SYNOPSIS 1847208Scael.Fd #include <sys/types.h> 1947208Scael.Fd #include <sys/wait.h> 2047208Scael.Ft pid_t 2147208Scael.Fn wait "int *status" 2247208Scael.Fd #include <sys/time.h> 2347208Scael.Fd #include <sys/resource.h> 2447208Scael.Ft pid_t 2547208Scael.Fn waitpid "pid_t wpid" "int *status" "int options" 2647208Scael.Ft pid_t 2747208Scael.Fn wait3 "int *status" "int options" "struct rusage *rusage" 2847208Scael.Ft pid_t 2947208Scael.Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage" 3047208Scael.Sh DESCRIPTION 3147208ScaelThe 3247208Scael.Fn wait 3347208Scaelfunction suspends execution of its calling process until 3447208Scael.Fa status 3547208Scaelinformation is available for a terminated child process, 3647208Scaelor a signal is received. 3720102SmckusickOn return from a successful 3847208Scael.Fn wait 3947208Scaelcall, 4047208Scaelthe 4147208Scael.Fa status 4247208Scaelarea contains termination information about the process that exited 4338599Skarelsas defined below. 4447208Scael.Pp 4538599SkarelsThe 4647208Scael.Fn wait4 4738599Skarelscall provides a more general interface for programs 4847208Scaelthat need to wait for certain child processes, 49*66936Sbosticthat need resource utilization statistics accumulated by child processes, 5038599Skarelsor that require options. 5138599SkarelsThe other wait functions are implemented using 5247208Scael.Fn wait4 . 5347208Scael.Pp 5438599SkarelsThe 5547208Scael.Fa wpid 5638599Skarelsparameter specifies the set of child processes for which to wait. 5738599SkarelsIf 5847208Scael.Fa wpid 5947208Scaelis -1, the call waits for any child process. 6038599SkarelsIf 6147208Scael.Fa wpid 6238599Skarelsis 0, 6338599Skarelsthe call waits for any child process in the process group of the caller. 6438599SkarelsIf 6547208Scael.Fa wpid 6638599Skarelsis greater than zero, the call waits for the process with process id 6747208Scael.Fa wpid . 6838599SkarelsIf 6947208Scael.Fa wpid 7047208Scaelis less than -1, the call waits for any process whose process group id 7138599Skarelsequals the absolute value of 7247208Scael.Fa wpid . 7347208Scael.Pp 7438599SkarelsThe 7547208Scael.Fa status 7638599Skarelsparameter is defined below. The 7747208Scael.Fa options 7838599Skarelsparameter contains the bitwise OR of any of the following options. 7947208ScaelThe 8047208Scael.Dv WNOHANG 8147208Scaeloption 8238599Skarelsis used to indicate that the call should not block if 8338599Skarelsthere are no processes that wish to report status. 8447208ScaelIf the 8547208Scael.Dv WUNTRACED 8647208Scaeloption is set, 8738599Skarelschildren of the current process that are stopped 8847208Scaeldue to a 8947208Scael.Dv SIGTTIN , SIGTTOU , SIGTSTP , 9047208Scaelor 9147208Scael.Dv SIGSTOP 9247208Scaelsignal also have 9338599Skarelstheir status reported. 9447208Scael.Pp 9538599SkarelsIf 9647208Scael.Fa rusage 9720102Smckusickis non-zero, a summary of the resources used by the terminated 9820102Smckusickprocess and all its 9920102Smckusickchildren is returned (this information is currently not available 10020102Smckusickfor stopped processes). 10147208Scael.Pp 10247208ScaelWhen the 10347208Scael.Dv WNOHANG 10447208Scaeloption is specified and no processes 10520102Smckusickwish to report status, 10647208Scael.Fn wait4 10720102Smckusickreturns a 10847208Scaelprocess id 10938599Skarelsof 0. 11047208Scael.Pp 11138599SkarelsThe 11247208Scael.Fn waitpid 11338599Skarelscall is identical to 11447208Scael.Fn wait4 11538599Skarelswith an 11647208Scael.Fa rusage 11738599Skarelsvalue of zero. 11838599SkarelsThe older 11947208Scael.Fn wait3 12038599Skarelscall is the same as 12147208Scael.Fn wait4 12238599Skarelswith a 12347208Scael.Fa wpid 12447208Scaelvalue of -1. 12547208Scael.Pp 12647208ScaelThe following macros may be used to test the manner of exit of the process. 12738599SkarelsOne of the first three macros will evaluate to a non-zero (true) value: 12847208Scael.Bl -tag -width Ds 12947208Scael.It Fn WIFEXITED status 13038599SkarelsTrue if the process terminated normally by a call to 13147208Scael.Xr _exit 2 13238599Skarelsor 13347208Scael.Xr exit 2 . 13447208Scael.It Fn WIFSIGNALED status 13538599SkarelsTrue if the process terminated due to receipt of a signal. 13647208Scael.It Fn WIFSTOPPED status 13738599SkarelsTrue if the process has not terminated, but has stopped and can be restarted. 13838599SkarelsThis macro can be true only if the wait call specified the 13947208Scael.Dv WUNTRACED 14038599Skarelsoption 14138599Skarelsor if the child process is being traced (see 14247208Scael.Xr ptrace 2 ) . 14347208Scael.El 14447208Scael.Pp 14538599SkarelsDepending on the values of those macros, the following macros 14638599Skarelsproduce the remaining status information about the child process: 14747208Scael.Bl -tag -width Ds 14847208Scael.It Fn WEXITSTATUS status 14947208ScaelIf 15047208Scael.Fn WIFEXITED status 15147208Scaelis true, evaluates to the low-order 8 bits 15238599Skarelsof the argument passed to 15347208Scael.Xr _exit 2 15438599Skarelsor 15547208Scael.Xr exit 2 15638599Skarelsby the child. 15747208Scael.It Fn WTERMSIG status 15847208ScaelIf 15947208Scael.Fn WIFSIGNALED status 16047208Scaelis true, evaluates to the number of the signal 16138599Skarelsthat caused the termination of the process. 16247208Scael.It Fn WCOREDUMP status 16347208ScaelIf 16447208Scael.Fn WIFSIGNALED status 16547208Scaelis true, evaluates as true if the termination 16638599Skarelsof the process was accompanied by the creation of a core file 16738599Skarelscontaining an image of the process when the signal was received. 16847208Scael.It Fn WSTOPSIG status 16947208ScaelIf 17047208Scael.Fn WIFSTOPPED status 17147208Scaelis true, evaluates to the number of the signal 17238599Skarelsthat caused the process to stop. 17347208Scael.El 17447208Scael.Sh NOTES 17520101SmckusickSee 17647208Scael.Xr sigaction 2 17738599Skarelsfor a list of termination signals. 17838599SkarelsA status of 0 indicates normal termination. 17947208Scael.Pp 18047208ScaelIf a parent process terminates without 18147208Scaelwaiting for all of its child processes to terminate, 18247208Scaelthe remaining child processes are assigned the parent 18347208Scaelprocess 1 ID (the init process ID). 18447208Scael.Pp 18538599SkarelsIf a signal is caught while any of the 18647208Scael.Fn wait 18738599Skarelscalls is pending, 18838599Skarelsthe call may be interrupted or restarted when the signal-catching routine 18938599Skarelsreturns, 19038599Skarelsdepending on the options in effect for the signal; 19138599Skarelssee 19247208Scael.Xr intro 2 , 19338599SkarelsSystem call restart. 19447208Scael.Sh RETURN VALUES 19547208ScaelIf 19647208Scael.Fn wait 19747208Scaelreturns due to a stopped 19820102Smckusickor terminated child process, the process ID of the child 19947208Scaelis returned to the calling process. Otherwise, a value of -1 20047208Scaelis returned and 20147208Scael.Va errno 20247208Scaelis set to indicate the error. 20347208Scael.Pp 20438599SkarelsIf 20547208Scael.Fn wait4 , 20647208Scael.Fn wait3 20738599Skarelsor 20847208Scael.Fn waitpid 20938599Skarelsreturns due to a stopped 21038599Skarelsor terminated child process, the process ID of the child 21138599Skarelsis returned to the calling process. 21238599SkarelsIf there are no children not previously awaited, 21347208Scael-1 is returned with 21447208Scael.Va errno 21547208Scaelset to 21647208Scael.Bq Er ECHILD . 21747208ScaelOtherwise, if 21847208Scael.Dv WNOHANG 21947208Scaelis specified and there are 22038599Skarelsno stopped or exited children, 22138599Skarels0 is returned. 22238599SkarelsIf an error is detected or a caught signal aborts the call, 22347208Scaela value of -1 22447208Scaelis returned and 22547208Scael.Va errno 22647208Scaelis set to indicate the error. 22747208Scael.Sh ERRORS 22847208Scael.Fn Wait 22947208Scaelwill fail and return immediately if: 23047208Scael.Bl -tag -width Er 23147208Scael.It Bq Er ECHILD 23220102SmckusickThe calling process has no existing unwaited-for 23320102Smckusickchild processes. 23447208Scael.It Bq Er EFAULT 23547208ScaelThe 23647208Scael.Fa status 23747208Scaelor 23847208Scael.Fa rusage 23947208Scaelarguments point to an illegal address. 24038599Skarels(May not be detected before exit of a child process.) 24147208Scael.It Bq Er EINTR 24238599SkarelsThe call was interrupted by a caught signal, 24347208Scaelor the signal did not have the 24447208Scael.Dv SA_RESTART 24547208Scaelflag set. 24647208Scael.El 24747208Scael.Sh STANDARDS 24838599SkarelsThe 24947208Scael.Fn wait 25038599Skarelsand 25147208Scael.Fn waitpid 25238599Skarelsfunctions are defined by POSIX; 25347208Scael.Fn wait4 25438599Skarelsand 25547208Scael.Fn wait3 25638599Skarelsare not specified by POSIX. 25747208ScaelThe 25847208Scael.Fn WCOREDUMP 25947208Scaelmacro 26038599Skarelsand the ability to restart a pending 26147208Scael.Fn wait 26238599Skarelscall are extensions to the POSIX interface. 26347208Scael.Sh SEE ALSO 26447208Scael.Xr exit 2 , 26547208Scael.Xr sigaction 2 26647208Scael.Sh HISTORY 26747208ScaelA 26847208Scael.Nm 26947208Scaelfunction call appeared in Version 6 AT&T UNIX. 270