Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

waitpid

Waits for a specific child process to change state.

Suspends the current process until the child identified by $pid terminates (or changes state, depending on flags). Sets $? to the wait status.

Returns:

  • The PID of the terminated child on success
  • 0 if WNOHANG was specified and no child has exited yet
  • -1 on error (e.g. no such child)

When $pid is -1, waits for any child (equivalent to wait()).

Synopsis

my $result = waitpid($pid, 0);        # blocking wait
my $result = waitpid($pid, WNOHANG);  # non-blocking
my $result = waitpid(-1, 0);           # any child (like wait)

See Also

wait, fork, POSIX