父母退出后如何使子进程死亡? [英] How to make child process die after parent exits?
问题描述
假设我有一个进程恰好产生一个子进程.现在,当父进程因任何原因(正常或异常,通过 kill、^C、断言失败或其他任何原因)退出时,我希望子进程终止.如何正确地做到这一点?
Suppose I have a process which spawns exactly one child process. Now when the parent process exits for whatever reason (normally or abnormally, by kill, ^C, assert failure or anything else) I want the child process to die. How to do that correctly?
关于stackoverflow的一些类似问题:
Some similar question on stackoverflow:
关于 Windows 的 stackoverflow 的一些类似问题:
Some similar question on stackoverflow for Windows:
推荐答案
通过指定选项PR_SET_PDEATHSIG
,当父进程死亡时,子进程可以请求内核传递SIGHUP
(或其他信号)在 prctl()
系统调用中像这样:
Child can ask kernel to deliver SIGHUP
(or other signal) when parent dies by specifying option PR_SET_PDEATHSIG
in prctl()
syscall like this:
prctl(PR_SET_PDEATHSIG, SIGHUP);
详见man 2 prctl
.
这仅适用于 Linux
This is Linux-only
这篇关于父母退出后如何使子进程死亡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!