等待和/或杀死 fork 产生的孙进程
我 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唯一可以从其遥远的第 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 setsignal(SIGCHLD, SIG_IGN)
so that theZ
processes do not create zombies. ProcessX
could even do that itself while it forks theY
processes by ignoring SIGCHILD in the child process after thefork()
and before anyexec*()
of theY
process. This only gets overridden if theY
processes explicitly set a different handler for SIGCHLD. And if theY
code explicitly sets SIGCHLD handling and does not actually collect its zombies (Z
processes), then you can report a bug in theY
code.不支持此操作。如果您的唯一目的是防止“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.你的问题使实际问题变得难以理解。尽管如此,我相信我可以辨别出以下内容:“我想摆脱僵尸”。好吧,我们都不是。
有多种方法可以做到这一点:
Y
忽略SIGCHLD
。forked
孩子死后不会变成僵尸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:
Y
ignoreSIGCHLD
.forked
children will not turn into zombies when they dieY
periodically reap (wait
) for any childrenIt's your choice which one you use, but it seems to me the first is what you want.