/* sigCross.c * * Neal Nelson 2009.11.04 * * Separate signal handlers for parent and child. Note that the child * has to be created before it can register its signal handler. * * See man pause. pause waits for a signal before continuing. * */ #include #include /* for pause() */ #include #include /* prototypes must occur before sig handlers are referenced */ static void parent_handler(); static void child_handler(); static int sigusr1 = 0; static int sigusr2 = 0; main() { int i, parent_pid, child_pid, status; /* prepare the parent_handler to catch SIGUSR1 */ signal(SIGUSR1, parent_handler); parent_pid = getpid(); if ((child_pid = fork()) == 0) /* child */ { /* prepare the child_handler to catch SIGUSR2 */ signal(SIGUSR2, child_handler); kill(parent_pid, SIGUSR1); pause(); /* child pause for SIGUSR2 */ printf("Child: sigusr1, sigusr2: %d, %d\n",sigusr1, sigusr2); exit(0); } else /* parent */ { kill(child_pid, SIGUSR2); wait(&status); /* parent wait for child termination */ printf("Parent: sigusr1, sigusr2: %d, %d\n",sigusr1, sigusr2); } } static void parent_handler() { sigusr1++; printf("parent_handler: incremented sigusr1 %d\n",sigusr1); return; } static void child_handler() { sigusr2++; printf("child_handler: incremented sigusr2 %d\n", sigusr2); return; }