等待和/或杀死 fork 产生的孙进程

发布于 2024-11-13 09:37:13 字数 403 浏览 7 评论 0原文

fork() 进入进程 X 和 Y,然后 Y forks() 再次进入其自身并多次处理 Z。

现在进程 Y 是某种“监听器”,我希望 X 成为删除器。 Z 进程执行实际操作。 Z 进程是 X 的孙子进程。

通过 FIFO 和一些信令,X 生成了 Z 进程的所有 pid 的列表。现在的问题是我想用 X 删除 Z 进程僵尸(遍历 pid 列表)。

我已经用 waitpid() 尝试过,但这当然不起作用(它只适用于直接子级)。但我已经读到了有关为此进行扩展的可能性。但我真的不知道该怎么做。

我想过删除器保留另一个包含僵尸的列表(退出时发出信号),但这与我保存 pid 时所做的相同,我想以不同的方式做。

有人知道如何做到这一点吗?

I fork() into process X and Y, afterwards Y forks() again into itself and process Z multiple times.

Now process Y is some kind of "listener" and I would like X to be the deleter. The Z processes perform the actual actions. Z processes are grandchildren of X.

With a FIFO and some signaling, X has produced a list of all pids of the Z processes. The problem now is that I would like to delete Z process zombies with X (going through the list of pids).

I've tried it with waitpid(), but of course that doesn't work (it only does for direct children). But I've read about the possibility of making an extension yourself for this. But I really wouldn't know how to do it.

I've thought of the deleter keeping another list with zombies (signal when exiting) but this is just the same as i did with saving the pids, I would like to do it differently.

Does anybody have an idea of how to do this?

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

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

发布评论

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

评论(3

从来不烧饼 2024-11-20 09:37:13

唯一可以从其遥远的第 N 代孙子进程获取退出状态的进程是“init”进程,这是内核实现的特殊情况规则。

一般来说,一个进程只能等待它的直接子进程死亡;它不能等待它的子孙死去。

病态的业务...


如果您负责流程 Y 代码,或者可以影响它,也许该流程应该设置 signal(SIGCHLD, SIG_IGN) 以便Z 进程不会创建僵尸进程。进程X甚至可以在分叉Y进程时自行执行此操作,方法是在fork()之后和任何之前忽略子进程中的SIGCHILD。 Y 进程的 code>exec*()。仅当 Y 进程显式为 SIGCHLD 设置不同的处理程序时,此操作才会被覆盖。如果 Y 代码显式设置 SIGCHLD 处理,但实际上并未收集其僵尸进程(Z 进程),那么您可以在 Y 中报告错误> 代码。

The only process that can acquire exit statuses from its distant Nth generation grand-children is the 'init' process, and that is a special case rule implemented by the kernel.

In general, a process can only wait for its direct children to die; it cannot wait for its children's progeny to die.

Morbid business...


If you're in charge of the process Y code, or can influence it, perhaps that process should set signal(SIGCHLD, SIG_IGN) so that the Z processes do not create zombies. Process X could even do that itself while it forks the Y processes by ignoring SIGCHILD in the child process after the fork() and before any exec*() of the Y process. This only gets overridden if the Y processes explicitly set a different handler for SIGCHLD. And if the Y code explicitly sets SIGCHLD handling and does not actually collect its zombies (Z processes), then you can report a bug in the Y code.

凡间太子 2024-11-20 09:37:13

不支持此操作。如果您的唯一目的是防止“Z”进程(即孙进程)变成僵尸进程,您可以使用 setsid()。然而,如果您确实需要它们的退出状态,那么您确实需要从“Y”进程中获取它们。

This is not supported. If your sole intent is to prevent the 'Z' processes (i.e., the grandchildren) from turning into zombies, you can use setsid(). If you actually need their exit status, however, you really need to reap them from the 'Y' processes.

世界和平 2024-11-20 09:37:13

你的问题使实际问题变得难以理解。尽管如此,我相信我可以辨别出以下内容:“我想摆脱僵尸”。好吧,我们都不是。

有多种方法可以做到这一点:

  • Y 忽略 SIGCHLDforked 孩子死后不会变成僵尸
  • Y 定期为任何孩子收获(wait

使用哪一个是你的选择,但是在我看来,第一个就是你想要的。

Your question does a terrific job of making the actual problem hard to understand. Still, I believe I can discern the following: "I want to get rid of the zombies". Well, don't we all.

There are multiple ways of doing this:

  • Have Y ignore SIGCHLD. forked children will not turn into zombies when they die
  • Have Y periodically reap (wait) for any children

It's your choice which one you use, but it seems to me the first is what you want.

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