Source for file simple_controller.php
Documentation is available at simple_controller.php
* PHP_Fork class usage examples
* ==================================================================================
* NOTE: In real world you surely want to keep each class into
* a separate file, then include() it into your application.
* For this examples is more useful to keep all_code_into_one_file,
* so that each example shows a unique feature of the PHP_Fork framework.
* ==================================================================================
* This example shows how to attach a controller to started pseudo-threads
* Forked processes can sometime die or defunct for many reasons...
* Without a controller process that periodically check thread status we
* can not ensure that started threads are still alive into system
* Here we have n executeThreads and a controllerThread that check all them
* when the controller detect that a thread is died it try to respawn it.
* ATTENTION: this example fails on Cygwin platform, probably due to some bugs
* into ipc implementations that prevent the shared memory to be totally released
* ==================================================================================
require_once ("PHP/Fork.php");
// number of executeThreads we want
// max delay (seconds) that controller can accept from child's ping
// controller will check threads status every CTRL_POLLING_INTERVAL secs.
define ("CTRL_POLLING_INTERVAL", 5 );
// The same as previos examples, with the add of the controller ping...
// setVariable is a method inherited from PHP_Fork super class
// it sets a variable that can be accessed thru its name
$this->setVariable ('counter', $this->counter++ );
* While using the controller, every child must notify to controller that it's alive
* The frequency of this notify must be set according to the parameter $maxIdleTime
* (controllerThread constructor, default is 60 sec.)
* Typically executeThreads runs an infinite loop making something useful; you should
* calculate a reasonable time for one loop, then set the controller's $maxIdleTime
* Into the while(true) { } loop of the thread insert a call to the "setAlive" method
* so that controller can detect properly the status of child processes.
// parent process can call this facility method
// in order to get back the actual value of the counter
return $this->getVariable ('counter');
* controllerThread::controllerThread()
* @param $executeThreadPool
function controllerThread($name, &$executeThreadPool, $maxIdleTime = 60 , $interval = 60 )
$this->_sleepInt = $interval;
$this->_executeThreadPool = &$executeThreadPool;
$this->_maxIdleTime = $maxIdleTime;
$this->_respawnThread = array ();
$this->setVariable ('_executeThreadPool', $this->_executeThreadPool);
$this->_detectDeadChilds ();
$this->_respawnDeadChilds ();
foreach($this->_executeThreadPool as $thread) {
echo "Stopped " . $thread->getName () . "\n";
unset ($this->_executeThreadPool);
$this->_executeThreadPool = array ();
$this->setVariable ('_executeThreadPool', $this->_executeThreadPool);
return $this->getVariable ('_executeThreadPool');
function _detectDeadChilds ()
// check every executethread to see if it is alive...
foreach ($this->_executeThreadPool as $idx => $executeThread) {
if ($executeThread->getLastAlive () > $this->_maxIdleTime) {
// this thread is not responding, probably [defunct]
$threadName = $executeThread->getName ();
print time() . "-" . $this->getName () . "-" . $threadName . " seems to be died...\n";
// remove this thread from the pool
// and add them to the "to be respawned" thread list...
$this->_respawnThread[] = $threadName;
function _respawnDeadChilds ()
foreach ($this->_respawnThread as $idx => $threadName) {
// usually we try to start a Thread without this check
// if Shared Memory Area is not ready, the start() method
// die, so the process is destroyed. When respawing a dead child
// this is not useful, because die() will cause the controller itself
// So let's check if IPC is ok before call the start() method.
$this->_executeThreadPool[] = &$n;
print time() . "-" . $this->getName () . "- New instance of " . $threadName . " successfully spawned (PID=" . $n->getPid () . ")\n";
$this->setVariable ('_executeThreadPool', $this->_executeThreadPool);
print "Unable to create IPC segment...\n";
* Functions used by the console
$fp = fopen("php://stdin", "r");
$input = fgets($fp, 255 );
* Main program. Bring up two instances of the executeThread class that
* runs concurrently. It's a multi-thread app with a few lines of code!!!
* executeThread does nothing interesting, it simply has a counter and increment
* this counter each second... (see class definition at top of this file)
$executeThread[$i]->start ();
echo "Started " . $executeThread[$i]->getName () . " with PID " . $executeThread[$i]->getPid () . "...\n";
print "Started " . $ctrl->getName () . " with PID " . $ctrl->getPid () . "...\n\n";
echo "This is the main process.\nPress [X] to terminate, [G] to read thread's counter, [T] to obtain the actual executeThreads dump.\nTo test the controller respawn functionality, simply kill one (or more) executeThread...\n";
* Console simple listener
// when using the controller we cannot traverse the $executeThread array
// because it only contains the original pool of thread.
// if a thread dies, the controller respawn it and updates it's own
// thread pool. The method stopAllThreads() stops all running threads.
// stop the controller itself
print "Stopped " . $ctrl->getName () . "\n";
echo $executeThread[$i]->getName () . " returns " . $executeThread[$i]->getCounter () . "\n";
print ($th->getCounter (). "\n");
Documentation generated on Mon, 11 Mar 2019 15:41:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|