SIGCHLD recursion - Aix

This is a discussion on SIGCHLD recursion - Aix ; I'm porting some code from LINUX to AIX and am hitting a problem in the SIGCHLD handler. Here is the code static void child_handler(int sig) { int n, status; for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++) ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: SIGCHLD recursion

  1. SIGCHLD recursion

    I'm porting some code from LINUX to AIX and am hitting a problem in the
    SIGCHLD handler. Here is the code

    static void child_handler(int sig)
    {
    int n, status;
    for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++) ;
    if (n == 0)
    printf("Didn't reap any childred\n");
    signal(sig, child_handler);
    }

    The problem is that wait4 (which isn't even listed in the AIX 5.2 tech
    ref) must not be working as expeced since when it exits the loop, "n"
    is still equal to 0, then when the signal() call is made at the end of
    the function, the child_handler get control right back and eventually
    overruns the stack. Can anybody see why it is acting this way? Since
    the code doesn't seem to do anything with the child, I changed the code
    to ignore this signal and it seems to work OK, but I'd still like to
    understand what is happening in case I do need to handle it in the
    future.
    Thanks.


  2. Re: SIGCHLD recursion


    dam@us.ibm.com wrote:
    > I'm porting some code from LINUX to AIX and am hitting a problem in the
    > SIGCHLD handler. Here is the code
    >
    > static void child_handler(int sig)
    > {
    > int n, status;
    > for (n = 0; wait4(-1, &status, WNOHANG, NULL) > 0; n++) ;
    > if (n == 0)
    > printf("Didn't reap any childred\n");
    > signal(sig, child_handler);
    > }
    >
    > The problem is that wait4 (which isn't even listed in the AIX 5.2 tech
    > ref) must not be working as expeced since when it exits the loop, "n"
    > is still equal to 0, then when the signal() call is made at the end of
    > the function, the child_handler get control right back and eventually
    > overruns the stack. Can anybody see why it is acting this way? Since
    > the code doesn't seem to do anything with the child, I changed the code
    > to ignore this signal and it seems to work OK, but I'd still like to
    > understand what is happening in case I do need to handle it in the
    > future.
    > Thanks.


    Replace the wait4() call with a waitpid() call...
    ....like this:
    for(n=0;waitpid(-1, &status, WNOHANG) > 0; n++) ;

    Or, compile the existing code with the BSD library:
    cc -o demo demo.c -D_BSD -lbsd

    Both will work...

    The current problem is that child process is not "seen" by the wait4()
    call,
    so that when "signal" is rearmed, it immediately goes (recursively)
    into the
    child_handler() function.

    If you're not opposed to making other fixes, I would suggest you
    convert
    the "main" signal() call, into a sigaction() call... eliminating the
    need
    to "re-arm" signal handling in child_handler.

    HTH,
    -tony


+ Reply to Thread