// A demo of using signal handlers and alarm. #include #include #include #include int beeps = 0; void sig_handler(int signo) { printf("BEEP\n"); if (++beeps < 5) { alarm(1); // schedule another SIGALRM } else { printf("DING DING!\n"); exit(0); } } int main() { if (signal(SIGALRM, sig_handler) < 0) { printf("error installing handler\n"); exit(1); } alarm(1); // send SIGALRM in 1 second while (1) { sleep(1); // sleep forever } return 0; }