Forking\Fork

An execution context that uses forked processes.

Implements
Process
Strand

As forked processes are created with the pcntl_fork() function, the PCNTL extension must be enabled to spawn forks. Not compatible with Windows.

enabled()

static Fork::enabled(): bool

Checks if forking is enabled.

Return value

True if forking is enabled, otherwise false.

spawn()

static Fork::spawn(
    callable(...$args): mixed $function,
    ...$args
): Fork

Spawns a new forked process and immediately starts it. All arguments following the function to invoke in the fork will be copied and passed as parameters to the function to invoke.

Parameters

callable(...$args): mixed $function
The function to invoke inside the forked process.
mixed ...$args
Arguments to pass to $function.

Return value

The fork context that was spawned.

Throws

Icicle\Exception\UnsupportedError
Thrown if the pcntl extension is not loaded.

__construct()

new Fork(
    callable(...$args): mixed $function,
    ...$args
)

Creates a new fork context. The forked process is not actually spawned until you call start().

Parameters

callable(...$args): mixed $function
The function to invoke inside the forked process.
mixed ...$args
Arguments to pass to $function.

Throws

Icicle\Exception\UnsupportedError
Thrown if the pcntl extension is not loaded.

getPid()

Fork::getPid(): int

Gets the forked process's process ID.

getPriority()

Fork::getPriority(): float

Gets the fork's scheduling priority as a percentage.

The priority is a float between 0 and 1 that indicates the relative priority for the forked process, where 0 is very low priority, 1 is very high priority, and 0.5 is considered a "normal" priority. The value is based on the forked process's "nice" value. The priority affects the operating system's scheduling of processes. How much the priority actually affects the amount of CPU time the process gets is ultimately system-specific.

See also: getpriority(2)

setPriority()

Fork::setPriority(float $priority)

Sets the fork's scheduling priority as a percentage.

Parameters

int $priority
A value between 0 and 1 indicating the relative priority to set.

Note

On many systems, only the superuser can increase the priority of a process.

Next: Process\ChannelledProcess