原文地址:https://www.yanbinghu.com/2019/02/14/37859.html
前言
子进程退出的时候,父进程能够收到子进程退出的信号,便于管理,但是有时候又需要在父进程退出的时候,子进程也退出,该怎么办呢?
父进程退出时,子进程会如何?
一般情况下,父进程退出后,是不会通知子进程的,这个时候子进程会成为孤儿进程,最终被init进程收养。我们先来看一下这种情况。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
//fork一个进程
pid = fork();
//创建失败
if (pid < 0)
{
perror("fork error:");
exit(1);
}
//子进程
if (pid == 0)
{
printf("child process.\n");
printf("child pid:%d,parent pid:%d\n",getpid(),getppid());
printf("sleep 10 seconds.\n");
//sleep一段时间,让父进程先退出,为了便于观察,sleep 10s
sleep(10);
printf("now child pid: %d parent pid:%d\n",getpid(),getppid());
}
//父进程
else
{
printf("parent process.\n");
sleep(1);
}
return 0;
}
在这个程序中,我们为了让父进程先退出,子进程sleep了10秒。
运行结果如下:
parent process.
child process.
child pid:17433,parent pid:17432
sleep 10 seconds.
now child pid: 17433 parent pid:1658
从结果中可以看到,一开始子进程17433的父进程id是17432,但是在10秒后,它的父进程变成了1658。1685是什么进程呢?
$ ls -al /proc/1658/exe
/proc/1658/exe -> /sbin/upstart
由于我使用的环境是带有图形界面的ubuntu系统,所以最终并不是被我们所熟知的init进程收养,而是被一个名为/sbin/upstart的进程所收养。另外还可以观察到,该进程也是其他系统进程的父进程。
如何确保父进程退出的同时,子进程也退出?
既然如此,如何确保父进程退出的同时,子进程也退出呢?或许我们可以在子进程和父进程之间建立通信管道,一旦通信异常,则认为父进程退出,子进程自己也回收资源退出。但是这样做总觉得不是很正经。有没有已有的函数帮我们做这件事呢?prctl函数可以帮助我们。第一个参数中,有一个选项,叫做PR_GET_PDEATHSIG:
PR_SET_PDEATHSIG (since Linux 2.1.57)
Set the parent death signal of the calling process to arg2 (either a signal value in the range 1..maxsig, or 0 to clear).
This is the signal that the calling