fork2次为什么还产生僵尸?

程序代码如下:
int fork2()
{
pid_t pid;
int rc;
int status;

if (!(pid = fork()))
{
switch (fork())
{
case 0:return 0;
case -1: _exit(errno);
default: _exit(0);
}
}

if (pid < 0 || waitpid(pid,&status,0) < 0)
return -1;

if (WIFEXITED(status))
if (WEXITSTATUS(status) == 0)
return 1;
else
errno = WEXITSTATUS(status);
else
errno = EINTR;

return -1;
}

func()
{
if (fork2()==0)

_exit(0);
}

你的操作系统版本是多少?在6.3.2上试了,没有僵尸啊?

6.2.1,我测试的系统是自己裁剪的一个系统,奇怪的是在完整系统上测好像不产生僵尸,但是在裁剪的系统上就有,这和什么有关呢?

对不起,func()是这样写的
func()
{
while()
{
if (fork2()==0)
{

_exit(0);
}
}
}
只要运行到_exit(0)就会出现僵尸

用pidin family看僵尸的ppid是1,难道是系统产生的

xtang,请问如何解决呢?

在生成child之前,把 SIGCHLD IGNORE 掉;或者,
在Child 启动后,做一个procmgr_daemon()的掉用应该也有用。