On Aug 24, 2009, at 6:19 PM, Dmitri A. Sergatskov wrote:
On Mon, Aug 24, 2009 at 4:39 PM, Rob Mahurin<address@hidden> wrote:
You probably want waitpid(-1,WNOHANG) so that print won't freeze
if there is actually no child.
Attached as a patch (which is not a changeset, sorry). This would
be good for 3.2.3.
With WNOHANG I see few (one or two) defunct processes remains after
loop of ~100 prints.
Sorry, I wasn't clear: it's safe to do
while (0 < waitpid (-1, WNOHANG) ); endwhile
which cleans up any & all errant children, doing nothing if there
are none.
That's not quite what I did in the patch, since I misunderstood
waitpid's return values.
Ben Abbott asks:
Also what about children that are not zombies? Will such produce an
infinite loop?
Not as written here (this one is tested).
Regarding the "zombies" does waitpid() kill them or just wait until
the die?
No. A "zombie" is a process that has already called exit() but is
being kept in memory so that its parent can check its exit status.
There's a little information in the man page for ps, and wikipedia
has a "zombie process" entry.
Rob