我正在尝试一些简单的代码上叉。当我给出这样的代码时,它工作正常。这将打印叉和僵尸进程
我是孩子
我父
,然后等待30秒。我明白这是由于这两个过程之间的切换。第一个孩子,然后执行父母和子女,然后...
#include<stdio.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
sleep(30);
exit(0);
}
if(pid>0)
{
printf("\nI am the parent\n");
wait();
}
}
但是,当我给像(无父等待)
#include<stdio.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
sleep(30);
exit(0);
}
if(pid>0)
{
printf("\nI am the parent\n");
}
}
它只是打印
我是孩子
我是母公司
并退出(不等待30秒)。
那么是因为没有等待调用父节点退出并且子节点仍在执行?但为什么它没有出现在终端(等待中)?
父母是否在这里变成僵尸?
请不要使用太多的问号。 ;-) – Nayuki