[Previous] [Contents] [Index] [Next]

waitpid()

Suspend the calling process

Synopsis:

#include <sys/types.h>
#include <sys/wait.h>

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

Library:

libc

Description:

The waitpid() function suspends execution of the calling process until status information from one of its terminated child processes is available, or until the delivery of a signal whose action is either to terminate the process or execute a signal handler. If status information is available prior to the call to waitpid(), the return is immediate.

The waitpid() function behaves the same as the wait() function when passed a pid argument of -1, and the options argument has a value of zero.

The pid argument modifies the behavior of waitpid() in that it allows the calling process to specify a set of child processes for which status information is requested:

  1. If pid is equal to -1, status is requested for any child process.
  2. If pid is greater than zero, it specifies the process ID of a single child process for which status is requested.
  3. If pid is equal to zero, status is requested from any child process whose process group ID is equal to that of the calling process.
  4. If pid is less than -1, status is requested for any child process whose process group ID is equal to the absolute value of pid.

If the stat_loc variable is non-NULL, the terminating status of the child process is placed here. For information about macros that extract information from stat_loc, see "Status macros" in the description of wait().

The options argument is constructed from the bitwise inclusive OR of zero or more of the following flags:

WCONTINUED
Return the status for any child that was stopped and has been continued.
WEXITED
Wait for process(es) to exit.
WNOHANG
Return immediately if there are no children to wait for.
WNOWAIT
Keep the process in a waitable state. This doesn't affect the state of the process; the process may be waited for again after this call completion.
WSTOPPED
Wait for and return the process status of any child that has stopped because it received a signal.
WUNTRACED
Report status of stopped child process.

Only one of the WIFEXITED(stat_val) and WIFSIGNALED(stat_val) macros can evaluate to a nonzero value.

Returns:

The process ID of the terminating child process on success. If waitpid() is invoked with WNOHANG set in options, it has at least one child process specified by pid for which status isn't available, and status isn't available for any process specified by pid, a value of zero is returned. On delivery of a signal, waitpid() returns -1, and errno is set to EINTR.

Errors:

ECHILD
The calling process has no existing unwaited-for child processes that meet the criteria set by pid.
EINTR
The function was interrupted by a signal. The value of the location pointed to by stat_loc is undefined.
EINVAL
The value of the options argument isn't valid.

Classification:

POSIX 1003.1

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

errno, spawn(), wait(), wait4(), waitid()


[Previous] [Contents] [Index] [Next]