防止子进程成为孤儿进程

发布于 2024-11-26 06:22:10 字数 208 浏览 2 评论 0原文

我的 Linux 进程有 4 个子进程。经过一段时间的执行后,所有子进程都被 init 进程采用。我们该如何预防这种情况呢? (僵尸进程不是这种情况)。

该过程是用C语言编写的,操作系统是Linux。我的代码调用 waitpid!可能是什么问题? 99.99%我们不会遇到这个问题。

最后更新:如果有人执行“kill -9”怎么办?这会立即终止父进程并使子进程成为孤儿。

My Linux process has 4 children. After some execution time all children adopted by the init process. How do we prevent this situation? (this is not the case with Zombie processes).

The process is written in C and the OS is Linux. My code calls waitpid! What might be the problem? In 99,99% we don't have this problem.

Last update: What if someone executes "kill -9 "? This terminates immediately the parent process and leaves the children orphan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不念旧人 2024-12-03 06:22:10

如果您的进程被 init 重新设置父进程,则意味着它们的父进程已死亡。当进程的父进程死亡时,init 会采用它,以便在子进程(即 init< /code>) 收到 SIGCHLD

如果您不希望 init 成为您子进程的父进程,则必须确保您的进程一直存在,直到您的所有子进程都已死亡并被您的程序收割。

If your processes are being reparented by init, that means that their parent process has died. When a process' parent dies, init adopts it so that it can reap the zombie by wait()ing on the child when it (that is, init) receives SIGCHLD.

If you do not want init to become the parent of your children, you will have to ensure that your process lives until all of your children have died and been reaped by your program.

极度宠爱 2024-12-03 06:22:10

等孩子们出去后再自己出去。有关更多详细信息,请参阅 wait(2) 手册页。

Wait for the children to exit before exiting yourself. See the wait(2) man page for more details.

空名 2024-12-03 06:22:10

从主页检查您的 waitpid API 参数,并确保您的父进程不应在所有子进程完成之前结束。
你可以在这里发布你的代码吗?

Check from main page for your waitpid API parameters, and make sure your parent process should not be over before all child processes are finished.
Can you post your code here?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文