ZendX_Console_Process_Unix allows you to spawn a class as a separated process

category ZendX
package ZendX_Console
copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
license New BSD License

 Methods

Constructor method

__construct(integer $puid = null, integer $guid = null, integer $umask = null

Allocates a new pseudo-thread object. Optionally, set a PUID, a GUID and a UMASK for the child process. This also initialize Shared Memory Segments for process communications.

Parameters

$puid

integer

$guid

integer

$umask

integer

Exceptions

\ZendX_Console_Process_Exception When running on windows
\ZendX_Console_Process_Exception When running in web enviroment
\ZendX_Console_Process_Exception When shmop_* functions don't exist
\ZendX_Console_Process_Exception When pcntl_* functions don't exist
\ZendX_Console_Process_Exception When posix_* functions don't exist

Stop the child on destruction

__destruct() 

Read the time elapsed since the last child setAlive() call.

getLastAlive() : integer

This method is useful because often we have a pseudo-thread pool and we need to know each pseudo-thread status. If the child executes the setAlive() method, the parent with getLastAlive() can know that child is alive.

Returns

integer

Returns the PID of the current pseudo-thread.

getPid() : integer

Returns

integer

Get a variable from the shared memory segment. Returns NULL if the variable doesn't exist.

getVariable(string $name) : mixed

Parameters

$name

string

Returns

mixed

Test if the pseudo-thread is already started.

isRunning() : boolean

Returns

boolean

Set a variable into the shared memory segment, so that it can accessed both from the parent and from the child process. Variable names beginning with underlines are only permitted to interal functions.

setVariable(string $name, mixed $value) : void

Parameters

$name

string

$value

mixed

Exceptions

\ZendX_Console_Process_Exception When an invalid variable name is supplied

Causes this pseudo-thread to begin parallel execution.

start() : void

This method first checks of all the Shared Memory Segment. If okay, it forks the child process, attaches signal handler and returns immediatly. The status is set to running, and a PID is assigned. The result is that two pseudo-threads are running concurrently: the current thread (which returns from the call to the start() method) and the other thread (which executes its run() method).

Exceptions

\ZendX_Console_Process_Exception When SHM segments can't be created
\ZendX_Console_Process_Exception When process forking fails

Causes the current thread to die.

stop() : boolean

The relative process is killed and disappears immediately from the processes list.

Returns

boolean

This is called from within the parent; all the communication stuff is done here.

_callCallbackMethod(string $methodName, array $argList = array(), string $type = self::VOID_METHOD) : mixed

Parameters

$methodName

string

$argList

array

$type

string

Returns

mixed

This method actually implements the pseudo-thread logic.

_run() : void

Set a pseudo-thread property that can be read from parent process in order to know the child activity.

_setAlive() : void

Practical usage requires that child process calls this method at regular time intervals; parent will use the getLastAlive() method to know the elapsed time since the last pseudo-thread life signals...

Destroy thread context and free relative resources.

_cleanProcessContext() : void

Create an IPC segment

_createIpcSegment() : boolean

Exceptions

\ZendX_Console_Process_Exception When SHM segment can't be created

Returns

boolean

Create IPC semaphore

_createIpcSemaphore() : boolean

Exceptions

\ZendX_Console_Process_Exception When semaphore can't be created

Returns

boolean

Read data from IPC segment

_readFromIpcSegment() : void

Exceptions

\ZendX_Console_Process_Exception When writing of SHM segment fails

Sends signal to the child process

_sendSigUsr1() : void

This is the signal handler that makes the communications between client and server possible.

_sigHandler(integer $signo) : void

Parameters

$signo

integer

Wait for IPC Semaphore

_waitForIpcSemaphore() : void

Write data to IPC segment

_writeToIpcSegment() : void

Exceptions

\ZendX_Console_Process_Exception When writing of SHM segment fails

Acutally Write a variable to the shared memory segment

_writeVariable(string $name, mixed $value) : void

Parameters

$name

string

$value

mixed

 Properties

 

GUID of the child process owner

$_guid : integer

Default

null
 

A data structure to hold data for Inter Process Communications

$_internalIpcData : array

Default

array()
 

Key to access to Shared Memory Area.

$_internalIpcKey : integer

Default

 

Key to access to Sync Semaphore.

$_internalSemKey : integer

Default

 

Is Shared Memory Area OK? If not, the start() method will block.

$_ipcIsOkay : boolean

Default

Otherwise we'll have a running child without any communication channel.

 

Filename of the IPC segment file

$_ipcSegFile : string

Default

 

Filename of the semaphor file

$_ipcSemFile : string

Default

 

Wether we are into child process or not

$_isChild : boolean

Default

false
 

Whether the process is yet forked or not

$_isRunning : boolean

Default

false
 

Unique thread name

$_name : string

Default

 

PID of the child process

$_pid : integer

Default

null
 

UID of the child process owner

$_puid : integer

Default

null

 Constants

 

Return method

RETURN_METHOD = 'void_method' 
 

Void method

VOID_METHOD = 'void_method'