此程序可以产生僵尸进程

//这个程序可以产生僵尸进程
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<stdlib.h>
#include<signal.h>
int main(int argc , char **argv)
{
//signal(SIGCHLD, SIG_IGN); 如果将这行代码加上将不会产生僵尸进程
int id; id=fork(); if(id<0) { printf("fork error "); } else if(id==0) { printf("I'm in child process%d parent process %d ",getpid(),getppid()); exit(0); } else if(id>0) {sleep(20); printf("I'm in parent process%d parent process %d ",getpid(),getppid()); } return 0; } [root@linux Desktop]# ./a.out I'm in child process3418 parent process 3417 //这句输出后大概停顿20秒,才输出下面 I'm in parent process3417 parent process 3276 [root@linux Desktop]# [root@linux Desktop]# ps aux | grep -w 'Z' root 3418 0.0 0.0 0 0 pts/0 Z+ 21:27 0:00 [a.out] <defunct> //产生僵尸进程 root 3428 0.0 0.0 4336 796 pts/1 S+ 21:27 0:00 grep -w Z [root@linux Desktop]#

如果将signal(SIGCHLD, SIG_IGN);这行代码加上将不会产生僵尸进程的

[root@linux Desktop]# ps aux | grep -w 'Z'
root 3575 0.0 0.0 4336 796 pts/1 S+ 21:36 0:00 grep -w Z
[root@linux Desktop]#

 
原文地址:https://www.cnblogs.com/leijiangtao/p/4078573.html