From 8822ec580e56f98aeef331850670855f42ffd570 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 22 Dec 2017 12:37:23 +0000 Subject: [PATCH] fork : add startDetachedChild git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4007 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- fork.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/fork.php b/fork.php index c9e6d9f..2c45f23 100644 --- a/fork.php +++ b/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 */