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