/* Handle the keyboard interrupt signal * Loop infinitely, until we interrupt. * This demonstrates that we can return from a handler * to the code that it was executing when the signal * arrived. This is not a typical use of a signal-handler. * Typically, we want to exit from the program. * But it demonstrates that the flow is different between signal-handling and exception-handling. */ #include #include #include int quit = 0; void handler (int signal) { printf("\nCaught signal %d\n", signal); printf( "The signal we caught is equal to SIGINT: %d\n", signal==SIGINT); quit = 1; return; //exit(-1); //to terminate } int main () { // SIGINT "program interrupt" signal, the signal is sent when the user type // INTR character, usually Ctrl+c signal(SIGINT, *handler); while (!quit) { printf("blah\n"); int a = 0; for (int i = 0; i < 1000000; i++) { if (i%2 == 0) { a += 1; } else { a -= 1; } } } printf("\nCleaning up\n"); return 0; }