Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

wait, waitpid - wait for child process to stop or terminate

&pagelevel(4)&pagelevel

Syntax

#include <sys/wait.h>

Optional
#include <sys/types.h> (End)

pid_t wait (int *stat_loc);
pid_t waitpid (pid_t pid, int *stat_loc, int options);

Description

wait() and waitpid() allow the calling process to obtain status information on one of its child processes. If status information is available for two or more child processes, the order in which their status is reported is unspecified.

wait() suspends execution of the calling process until the exit status for one of its child processes is available, or until delivery of a signal whose action is either to execute a signalhandling function or SIG_DFL. If the status information is available before the call to wait(), the function will return immediately.

waitpid() behaves identically to the wait() function if the value of pid is (pid_t)-1 and the value of options is 0. Otherwise, its behavior is modified by the values of the pid and options arguments.

pid specifies a set of child processes for which status is requested. waitpid() will only return the status of a child process from this set:

  • If pid is equal to (pid_t)-1, the status is requested for any child process. In this respect, waitpid() is then equivalent to wait().
  • If pid is greater than 0, it specifies the process ID of a single child process for which the status is requested.
  • If pid is 0, the status is requested for any child process whose process group ID is equal to that of the calling process.
  • If pid is less than (pid_t)-1, the status is requested for any child process whose process group ID is equal to the absolute value of pid.

options is constructed from the bitwise-inclusive OR of zero or more of the following flags, which are defined in the header sys/wait.h.

WCONTINUED

waitpid() determines the status of a child process specified by pid which is continued and whose status has not been queried since being resumed after a job control stop.

WNOHANG

waitpid() will not suspend execution of the calling process if the status is not immediately available for one of the child processes specified by pid.

WUNTRACED

The status of any child processes specified by pid that are stopped, and whose status has not yet been returned since they stopped, will also be reported to the calling process.

If wait() or waitpid() returns because the status of a child process is available, the return value of these functions will be the process ID of the child process. In this case, if the value of stat_loc is not a null pointer, the status information will be stored in the location pointed to by stat_loc.

If the status returned is from a terminated child process that returned the value 0 from main() or passed 0 as the status argument to _exit() or exit() , the value stored at the address pointed to by stat_loc will be 0. Regardless of its value, this information may be interpreted using the following macros, which are defined in sys/wait.h and evaluate to integral expressions; the stat_val argument is the integer value pointed to by stat_loc.

WIFEXITED( stat_val )

Evaluates to a non-zero value (true in C) if the status was returned for a child process that terminated normally.

WEXITSTATUS( stat_val )

If the value of WIFEXITED( stat_val ) is non-zero, this macro evaluates to the low-order 8 bits of the exit status that the child process passed to _exit() or exit(), or the value the child process returned from main().

WIFSIGNALED( stat_val )

Evaluates to non-zero value if the status was returned for a child process
that terminated due to the receipt of a signal that was not caught (see also signal.h).

WTERMSIG( stat_val )

If the value of WIFSIGNALED( stat_val ) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.

WIFSTOPPED( stat_val )

Evaluates to a non-zero value if the status was returned for a child process that is currently stopped.

WSTOPSIG( stat_val )

If the value of WIFSTOPPED( stat_val ) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.

WIFCONTINUED( stat_val )

Calculates a non-zero value if the status for a child process that was resumed after a job control stop is returned.

If the status stored at the location stat_loc was stored there by a waitpid() call which:

  • specified the flag WUNTRACED but not the flag WCONTINUED:
    then precisely one of the macros WIFEXITED( *stat_loc ), WIFSIGNALED( *stat_loc ) or WIFSTOPPED( *stat_loc ) returns a non-zero value.

  • specified the flags WUNTRACED and WCONTINUED:
    then precisely one of the macros WIFEXITED( *stat_loc ), WIFSIGNALED( *stat_loc ) and FSTOPPED( *stat_loc ) or WIFCONTINUED( *stat_loc ) returns a non-zero value.

  • specified neither the flag WUNTRACED nor the flag WCONTINUED, or was stored by a call of the wait() function:
    then precisely one of the macros WIFEXITED( *stat_loc ) or WIFSIGNALED( *stat_loc ) returns a non-zero value.

  • specified the flag WCONTINUED but not the flag WUNTRACED, or was stored by a call of the wait() function:
    precisely one of the macros WIFEXITED( *stat_loc ), WIFSIGNALED( *stat_loc ) or WIFCONTINUED( *stat_loc ) returns a non-zero value.

If a parent process terminates without waiting for all of its child processes to terminate, the remaining child processes will be assigned a new parent process ID, namely that of the system process init.

If threads are used, the wait() and waitpid() functions affect the process or a thread in the following manner: The calling thread is suspended until the status information is available.

Return val.

Process ID of the child process


if wait() or waitpid() returns because the status of a child process is available.

-1

if the wait() or waitpid() returns because a signal is delivered. errno is set to EINTR.

0

if waitpid() was invoked with the flag WNOHANG set in the options argument and the function has at least one child process specified by pid.

(pid_t)-1

if an error occurs. errno is set to indicate the error.

Errors

wait() will fail if:

 

ECHILD

The calling process has no existing unwaited-for child processes.

 

EINTR

The function was interrupted by a signal. The value of the object pointed to by stat_loc is undefined in this case.

 

waitpid() will fail if:

 

ECHILD

The process specified with pid or the process group does not exist or is not a child process of the calling process.

 

EINTR

The function was interrupted by a signal. The value of the object pointed to by stat_loc is undefined in this case.

 

EINVAL

options is not valid.

See also

exec, exit(), fork(), sys/types.h, sys/wait.h.