由于我在下面的代码中没有等待children进程,但是运行后没有僵尸进程,这是为什么呢?
在我的理解中,僵尸进程是在这种情况下产生的:父进程退出而没有为其子进程调用'wait'或'waitpid',所以系统进程init会拾取这些子进程,称为僵尸进程,不是吗?
<?php
define("PC", 10); // Process Count
if (!function_exists('posix_mkfifo')) {
die("posix_mkfifo not existing\n");
}
if (!function_exists('pcntl_fork')) {
die("pcntl_fork not existing");
}
// create pipe
$sPipePath = "my_pipe.".posix_getpid();
if (!posix_mkfifo($sPipePath, 0666)) {
die("create pipe {$sPipePath} error");
}
$arrPID = array();
for ($i = 0; $i < PC; ++$i ) {
$nPID = pcntl_fork(); // create a child process
if ($nPID == 0) {
// children
$oW = fopen($sPipePath, 'w');
fwrite($oW, $i."\n");
fclose($oW);
exit(0); // child end
}elseif($nPID > 0) {
// parent
//array_push($arrPID, $nPID);
//while($pid = pcntl_wait($nStatus, WNOHANG OR WUNTRACED) > 0) {
//echo "$pid exit\n";
//}
}
}
// wait for all child process
//foreach($arrPID as $nPID){
//echo "$nPID\n";
//pcntl_waitpid($nPID, $nStatus); // cause process undead
//pcntl_wait($nStatus);
//}
// parent
$oR = fopen($sPipePath, 'r');
$sData = '';
while(!feof($oR)) {
$sData .= fread($oR, 1024);
}
fclose($oR);
unlink($sPipePath); // remove pipe
//check result
$arr2 = explode("\n", $sData);
$map = array();
foreach($arr2 as $i) {
if (is_numeric(trim($i))) {
$map[trim($i)] = '';
}
}
echo count($map) == PC ? 'ok' : "error count " . count($map);
?>