Block sigchld to make sure all children handled
I have some code like this, which is a copy from
and code from real programs I found on the internet:
struct sigaction signalHandler;
signalHandler.sa_handler = sigchld;
signalHandler.sa_mask = blockedSignals;
signalHandler.sa_flags = 0;
sigaction(SIGCHLD, &signalHandler, NULL);
It doesn't work for me - meaning sigchld() doesn't run for /every/ child
that exits. So I have some zombies left over and my counter for running
children is not decremented.
All I want to do is make sure that I know when every child exited. What
am I doing wrong? What am I not doing? Is there an easier way?
Could the problem be somewhere else? What else could cause this problem,
other than what's handled by sa_mask?
Thanks in advance
Re: Block sigchld to make sure all children handled
in comp.unix.programmer i read:
>meaning sigchld() doesn't run for /every/ child that exits. So I have
>some zombies left over and my counter for running children is not
most signals aren't reliable nor are they more than a single bit, so if
two child processes exit before the signal is dispatched there will be only
the one indication.
trying to maintain a counter has issues too. consider, what if the type is
such that multiple instructions are needed to increment it and the signal
interrupts that mid-way through?
>All I want to do is make sure that I know when every child exited. What am
>I doing wrong? What am I not doing? Is there an easier way?[/color]
use waitpid(...,WNOHANG) in a loop inside your signal handler; you've run
out of dead children when it returns -1 with errno == ECHILD, though the
more sensible termination check is for errno != EINTR (e.g., in case you
have an argument problem which would result in -1/EINVAL).