fork : add startDetachedChild
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4007 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
36
fork.php
36
fork.php
@@ -55,6 +55,42 @@ class fork
|
||||
exit;
|
||||
}
|
||||
|
||||
/** Create a detached child. The terminal is closed. All the displayed
|
||||
* messages from the child are silently dropped.
|
||||
* If some parameters are provided, the called child method will receive them
|
||||
* This function fork and return the child PID
|
||||
* @param callable $callable The callback method to use in child
|
||||
* @param mixed|null $params The params to provide to child method
|
||||
* @return The child PID
|
||||
*/
|
||||
public function startDetachedChild ($callable, $params = array ())
|
||||
{
|
||||
$pid = pcntl_fork ();
|
||||
if ($pid === -1)
|
||||
throw new \Exception ("Can't fork the child", 500);
|
||||
elseif ($pid)
|
||||
{
|
||||
// The parent
|
||||
$this->pidList[$pid] = $pid;
|
||||
return $pid;
|
||||
}
|
||||
// Call the child method
|
||||
$sid = posix_setsid();
|
||||
|
||||
// Will catch all the text messages from the application to not crash if
|
||||
// there is an "echo"
|
||||
ob_start ();
|
||||
|
||||
// Close the file handlers STDOUT/STDIN
|
||||
fclose (STDIN);
|
||||
fclose (STDOUT);
|
||||
fclose (STDERR);
|
||||
$args = func_get_args ();
|
||||
unset ($args[0]);
|
||||
call_user_func_array ($callable, $args);
|
||||
exit;
|
||||
}
|
||||
|
||||
/** Wait the end of one child
|
||||
* Return the PID of the dead child
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user