Linux进程(五):托孤
在Linux进程中,若父进程先于子进程死亡,那么Linux内核将把子进程“托孤”给进程树为subreaper的进程,并由这个subreaper来负责该孤儿进程的“收尸过程”(清理
task_struct),若未找到subreaper进程,则该孤儿进程之间托付给init进程:

一个进程可以通过prctl这个系统调用把自己生命成一个subreaper ,PR_SET_CHILD_SUBREAPER是Linux3.4加入的新特性,把她设置为非零值,当前进程就会变成subreaper,会像1号进程那样收养孤儿进程:
/*Become reaper of our children*/
if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0){
log_waring("Failed to make us a subreaper: %m");
if (errno == EINVAL){
log_info("Perhaps the kernel version is too old(<3.4:)");
}
}
注:被声明为subreaper的进程会在pstree命令中被显示为一个init。
例
int main(void)
{
pid_t pid,wait_pid;
int status;
pid = fork();
if (pid==-1) {
perror("Cannot create new process");
exit(1);
} else if (pid==0) {
printf("child process id: %ld\n", (long) getpid());
pause();
_exit(0);
} else {
printf("parent process id: %ld\n", (long) getpid());
wait_pid=waitpid(pid, &status, WUNTRACED | WCONTINUED);
if (wait_pid == -1) {
perror("cannot using waitpid function");
exit(1);
}
if(WIFSIGNALED(status))
printf("child process is killed by signal %d\n", WTERMSIG(status));
exit(0);
}
}
在上述例子中,我们创造出一个子进程,父进程等待进程的结束,此时,我们如果将父进程杀掉,子进程将被托孤给pstree中显示的最近的init中。
运行程序,父进程创建子进程:

此时查看pstree:

产生了两个a.out,若我们此时将父进程干掉,子进程的a.out将会被托孤至离他最近的init上。
![]](/https://i-blog.csdnimg.cn/blog_migrate/177f45e67e91b7886f8dc2ecd88ae2e9.png)
所以在Linux操作系统中,不会存在真正的孤儿进程,因为所有的进程都会被托孤给init进程或subreaper。
本文深入探讨了Linux系统中进程的托孤机制,当父进程早于子进程终止时,子进程将被托付给subreaper或init进程进行清理。通过prctl系统调用,一个进程可以成为subreaper,负责收养并清理孤儿进程。
:托孤&spm=1001.2101.3001.5002&articleId=108168694&d=1&t=3&u=dbbc8cd564a84a6997bf45afec27fc7e)
1940

被折叠的 条评论
为什么被折叠?



