/* * The text on p.273 reads: * * We invoke the program in the background and use the kill(1) command * to send it signals. Note that the term "kill" in Unix is a misnomer. * The kill(1) command the kill(2) function just send a signal to a * process or process group. Whether or not that signal terminates * the process depends on which signal is sent and whether the process * has arranged to catch the signal * * | * $ a.out & | start process in background * [1] 4720 | job-control shell prints job# and PID * $ kill -USR1 4720 | send it SIGUSR1 * received SIGUSR1 | * $ kill -USR2 4720 | send it SIGUSR2 * received SIGUSR2 | * $ kill 4720 | now send it SIGTERM * [1] + Terminated a.out & | * | * * When we send the SIGTERM signal the process is terminated, since it * doesn't catch the signal and the default action for the signal * is termination. * * Note: The above example interaction with the program never sent * the same signal twice. The action for a signal gets reset to it's * default each time the signal occurs. Since our program doesn't * explicitly reset the signal handler once it executes, a second * signal of the same type would not be caught by our handler. * (instead, the signal's default action would be executed) * * Modified Feb 18/99: removed dependency on ourhdr.h */ #include #include #include static void sig_usr (int); /* one handler for both signals */ int main (void) { if (signal (SIGUSR1, sig_usr) == SIG_ERR) { fprintf(stderr, "can't catch SIGUSR1"); exit(1); } if (signal (SIGUSR2, sig_usr) == SIG_ERR) { fprintf(stderr, "can't catch SIGUSR2"); exit(1); } for ( ; ; ) pause(); } static void sig_usr (int signo) /* argument is signal number */ { if (signo == SIGUSR1) printf("received SIGUSR1\n"); else if (signo == SIGUSR2) printf("received SIGUSR2\n"); else { fprintf(stderr, "received signal %d\n", signo); exit(1); } return; }