tags:

views:

57

answers:

1

I'm trying to figure out what the pid is of a process that sent the SIGCHLD signal, and I want to do this in a signal handler I created for SIGCHLD. How would I do this? I'm trying:

int pid = waitpid(-1, NULL, WNOHANG);

.. because I want to wait for any child process that is spawned.

Thanks, Hristo

A: 

If you use waitpid() more or less as shown, you will be told the PID of one of the child processes that has died - usually that will be the only process that has died, but if you get a flurry of them, you might get one signal and many corpses to collect. So, use:

void sigchld_handler(int signum)
{
    pid_t pid;
    while ((pid = waitpid(-1, NULL, WNOHANG)) != -1)
    {
        unregister_child(pid);   // Or whatever you need to do with the PID
    }
}
Jonathan Leffler