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

pcntl_rfork

Description

The pcntl_rfork of PCNTL for PHP manipulates process resources.

Only available on BSD systems.

Syntax

pcntl_rfork(
    int $flags,
    int $signal = 0
): int

Parameters

flags

The flags parameter determines which resources of the invoking process (parent) are shared by the new process (child) or initialized to their default values.

flags is the logical OR of some subset of:

ContstantDescription
RFPROCIf set a new process is created; otherwise changes affect the current process.
RFNOWAITIf set, the child process will be dissociated from the parent. Upon exit the child will not leave a status for the parent to collect.
RFFDGIf set, the invoker's file descriptor table is copied; otherwise the two processes share a single table.
RFCFDGIf set, the new process starts with a clean file descriptor table. Is mutually exclusive with RFFDG.
RFLINUXTHPNIf set, the kernel will return SIGUSR1 instead of SIGCHILD upon thread exit for the child. This is intended to do Linux clone exit parent notification.

signal

The signal number.

Return

On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution.

On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised.

Examples

1 · flags

<?

$flags = RFNOWAIT | RFTSIGZMB;

$return = pcntl_rfork($flags);

echo $return;

2 · signal

<?

$flags = RFNOWAIT | RFTSIGZMB;
$signal = SIGUSR1;

$return = pcntl_rfork($flags, $signal);

echo $return;

3 · parent child

<?

$flags = RFNOWAIT | RFTSIGZMB;
$signal = SIGUSR1;

$return = pcntl_rfork($flags, $signal);

if($return > 0)
{
    echo "parent: $return";
}
else
{
    echo "child: $return";

    sleep(2);//as the child does not wait, so we see its pid
}