Quote Originally Posted by Blasz View Post
Can you suggest a good site that tells you how to setup a sighandler?

The man page didn't really help.
Why is it that everyone complains about Google but no one seems to use it? First hit on grepping for SIGCHILD example:
Code:
#include <sys/types.h>  /* include this before any other sys headers */
#include <sys/wait.h>   /* header for waitpid() and various macros */
#include <signal.h>     /* header for signal functions */
#include <stdio.h>      /* header for fprintf() */
#include <unistd.h>     /* header for fork() */

void sig_chld(int);     /* prototype for our SIGCHLD handler */

int main() 
{
    struct sigaction act;
    pid_t pid;

    /* Assign sig_chld as our SIGCHLD handler */
    act.sa_handler = sig_chld;

    /* We don't want to block any other signals in this example */
    sigemptyset(&act.sa_mask);

    /*
     * We're only interested in children that have terminated, not ones
     * which have been stopped (eg user pressing control-Z at terminal)
     */
    act.sa_flags = SA_NOCLDSTOP;

    /*
     * Make these values effective. If we were writing a real 
     * application, we would probably save the old value instead of 
     * passing NULL.
     */
    if (sigaction(SIGCHLD, &act, NULL) < 0) 
    {
        fprintf(stderr, "sigaction failed\n");
        return 1;
    }

    /* Fork */
    switch (pid = fork())
    {
    case -1:
        fprintf(stderr, "fork failed\n");
        return 1;

    case 0:                         /* child -- finish straight away */
        _exit(7);                   /* exit status = 7 */

    default:                        /* parent */
        sleep(10);                  /* give child time to finish */
    }

    return 0;
}

/*
 * The signal handler function -- only gets called when a SIGCHLD
 * is received, ie when a child terminates
 */
void sig_chld(int signo) 
{
    int status, child_val;

    /* Wait for any child without blocking */
    if (waitpid(-1, &status, WNOHANG) < 0) 
    {
        /*
         * calling standard I/O functions like fprintf() in a 
         * signal handler is not recommended, but probably OK 
         * in toy programs like this one.
         */
        fprintf(stderr, "waitpid failed\n");
        return;
    }

    /*
     * We now have the info in 'status' and can manipulate it using
     * the macros in wait.h.
     */
    if (WIFEXITED(status))                /* did child exit normally? */
    {
        child_val = WEXITSTATUS(status); /* get child's exit status */
        printf("child's exited normally with status %d\n", child_val);
    }
}