什么是孤儿进程?
孤儿进程, 指在父进程退出后,而子进程还在运行,
这个子进程就成了孤儿进程,这时由init进程(pid=1)接管
来看看例子:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
pid_t pid ;
//父进程先结束,子进程还在运行
//如果父进程结束,则子进程的父亲为init
//孤儿进程由进程他的祖先收留
pid = fork();
if(0 == pid)
{
printf("first child pid:%d ppid:%d \n" , getpid() , getppid());
sleep(2);
printf("second child pid:%d ppid:%d \n" , getpid() , getppid());
while(1)
sleep(1);
return 0 ;
}
sleep(1);
printf("parent pid:%d ppid:%d \n" , getpid() , getppid());
printf("I am die ... \n");
return 0 ;
}