/*--------------------------------------------------------------------*/ /* testsignalignore.c */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include /*--------------------------------------------------------------------*/ /* Demonstrate ignoring signals and restoring default signal behavior. As usual, argc is the command-line argument count, and argv contains the command-line arguments. The function never returns. */ int main(int argc, char *argv[]) { enum {SLEEP_SECONDS = 3}; void (*pfRet)(int); for (;;) { printf("\nFor the next %d seconds ", SLEEP_SECONDS); printf("SIGINT signals are ignored.\n"); /* Ignore SIGINT signals. */ pfRet = signal(SIGINT, SIG_IGN); if (pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); } sleep(SLEEP_SECONDS); printf("\nFor the next %d seconds ", SLEEP_SECONDS); printf("SIGINT signals are not ignored.\n"); /* Restore the default behavior for SIGINT signals. */ pfRet = signal(SIGINT, SIG_DFL); if (pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); } sleep(SLEEP_SECONDS); } /* Will not reach this point. */ } /*--------------------------------------------------------------------*/ /* Sample execution: $ gcc217 testsignalignore.c -o testsignalignore $ ./testsignalignore For the next 3 sec SIGINT signals are ignored. ^C^C^C For the next 3 sec SIGINT signals are not ignored. For the next 3 sec SIGINT signals are ignored. ^C^C^C^C For the next 3 sec SIGINT signals are not ignored. ^C */