【问题标题】:Why there is No Zombie process为什么没有僵尸进程
【发布时间】:2012-07-05 13:19:19
【问题描述】:

由于我在下面的代码中没有等待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);
?>

【问题讨论】:

    标签: php unix zombie-process


    【解决方案1】:

    它们只是僵尸进程,直到它们的父进程要么收割它们,要么死亡。一旦它们被重新定义为init,它们就会被收割,因此不再存在。

    【讨论】:

    • 父母死亡也会导致孩子死亡?
    • 没有。它将导致现在的 orphan 进程重新设置为 init
    【解决方案2】:

    你错误地理解了僵尸进程。

    流程

    1.每当任何子进程终止时,它都会向父进程发送一些信号, 然后父进程向操作系统发送请求以删除该子进程的进程表。

    2.假设你的子进程在你的父进程处于睡眠状态的同时终止。所以现在父进程没有收到任何信号。

    3.现在虽然子进程终止了,但它仍然存在并占用进程ID以及进程表中的空间。 现在你可以说你的子进程变成了僵尸。

    实际场景: 每个新进程都被分配了一个唯一的进程 ID。 如果存在许多僵尸进程,它将不允许您创建新进程,因为进程 ID 是有限的

    【讨论】:

      猜你喜欢
      • 2011-03-14
      • 2013-04-11
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多