/*--------------------------------------------------------------------*/ /* testsignalignore.c */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include #include /* Required for splint. */ /*--------------------------------------------------------------------*/ /* 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[]) { void (*pfRet)(int); sigset_t sSet; int iRet; /* Make sure SIGINT signals are not blocked. */ iRet = sigemptyset(&sSet); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } iRet = sigaddset(&sSet, SIGINT); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } iRet = sigprocmask(SIG_UNBLOCK, &sSet, NULL); if (iRet == -1) {perror(argv[0]); exit(EXIT_FAILURE); } /* Ignore SIGINT signals. */ pfRet = signal(SIGINT, SIG_IGN); if (pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); } /* Sleep for a while. */ printf("Going to sleep\n"); fflush(stdout); sleep(5); printf("Waking up\n"); fflush(stdout); /* Restore the default behavior for SIGINT signals. */ pfRet = signal(SIGINT, SIG_DFL); if (pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); } /* Enter an infinite loop. */ printf("Entering an infinite loop\n"); fflush(stdout); for (;;) sleep(1); /* Will not reach this point. */ } /*--------------------------------------------------------------------*/ /* Sample execution: $ gcc217 testsignalignore.c -o testsignalignore $ testsignalignore Going to sleep ^C^C^C^C^C^C^C^CWaking up Entering an infinite loop ^C */