int main(int argc, char *argv[]) { struct sigaction sa; sigset_t prevMask, blockMask, pendingMask; printf("%s: PID is %ld\n", argv[0], (long) getpid()); sa.sa_handler = sigcont_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGCONT, &sa, NULL) == -1) errExit("sigaction error"); if (sigaction(SIGUSR1, &sa, NULL) == -1) errExit("sigaction error"); sigemptyset(&blockMask); sigaddset(&blockMask, SIGCONT); sigaddset(&blockMask, SIGUSR1); printf("Start blocking SIGCONT & SIGUSR1\n"); if (sigprocmask(SIG_SETMASK, &blockMask, &prevMask) == -1) errExit("sigprocmask error"); printf("Start sleeping ...\n"); sleep(10); printf("Stop sleeping ...\n"); if (sigpending(&pendingMask) == -1) errExit("sigpending"); printf("%s: pending signals are: \n", argv[0]); printSigset(stdout, "\t\t", &pendingMask); printf("Stop blocking SIGCONT & SIGUSR1\n"); if (sigprocmask(SIG_SETMASK, &prevMask, NULL) == -1) errExit("sigprocmask error"); if (sigpending(&pendingMask) == -1) errExit("sigpending"); printf("%s: pending signals are: \n", argv[0]); printSigset(stdout, "\t\t", &pendingMask); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int n, numSecs; sigset_t pendingMask, blockingMask, emptyMask; printf("%s: PID is %ld\n", argv[0], (long) getpid()); /* Here we use the simpler signal() API to establish a signal handler, but for the reasons described in Section 22.7 of TLPI, sigaction() is the (strongly) preferred API for this task. */ for (n = 1; n < NSIG; n++) /* Same handler for all signals */ (void) signal(n, handler); /* Ignore errors */ /* If a sleep time was specified, temporarily block all signals, sleep (while another process sends us signals), and then display the mask of pending signals and unblock all signals */ if (argc > 1) { numSecs = getInt(argv[1], GN_GT_0, NULL); sigfillset(&blockingMask); if (sigprocmask(SIG_SETMASK, &blockingMask, NULL) == -1) errExit("sigprocmask"); printf("%s: sleeping for %d seconds\n", argv[0], numSecs); sleep(numSecs); if (sigpending(&pendingMask) == -1) errExit("sigpending"); printf("%s: pending signals are: \n", argv[0]); printSigset(stdout, "\t\t", &pendingMask); sigemptyset(&emptyMask); /* Unblock all signals */ if (sigprocmask(SIG_SETMASK, &emptyMask, NULL) == -1) errExit("sigprocmask"); } while (!gotSigint) /* Loop until SIGINT caught */ continue; for (n = 1; n < NSIG; n++) /* Display number of signals received */ if (sigCnt[n] != 0) printf("%s: signal %d caught %d time%s\n", argv[0], n, sigCnt[n], (sigCnt[n] == 1) ? "" : "s"); exit(EXIT_SUCCESS); }
int /* Print signals currently pending for this process */ printPendingSigs(FILE *of, const char *msg) { sigset_t pendingSigs; if (msg != NULL) fprintf(of, "%s", msg); if (sigpending(&pendingSigs) == -1) return -1; printSigset(of, "\t\t", &pendingSigs); return 0; }
int /* Print mask of blocked signals for this process */ printSigMask(FILE *of, const char *msg) { sigset_t currMask; if (msg != NULL) fprintf(of, "%s", msg); if (sigprocmask(SIG_BLOCK, NULL, &currMask) == -1) return -1; printSigset(of, "\t\t", &currMask); return 0; }
int printPendingSigs(FILE *of, const char *msg) { sigset_t pendingSigs; if (msg != NULL) fprintf(of, "%s", msg); if (sigpending(&pendingSigs) == -1) return -1; printSigset(of, "\t\t", &pendingSigs); return 0; }
int printSigMask(FILE *of, const char *msg) { sigset_t currMask; if (msg != NULL) fprintf(of, "%s", msg); if (sigprocmask(SIG_BLOCK, NULL, &currMask) == -1) return -1; printSigset(of, "\t\t", &currMask); return 0; }
int main(int argc, char *argv[]) { sigset_t pending, blocked; const int numSecs = 5; struct sigaction sa; /* Set up a handler for SIGINT */ printf("Setting up handler for SIGINT\n"); sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = handler; if (sigaction(SIGINT, &sa, NULL) == -1) errExit("sigaction"); /* Block SIGINT for a while */ sigemptyset(&blocked); sigaddset(&blocked, SIGINT); if (sigprocmask(SIG_SETMASK, &blocked, NULL) == -1) errExit("sigprocmask"); printf("BLOCKING SIGINT for %d seconds\n", numSecs); sleep(numSecs); /* Display mask of pending signals */ if (sigpending(&pending) == -1) errExit("sigpending"); printf("PENDING signals are: \n"); printSigset(stdout, "\t\t", &pending); /* Now ignore SIGINT */ sleep(2); printf("Ignoring SIGINT\n"); if (signal(SIGINT, SIG_IGN) == SIG_ERR) errExit("signal"); /* Redisplay mask of pending signals */ if (sigpending(&pending) == -1) errExit("sigpending"); if (sigismember(&pending, SIGINT)) { printf("SIGINT is now pending\n"); } else { printf("PENDING signals are: \n"); printSigset(stdout, "\t\t", &pending); } sleep(2); /* Reestablish SIGINT handler */ printf("Reestablishing handler for SIGINT\n"); sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = handler; if (sigaction(SIGINT, &sa, NULL) == -1) errExit("sigaction"); sleep(2); /* And unblock SIGINT */ printf("UNBLOCKING SIGINT\n"); sigemptyset(&blocked); if (sigprocmask(SIG_SETMASK, &blocked, NULL) == -1) errExit("sigprocmask"); exit(EXIT_SUCCESS); }