ForkManager

Class
Final: Yes

Coordinates the lifecycle of worker processes using process forking.

Description

This class acts as the central orchestrator responsible for:

  • Creating worker processes
  • Managing their lifecycle and state
  • Handling inter-process communication via streams
  • Propagating and handling system signals

Implementations MUST ensure that process control functions are available. The manager SHALL operate as a master process and MUST NOT be reused inside worker processes.

Table of Contents

Interfaces

ForkManagerInterface

Implementations of this interface MUST provide the orchestration layer responsible for spawning, supervising, signaling, and synchronizing worker processes.

Constants

INTERRUPTED_SYSTEM_CALL_ERROR

POSIX error code for interrupted system calls.

 = 4
NO_CHILD_PROCESS_ERROR

POSIX error code for missing child processes.

 = 10

Properties

 : LoggerInterface|null
$masterPid

PID of the master process that instantiated this manager.

 : int
$statesByPid

Stores runtime state objects associated with each worker.

 : array<int, WorkerState>
$workersByPid

Stores all worker instances indexed by their process ID (PID).

 : array<int, Worker>

Methods

__construct()

Initializes the manager, validates environment support, and registers signal handlers.

 : mixed
fork()

Forks one or more worker processes.

 : WorkerGroupInterface
getMasterPid()

Retrieves the PID of the master process.

 : int
isMaster()

Determines whether the current process is the master process.

 : bool
isSupported()

Determines whether the current environment supports process forking.

 : bool
isWorker()

Determines whether the current process is a worker process.

 : bool
kill()

Sends a signal to one or more workers.

 : void
wait()

Waits for one or more workers or worker groups to finish execution.

 : void
assertWorkerBelongsToManager()

Ensures that a worker belongs to this manager.

 : void
collectExitedWorkers()

Collects terminated child processes.

 : void
detectPid()

Detects the current process ID (PID).

 : int
drainRunningWorkerOutputs()

Drains output buffers from all running workers.

 : void
hasRunningTargets()

Checks if any target worker is still running.

 : bool
isInterruptedWait()

Determines if a wait error was caused by an interrupted system call.

 : bool
isNoChildError()

Determines if there are no remaining child processes.

 : bool
killWorkers()

Sends a signal to all provided workers.

 : void
registerSignalHandlers()

Registers signal handlers for all configured signals.

 : void
resolveWorkers()

Resolves worker and group inputs into a unique set of workers.

 : array<int, Worker>
waitOnWorkers()

Waits for the provided workers and processes their output streams.

 : void
Constants

Constants

INTERRUPTED_SYSTEM_CALL_ERROR

Private

POSIX error code for interrupted system calls.

private mixed INTERRUPTED_SYSTEM_CALL_ERROR = 4

NO_CHILD_PROCESS_ERROR

Private

POSIX error code for missing child processes.

private mixed NO_CHILD_PROCESS_ERROR = 10
Properties

$logger

Private Read-only
private LoggerInterface|null $logger = null

$masterPid

Private Read-only

PID of the master process that instantiated this manager.

private int $masterPid

Description

This value MUST remain immutable after initialization.

$statesByPid

Private

Stores runtime state objects associated with each worker.

private array<int, WorkerState> $statesByPid = []

Description

Each state instance SHALL reflect the current lifecycle status of a worker.

$workersByPid

Private

Stores all worker instances indexed by their process ID (PID).

private array<int, Worker> $workersByPid = []

Description

Each worker instance MUST be uniquely identifiable by its PID.

Methods

__construct()

Public

Initializes the manager, validates environment support, and registers signal handlers.

public __construct([SignalHandlerInterface  $signalHandler = new DefaultSignalHandler()][, LoggerInterface|null  $logger = null]) : mixed

Description

The implementation MUST verify that all required extensions are available. If the environment does not support forking, a RuntimeException MUST be thrown.

Parameters
$signalHandler : SignalHandlerInterface = new DefaultSignalHandler()

Description

handler responsible for reacting to system signals

$logger : LoggerInterface|null = null

Description

logger used for lifecycle and output events

Tags
throws

Description

if the environment does not support process forking

fork()

Public

Forks one or more worker processes.

public fork( callable  $workerCallback[, int  $workerCount = 1]) : WorkerGroupInterface

Description

The manager MUST NOT allow forking from within a worker process. The number of workers MUST be greater than zero.

In case of failure during creation, all previously created workers SHALL be terminated.

Parameters
$workerCallback : callable

Description

callback executed inside each worker process

$workerCount : int = 1

Description

number of workers to spawn

Tags
throws

Description

if called from a worker process

throws

Description

if worker count is invalid

throws

Description

if worker creation fails

Return values

Description

a group containing all created workers

getMasterPid()

Public

Retrieves the PID of the master process.

public getMasterPid() : int
Return values
int

Description

the master process identifier

isMaster()

Public

Determines whether the current process is the master process.

public isMaster() : bool
Return values
bool

Description

true if master; otherwise false

isSupported()

Public

Determines whether the current environment supports process forking.

public isSupported() : bool

Description

All required PHP functions MUST be available for safe execution.

Return values
bool

Description

true if forking is supported; otherwise false

isWorker()

Public

Determines whether the current process is a worker process.

public isWorker() : bool
Return values
bool

Description

true if worker; otherwise false

assertWorkerBelongsToManager()

Private

Ensures that a worker belongs to this manager.

private assertWorkerBelongsToManager(WorkerInterface  $worker) : void

Description

The method MUST reject unsupported implementations and foreign workers.

Parameters
$worker : WorkerInterface

Description

worker to validate

Tags
throws

Description

if validation fails

collectExitedWorkers()

Private

Collects terminated child processes.

private collectExitedWorkers() : void

Description

The implementation MUST handle interrupted system calls and absence of child processes.

Tags
throws

Description

if waiting fails unexpectedly

detectPid()

Private

Detects the current process ID (PID).

private detectPid() : int

Description

The implementation SHOULD prefer POSIX functions when available. If detection fails, a RuntimeException MUST be thrown.

Tags
throws

Description

if the PID cannot be determined

Return values
int

Description

the current process identifier

drainRunningWorkerOutputs()

Private

Drains output buffers from all running workers.

private drainRunningWorkerOutputs() : void

Description

This method SHOULD be called periodically to avoid blocking pipes.

hasRunningTargets()

Private

Checks if any target worker is still running.

private hasRunningTargets( array<int, true>  $targetPids) : bool
Parameters
$targetPids : array<int, true>

Description

target process IDs

Return values
bool

Description

true if at least one worker is running

isInterruptedWait()

Private

Determines if a wait error was caused by an interrupted system call.

private isInterruptedWait( int  $error) : bool
Parameters
$error : int

Description

error code

Return values
bool

Description

true if interrupted; otherwise false

isNoChildError()

Private

Determines if there are no remaining child processes.

private isNoChildError( int  $error) : bool
Parameters
$error : int

Description

error code

Return values
bool

Description

true if no child processes remain

killWorkers()

Private

Sends a signal to all provided workers.

private killWorkers( array<int, Worker$workers, Signal  $signal) : void
Parameters
$workers : array<int, Worker>

Description

workers to signal

$signal : Signal

Description

signal to send

registerSignalHandlers()

Private

Registers signal handlers for all configured signals.

private registerSignalHandlers() : void

Description

The implementation MUST enable asynchronous signal handling.

waitOnWorkers()

Private

Waits for the provided workers and processes their output streams.

private waitOnWorkers( array<int, Worker$workers) : void

Description

The method SHALL continuously monitor worker state and output until all workers terminate.

Parameters
$workers : array<int, Worker>

Description

workers to monitor