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

pcntl_wait

Description

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

Syntax

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

Parameters

status

pcntl_wait() 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

If wait3 is available on your system (mostly BSD-style systems), you can provide the optional flags parameter. If this parameter is not provided, wait will be used for the system call. If wait3 is not available, providing a value for flags will have no effect. The value of flags is the value of zero or more of the following two 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 provided as an option (on wait3-available systems) and no child was available.

Examples

1 · staus

<?

$return = pcntl_wait($status);

var_dump($return, $status);

?>
int(-1)
int(0)

2 · flags · 0

<?

$flags = 0;

$return = pcntl_wait($status, $flags);

var_dump($return, $status);

?>
int(-1)
int(0)

3 · flags · WNOHANG

<?

$flags = WNOHANG;

$return = pcntl_wait($status, $flags);

var_dump($return, $status);

?>
int(-1)
int(0)

4 · flags · WUNTRACED

<?

$flags = WUNTRACED;

$return = pcntl_wait($status, $flags);

var_dump($return, $status);

?>
int(-1)
int(0)

5 · resource_usage

<?

$flags = WNOHANG;

$return = pcntl_wait($status, $flags, $resource_usage);

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

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

6 · parent child

<?

$process_id = pcntl_fork();

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

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

?>
child: 0
HomeMenu