Wait for and/or kill process grandchildren produced by fork

徘徊边缘 提交于 2019-11-29 12:45:06

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.

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!