Block sigchld to make sure all children handled - Unix

This is a discussion on Block sigchld to make sure all children handled - Unix ; Hi I have some code like this, which is a copy from http://www.gnu.org/software/libc/man...ng-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; ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Block sigchld to make sure all children handled

  1. Block sigchld to make sure all children handled

    Hi

    I have some code like this, which is a copy from
    http://www.gnu.org/software/libc/man...ng-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

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

+ Reply to Thread