|
@@ -62,13 +62,15 @@ class util
|
|
|
else
|
|
|
{
|
|
|
$ret = pcntl_waitpid($pid,$status,WNOHANG);
|
|
|
- Log::record("pcntl_waitpid pid = {$pid}",Log::DEBUG);
|
|
|
+ Log::record("pcntl_waitpid pid = {$pid},ret={$ret}",Log::DEBUG);
|
|
|
|
|
|
if($ret == 0) {
|
|
|
Log::record("fork_child: successful ret == 0 PID: {$pid}",Log::DEBUG);
|
|
|
+ return $pid;
|
|
|
}
|
|
|
elseif($ret == -1) {
|
|
|
Log::record("fork_child: ret == -1 PID: {$pid}",Log::DEBUG);
|
|
|
+
|
|
|
}
|
|
|
else {
|
|
|
Log::record("fork_child: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
|
|
@@ -76,14 +78,8 @@ class util
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- static function fork_listen($host, $port, $callback, $count)
|
|
|
+ static function fork_listen($fd, $callback, $count)
|
|
|
{
|
|
|
- $fd = util::listen($host,$port);
|
|
|
- if($fd === false) {
|
|
|
- Log::record("cannot open listen socket = {$host}:{$port}",Log::DEBUG);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
$params = [$fd];
|
|
|
$count = intval($count);
|
|
|
if($count <= 0) {
|
|
@@ -91,11 +87,62 @@ class util
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ $childs = [];
|
|
|
while ($count--) {
|
|
|
- util::fork_child($callback,$params);
|
|
|
+ $childs[] = util::fork_child($callback,$params);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $childs;
|
|
|
+ }
|
|
|
+
|
|
|
+ static function fork_listenex($fd, $callback, $count)
|
|
|
+ {
|
|
|
+ self::fork_listen($fd,$callback,$count);
|
|
|
+ while ($count) {
|
|
|
+ Log::record("waitting child quit......",Log::DEBUG);
|
|
|
+ $ret = pcntl_wait($status);
|
|
|
+ Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
|
|
|
+ if($ret > 0)
|
|
|
+ {
|
|
|
+ if(pcntl_wifexited($status)) {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ } else {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ }
|
|
|
+ self::fork_listen($fd,$callback,1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //子进程已经退出干净了.
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static function join()
|
|
|
+ {
|
|
|
+ do {
|
|
|
+ Log::record("waitting child quit......",Log::DEBUG);
|
|
|
+ $ret = pcntl_wait($status);
|
|
|
+ Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
|
|
|
+ if($ret > 0)
|
|
|
+ {
|
|
|
+ if(pcntl_wifexited($status)) {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ } else {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //子进程已经退出干净了.
|
|
|
+ break;
|
|
|
}
|
|
|
- socket_close($fd);
|
|
|
}
|
|
|
+ while(true);
|
|
|
}
|
|
|
|
|
|
static function fork_worker($callback, $count)
|
|
@@ -111,4 +158,29 @@ class util
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ static function fork_workerex($callback, $count)
|
|
|
+ {
|
|
|
+ self::fork_worker($callback,$count);
|
|
|
+ while ($count) {
|
|
|
+ Log::record("waitting child quit......",Log::DEBUG);
|
|
|
+ $ret = pcntl_wait($status);
|
|
|
+ Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
|
|
|
+ if($ret > 0)
|
|
|
+ {
|
|
|
+ if(pcntl_wifexited($status)) {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ } else {
|
|
|
+ $ret = pcntl_wexitstatus($status);
|
|
|
+ Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
|
|
|
+ }
|
|
|
+ self::fork_worker($callback,1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //子进程已经退出干净了.
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|