|
Home > Archive > Unix Programming > October 2007 > Block sigchld to make sure all children handled
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Block sigchld to make sure all children handled
|
|
| Andrew Smith 2007-10-20, 7:31 am |
| Hi
I have some code like this, which is a copy from
http://www.gnu.org/software/libc/ma...ing-for-Handler
and code from real programs I found on the internet:
struct sigaction signalHandler;
sigset_t blockedSignals;
signalHandler.sa_handler = sigchld;
sigemptyset(&blockedSignals);
sigaddset(&blockedSignals, 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
Andrew
| |
| those who know me have no need of my name 2007-10-20, 1:22 pm |
| 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
>decremented.
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?
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).
--
a signature
|
|
|
|
|