Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Experiment to see what the default SIGCHLD handling was

Posted by peeterjoot on April 15, 2011

For some multi-process code I’d see that our waitpid() wrapper function blocked SIGCHLD before calling waitpid(). That didn’t quite make sense to me. If SIGCHLD had to be blocked, wouldn’t that have to be done before the fork?

I didn’t even want SIGCHLD to be delivered, since I was calling waitpid(). Is the default handler for SIGCHLD SIG_IGN? To experiment (non-portably), I wrote the following mini-test:

#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

int main()
   pid_t pid = fork() ;

   if ( pid )
      int status ;
      printf( "forked: %d\n", (int)pid ) ;

      kill( pid, SIGKILL ) ; // does this generate SIGCHLD?  Is the default for that signal to be blocked?

      int rc = waitpid(0, &status, 0) ;
      printf( "waitpid(): rc, status = %d, %X\n", rc, status ) ;
      sleep( 10 ) ;
      _exit( 1 ) ;

   return 0 ;

I see:

forked: 16787
waitpid(): rc, status = 16787, 9

and don’t appear to get taken out due to no handler for SIGCHLD. The default does appear to be SIG_IGN (on Linux and AIX). This makes sense (man 7 signal on linux also appears to confirm).

I wonder if there is a way to query the handler (like calling sigaction for read only). If I wanted to be super safe, putting in an assert that the handler was SIG_IGN would then be possible.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: