#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include <stropts.h>
#include<sys/resource.h>
int main()
{
pid_t pid;
if((pid=fork())<0){
perror("creat");
}else if(pid==0){
if((pid=fork())<0){
perror("sec creat");
}else if(pid>0)
exit(0);//第一个子进程立即结束。waitpid等到了,退出了。
sleep(2);//由于父进程终止了,所以第二个子进程由init领养。因此不存在僵死状态。
printf("second child ,parent pid is %d\n",getppid());
exit(0);
}
if(waitpid(pid,NULL,0)!=pid)
perror("wait");
return 0;
}
所谓僵死进程,简单就是父进程在,子进程退出,父进程还没有wait子进程,子进程在这段时间就是僵死进程。
另外:子进程等待父进程结束:
while(getppid()!=1)
sleep(1)
此为轮询方式(polling)