孩子只有在结束时才会变成僵尸,只要它自己还活着,父母就不会打电话。wait*()
在父进程也结束的那一刻,子进程被init
负责调用wait*()
子进程的进程收割,所以它最终会结束并离开僵尸状态并从进程列表中消失。
要使您的示例代码中创建的孩子成为僵尸,请修改代码,例如:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t p = fork();
if (p != 0)
{
waitpid(p, NULL, 0); /* See if the child already had ended. */
sleep(1); /* Wait 1 seconds for the child to end. And eat away the SIGCHLD in case if arrived. */
pause(); /* Suspend main task. */
}
else
{
sleep(3); /* Just let the child live for some tme before becoming a zombie. */
}
return 0;
}
由于以下两个事实:
- 孩子睡了 3 秒,所以父母的电话
waitpid()
很可能总是失败
- 的默认处理
SIGCHLD
是忽略它。
上面的代码实际上是一样的:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t p = fork();
if (p != 0)
{
pause(); /* Suspend main task. */
}
else
{
sleep(3); /* Just let the child live for some tme before becoming a zombie. */
}
return 0;
}