2012-07-05 76 views
0

因为我没有在下面的代码中等待子进程,但运行后没有僵尸进程,为什么?根据我的理解,在这种情况下生成的僵尸进程:父进程退出时没有为其子进程调用'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); 
?> 

回答

1

他们只是僵尸进程,直到他们的父母收获他们或死亡。一旦它们被重新设置为init,它们就会被收割,因此不再存在。

+0

父母死亡也会导致孩子死亡? – bourneli 2012-07-25 04:22:56

+0

不会。它会导致现在的**孤儿**进程被重新设置为'init'。 – 2012-07-25 04:23:33