The child process of fork dies with the parent process prctl

Use the PR_SET_PDEATHSIG property of prctl to set the signal sent to the child process when the parent process dies.

int pid;
pid = fork()
if (pid == 0) {
    
    
//子进程
//设置父进程死亡时自动给子进程发的信号
prctl(PR_SET_PDEATHSIG, SIGKILL)....

} else {
    
    
//父进程

}

The relevant attributes are as follows:

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).

Reference link:
https://man7.org/linux/man-pages/man2/prctl.2.html

Guess you like

Origin blog.csdn.net/sun172270102/article/details/112721662