/* unix_signals.c Started by Jeff Ondich on 3/26/96 Last modified 4/7/10 This program gives a simple example of how a process can catch a signal. Here, SIGINT signals (typically generated by the user hitting Ctrl-C) are caught. Compile, run, and hit Ctrl-C while it's running. */ // It's interesting to comment out #include statements one // at a time to find out which functions require which .h files. #include #include #include #include void interruptHandler(int); const int gLimit = 2000000000; const int gOutputFrequency = 20000000; int gCounter; int main() { /* Announce your process ID just for fun. */ printf("Hi, I'm process %d\n", getpid()); /* Always always always check the return value for system functions. That said, since the initial signal handler for SIGINT is the default, it would be extraordinarily weird for this function call to return anything but SIG_DFL. */ if (signal(SIGINT, interruptHandler) != SIG_DFL) { fprintf(stderr, "I'm confused.\n"); } /* Start counting. */ for (gCounter = 0; gCounter < gLimit; gCounter++) { if (gCounter % gOutputFrequency == 0) { fprintf(stderr, "%d\n", gCounter); fflush(stderr); } } return 0; } void interruptHandler(int sig) { fprintf(stderr, "We counted all the way up to %d.\n", gCounter); fprintf(stderr, "This is process %d signing off.\n", getpid()); fflush(stderr); exit(0); }