Even though the linux man page for wait 1 explains very well that you need to wait()
for child processes for them no to turn into zombies, it does not tell why at all.
I planned my program (which is my first multithreaded one, so excuse my naivity) around a for(;;)
ever loop that starts child processes which get exec()
ed away and are sure to terminate on their own.
I cannot use wait(NULL)
because that makes parallel computation impossible, therefore I'll probably have to add a process table that stores the child pids and have to use waitpid
- not immideately, but after some time has passed - which is a problem, because the running time of the children varies from few microseconds to several minutes. If I use waitpid
too early, my parent process will get blocked, when I use it too late, I get overwhelmed by zombies and cannot fork()
anymore, which is not only bad for my process, but can cause unexpected problems on the whole system.
I'll probably have to program some logic of using some maximum number of children and block the parent when that number is reached - but that should be not necessary because most of the children terminate quickly. The other solution that I can think of (creating a two-tiered parent process that spawns concurrent children which in turn concurrently spawn and wait
for grandchildren) is too complicated for me right now. Possibly I could also find a non-blocking function to check for the children and use waitpid
only when they have terminated.
Nevertheless the question:
Why does Linux keep zombies at all? Why do I have to wait for my children? Is this to enforce discipline on parent processes? In decades of using Linux I have never got anything useful out of zombie processes, I don't quite get the usefulness of zombies as a "feature".
If the answer is that parent processes need to have a way to find out what happened to their children, then for god's sake there is no reason to count zombies as normal processes and forbid the creation of non-zombie processes just because there are too many zombies. On the system I'm currently developing for I can only spawn 400 to 500 processes before everything grinds to halt (it's a badly maintained CentOS system running on the cheapest VServer I could find - but still 400 zombies are less than a few kB of information)
See Question&Answers more detail:os