您的位置:首页 > 运维架构 > Linux

Linux系统编程----孤儿进程

2016-09-30 09:30 295 查看
什么是孤儿进程?
   孤儿进程,  指在父进程退出后,而子进程还在运行,
这个子进程就成了孤儿进程,这时由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 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐