问题描述
假如我有一个派生只有一个子进程的过程。现在,当父进程退出无论出于何种原因(正常或异常,通过杀,^ 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?
在计算器的一些类似的问题:
Some similar question on stackoverflow:
- (刚才问)How我可以导致子进程退出时,家长呢?
- (后来问)Are )叉(创建子进程时,父被杀害自动杀?
- (asked earlier) How can I cause a child process to exit when the parent does?
- (asked later) Are child processes created with fork() automatically killed when the parent is killed?
有关计算器的一些类似的问题的窗口
Some similar question on stackoverflow for Windows:
- How do I automatically destroy child processes in Windows?
- Kill child process when parent process is killed
推荐答案
儿童可以要求内核提供 SIGHUP
(或其他信号)时,家长通过指定选项 PR_SET_PDEATHSIG 在使用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);
请参阅 2人使用prctl
了解详情。
编辑:这是仅支持Linux的
This is Linux-only
这篇关于如何使父退出后,子进程死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!