当父进程被杀死时,使用 fork() 创建的子进程是否会自动终止? [英] Are child processes created with fork() automatically killed when the parent is killed?

查看:35
本文介绍了当父进程被杀死时,使用 fork() 创建的子进程是否会自动终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C/C++ 中的 fork() 创建子进程.
当父进程结束(或由于某种原因被杀死)时,我希望所有子进程也被杀死.
这是系统自动完成的吗?还是我必须自己做?

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?

谢谢.

预先存在的类似问题:

推荐答案

没有.如果父进程被杀死,子进程将成为 init 进程的子进程(进程 ID 为 1,并由内核作为第一个用户进程启动).

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 进程定期检查新的子进程,并等待它们(从而释放由它们的返回值分配的资源).

The init process checks periodically for new children, and waits for them (thus freeing resources that are allocated by their return value).

这里已经讨论了这个问题并提供了高质量的答案:如何让子进程在父进程退出后死亡?

The question was already discussed with quality answers here: How to make child process die after parent exits?

这篇关于当父进程被杀死时,使用 fork() 创建的子进程是否会自动终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆