等待和/或杀死fork产生的子孙 [英] Wait for and/or kill process grandchildren produced by fork

查看:87
本文介绍了等待和/或杀死fork产生的子孙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I fork()进入过程X和Y,然后Y forks()再次进入其自身并多次加工Z.

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

现在进程Y是某种侦听器",我希望X成为删除器. Z进程执行实际动作. Z进程是X的孙代.

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.

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

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).

我已经用waitpid()进行了尝试,但是当然不起作用(仅对直系子女有效).但是我已经读过自己为此扩展的可能性.但是我真的不知道该怎么做.

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.

我想到了删除程序将另一个列表包含僵尸(退出时发出信号),但这与保存pid一样,但我想以不同的方式做.

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.

有人知道怎么做吗?

推荐答案

唯一可以从其遥远的第N代子孙中获取退出状态的过程是"init"过程,这是由内核.

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.

生意不佳...

如果您负责进程Y的代码,或者可以影响它,则也许该进程应设置signal(SIGCHLD, SIG_IGN),以使Z进程不会创建僵尸.通过忽略子进程中的SIGCHILD,在fork()之后且在Y进程中的任何exec*()之前,进程X甚至可以自己进行分叉Y进程.仅当Y进程显式为SIGCHLD设置其他处理程序时,此方法才会被覆盖.并且,如果Y代码显式设置了SIGCHLD处理并且未真正收集其僵尸(Z进程),那么您可以在Y代码中报告错误.

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.

这篇关于等待和/或杀死fork产生的子孙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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