C++ 当父进程被杀死时,使用 fork() 创建的子进程是否会自动终止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/395877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are child processes created with fork() automatically killed when the parent is killed?
提问by GetFree
I'm creating child processes with fork()
in C/C++.
When the parent process ends (or is killed for some reason) I want all child processes to be killed as well.
Is that done automatically by the system? Or I have to do it myself?
我正在用fork()
C/C++创建子进程。
当父进程结束(或由于某种原因被杀死)时,我希望所有子进程也被杀死。
这是系统自动完成的吗?还是我必须自己做?
Thanks.
谢谢。
Pre-existing similar questions:
预先存在的类似问题:
回答by Johannes Schaub - litb
No. If the parent is killed, children become children of the init process (that has the process id 1 and is launched as the first user process by the kernel).
否。如果父进程被杀死,子进程将成为 init 进程的子进程(进程 ID 为 1,并由内核作为第一个用户进程启动)。
The init process checks periodically for new children, and waits for them (thus freeing resources that are allocated by their return value).
init 进程定期检查新的子进程,并等待它们(从而释放由它们的返回值分配的资源)。
The question was already discussed with quality answers here: How to make child process die after parent exits?
这个问题已经在这里用高质量的答案进行了讨论: How to make child process die after parent exits?