fork: Add name of the child process for "ps -edf"
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4008 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
18
fork.php
18
fork.php
@@ -14,6 +14,7 @@ class fork
|
||||
if (! function_exists ("pcntl_fork"))
|
||||
throw new \Exception ("Can't fork as PHP doesn't have the pcntl_fork",
|
||||
500);
|
||||
declare (ticks=1);
|
||||
}
|
||||
|
||||
/** Return the number of active PID
|
||||
@@ -59,11 +60,12 @@ class fork
|
||||
* 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 string $name The name displayed in the processus list
|
||||
* @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 ())
|
||||
public function startDetachedChild ($name, $callable, $params = array ())
|
||||
{
|
||||
$pid = pcntl_fork ();
|
||||
if ($pid === -1)
|
||||
@@ -85,6 +87,9 @@ class fork
|
||||
fclose (STDIN);
|
||||
fclose (STDOUT);
|
||||
fclose (STDERR);
|
||||
if (function_exists ("cli_set_process_title"))
|
||||
cli_set_process_title ($name);
|
||||
|
||||
$args = func_get_args ();
|
||||
unset ($args[0]);
|
||||
call_user_func_array ($callable, $args);
|
||||
@@ -119,6 +124,9 @@ class fork
|
||||
*/
|
||||
public function stopChild ($pid, $maxWait = null)
|
||||
{
|
||||
if (! key_exists ($pid, $this->pidList))
|
||||
throw new \Exception ("Can't stop pid '$pid' : not in the fork list",
|
||||
500);
|
||||
return $this->sendSignalToChild (SIGTERM, $pid, $maxWait);
|
||||
}
|
||||
|
||||
@@ -133,6 +141,9 @@ class fork
|
||||
*/
|
||||
public function killChild ($pid, $maxWait = null)
|
||||
{
|
||||
if (! key_exists ($pid, $this->pidList))
|
||||
throw new \Exception ("Can't kill pid '$pid' : not in the fork list",
|
||||
500);
|
||||
return $this->sendSignalToChild (SIGKILL, $pid, $maxWait);
|
||||
}
|
||||
|
||||
@@ -148,10 +159,7 @@ class fork
|
||||
*/
|
||||
private function sendSignalToChild ($signal, $pid, $maxWait = null)
|
||||
{
|
||||
if (! key_exists ($pid, $this->pidList))
|
||||
throw new \Exception ("Can't stop/kill pid '$pid' : not in the fork list",
|
||||
500);
|
||||
posix_kill ($pid, SIGKILL);
|
||||
posix_kill ($pid, $signal);
|
||||
if ($maxWait !== null)
|
||||
{
|
||||
$startWait = time ();
|
||||
|
||||
Reference in New Issue
Block a user