confusion about signals
Stephen Weeks
sweeks@acm.org
Sun, 17 Mar 2002 13:56:25 -0800
The following C program forks off a child that immediately exits and
hence causes a SIGCHLD to be sent to the parent. The parent handles
SIGCHLD and sets handled to 1. Therefore, I believe that the program
should always print
handled = 1
However, it doesn't always do this. In fact, most of the time it
prints
handled = 0
Can anyone explain why?
--------------------------------------------------------------------------------
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
volatile int handled = 0;
static void handler(int sig) {
handled = 1;
}
int main(void) {
struct sigaction act;
int i;
act.sa_flags = 0;
act.sa_handler = &handler;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
if (0 != fork()) {
/* Parent. Wait on SIGCHLD from child. */
sigsuspend(NULL);
fprintf(stderr, "handled = %d\n", handled);
}
return 0;
}