孤儿进程:
父进程如果不等待子进程退出,在子进程之前就结束了自己的“生命”,此时子进程叫做孤儿进程
linux避免系统存在过多孤儿进程,init进程收留孤儿进程,变成孤儿进程的父进程
代码验证:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int cnt=0;
int status=10;
pid=fork();
if(pid>0){
printf("this is father print%d\n",getpid());
}
if(pid==0){
while(1){
printf("this is child print,pid=%d,father pid=%d\n",getpid(),getppid());
sleep(1);
cnt++;
if(cnt==5){
//break;
//exit(0);
//_exit(0);
exit(3);
}
}
}
return 0;
}
编译运行结果:可以看到原先父进程pid为24463,父进程结束后被系统init收留父进程pid变为1
this is father print24463
this is child print,pid=24464,father pid=24463
CLC@Embed_Learn:~/jincheng$ this is child print,pid=24464,father pid=1
this is child print,pid=24464,father pid=1
this is child print,pid=24464,father pid=1
this is child print,pid=24464,father pid=1