Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle SIGCHLD?

Tags:

c

sigchld

I need to handle SIGCHLD properly. How can I use it with my existing code? at the moment I cant wait for the child process unless I use 0 instead of WNOHANG|WUNTRACED.

status = 0; 
pid_t child, endID;

if(amp == 1)
        signal( SIGCHLD, SIG_IGN ); 

child = fork(); 

if (child  <  0) {    
        perror("fork() error\n");
        exit(EXIT_FAILURE);

} else if (child == 0) { 
        // do sth here
        perror("error\n");

} else { 
        //sleep(1)

If I remove sleep then parent is executed 1st.. why?

like image 611
kanoz Avatar asked Aug 24 '11 07:08

kanoz


People also ask

How do you catch SIGCHLD?

When a child process stops or terminates, SIGCHLD is sent to the parent process. The default response to the signal is to ignore it. The signal can be caught and the exit status from the child process can be obtained by immediately calling wait(2) and wait3(3C).

What is the use of SIGCHLD signal?

Sends a SIGCHLD signal to the parent process to indicate that the child process has ended. Saves the exit status of the child process so that the parent process can identify which child process (by process ID) ended and its exit status.

What is the default action of SIGCHLD?

Note that the default action for SIGCHLD is to ignore this signal; nevertheless signal(SIGCHLD, SIG_IGN) has effect, namely that of preventing the transformation of children into zombies.

How do you reap process?

The process of eliminating zombie processes is known as 'reaping'. The simplest method is to call wait , but this will block the parent process if the child has not yet terminated. Alternatives are to use waitpid to poll or SIGCHLD to reap asynchronously. The method described here uses SIGCHLD .


1 Answers

Here is a start (but read below):

static void
child_handler(int sig)
{
    pid_t pid;
    int status;

    /* EEEEXTEERMINAAATE! */
    while((pid = waitpid(-1, &status, WNOHANG)) > 0)
        ;
}

/* Establish handler. */
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;

sigaction(SIGCHLD, &sa, NULL);

Of course, this is all pointless. If the parent simply ignores SIGCHLD, the children are silently reaped and won't turn into zombies.

Quoting TLPI:

Explicitly setting the disposition of SIGCHLD to SIG_IGN causes any child process that subsequently terminates to be immediately removed from the system instead of being converted into a zombie.

So something like this should do the trick for you:

signal(SIGCHLD, SIG_IGN); /* Silently (and portably) reap children. */
like image 88
cnicutar Avatar answered Sep 18 '22 12:09

cnicutar