利用prctl的PR_SET_PDEATHSIG属性设置父进程死亡时,发送给子进程的信号。
int pid;
pid = fork()
if (pid == 0) {
//子进程
//设置父进程死亡时自动给子进程发的信号
prctl(PR_SET_PDEATHSIG, SIGKILL)....
} else {
//父进程
}
相关属性如下:
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 process will get when its parent dies. This value is cleared for the child of a fork(2)
and (since Linux 2.4.36 / 2.6.23) when executing a set-user-ID or set-group-ID binary, or a binary that has associated capa‐
bilities (see capabilities(7)). This value is preserved across execve(2).
参考链接:
https://man7.org/linux/man-pages/man2/prctl.2.html