Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

pcntl_waitpid

Description

The pcntl_waitpid of PCNTL for PHP waits on or returns the status of a forked child.

Syntax

pcntl_waitpid(
    int $process_id,
    int &$status,
    int $flags = 0,
    array &$resource_usage = []
): int

Parameters

process_id

The value of process_id can be one of the following:

ValueDescription
< -1wait for any child process whose process group ID is equal to the absolute value of process_id.
-1wait for any child process; this is the same behaviour that the wait function exhibits.
0wait for any child process whose process group ID is equal to that of the calling process.
> 0wait for the child whose process ID is equal to the value of process_id.

NOTE: Specifying -1 as the process_id is equivalent to the functionality pcntl_wait() provides (minus flags).

status

pcntl_waitpid() will store status information in the status parameter which can be evaluated using the following functions: pcntl_wifexited(), pcntl_wifstopped(), pcntl_wifsignaled(), pcntl_wexitstatus(), pcntl_wtermsig() and pcntl_wstopsig().

flags

The value of flags is the value of zero or more of the following two global constants OR'ed together:

ValueContstantDescription
1WNOHANGReturn immediately if no child has exited.
2WUNTRACEDReturn for children which are stopped, and whose status has not been reported.

Return

Returns the process ID of the child which exited, -1 on error, or zero if WNOHANG was used and no child was available.

Examples

1 · process_id · < -1 · status

<?

$process_id = -2;

$return = pcntl_waitpid($process_id, $status);

var_dump($return, $status);

?>
int(-1)
int(0)

2 · process_id · -1 · status

<?

$process_id = -1;

$return = pcntl_waitpid($process_id, $status);

var_dump($return, $status);

?>
int(-1)
int(0)

3 · process_id · 0 · status

<?

$process_id = 0;

$return = pcntl_waitpid($process_id, $status);

var_dump($return, $status);

?>
int(-1)
int(0)

4 · process_id · > 0 · status

<?

$process_id = 1;

$return = pcntl_waitpid($process_id, $status);

var_dump($return, $status);

?>
int(-1)
int(0)

5 · flags · 0

<?

$process_id = pcntl_fork();
$flags = 0;

$return = pcntl_waitpid($process_id, $status, $flags);

var_dump($return, $status);

?>
int(-1)
int(0)

6 · flags · WNOHANG

<?

$process_id = pcntl_fork();
$flags = WNOHANG;

$return = pcntl_waitpid($process_id, $status, $flags);

var_dump($return, $status);

?>
int(0)
int(0)

7 · flags · WUNTRACED

<?

$process_id = pcntl_fork();
$flags = WUNTRACED;

$return = pcntl_waitpid($process_id, $status, $flags);

var_dump($return, $status);

?>
int(-1)
int(0)

8 · resource_usage

<?

$process_id = pcntl_fork();
$flags = WNOHANG;

$return = pcntl_waitpid($process_id, $status, $flags, $resource_usage);

var_dump($return, $status, $resource_usage);

?>
int(0)
int(0)
array(0) {
}

9 · parent child

<?

$process_id = pcntl_fork();

if($process_id == -1)
{
    die("pcntl_fork");
}
else if($process_id)
{
    echo "parent: $process_id";

    pcntl_waitpid($process_id, $status);//protect against zombie children
}
else
{
    echo "child: $process_id";
}

?>
child: 0
HomeMenu