AI智能
改变未来

Linux进程(五):托孤

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

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Linux进程(五):托孤