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
上。
所以在Linux操作系统中,不会存在真正的孤儿进程,因为所有的进程都会被托孤给init
进程或subreaper
。