#include #include #include #include #include #include // modified version of Kerrisk's code. using namespace std; void catcher(int sig) { static int count = 0; cout << "Caught a Signal " << sig << " (" << strsignal(sig) << ")" << endl; cout << "Call number " << count << endl; sleep(1); if (count == 0) { count ++; cout << "Going to raise another sigusr1 in the call " << endl; raise(SIGUSR1); } cout <<"Done in signal handler " << count << endl; cout << endl; return; } int main(int argc, char *argv[]) { int x, y; sigset_t blockSet, prevMask; bool blocking; struct sigaction sa; if (argc > 1 && strchr(argv[1], 'i') != NULL) { cout << "Ignoring SIGUSR1" << endl; // ok for an ignore if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) { perror("signal"); exit(-1); } } else if (argc > 1 && strchr(argv[1], 'h') != NULL) { cout << "Catching SIGUSR1" << endl; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sa.sa_handler = catcher; if (sigaction(SIGUSR1, &sa, NULL) == -1) { perror("sigaction"); exit (-1); } } blocking = argc > 1 && strchr(argv[1], 'b') != NULL; if (blocking) { cout << "Blocking SIGUSR1" << endl; sigemptyset(&blockSet); sigaddset(&blockSet, SIGUSR1); if (sigprocmask(SIG_BLOCK, &blockSet, &prevMask) == -1) { perror("sigprocmask"); exit(-1); } } cout << "About to generate SIGUSR1\n" << endl; raise(SIGUSR1); if (blocking) { cout << "Sleeping before unblocking" << endl; sleep(2); cout << "Unblocking SIG1PE" << endl; if (sigprocmask(SIG_SETMASK, &prevMask, NULL) == -1) { perror("sigprocmask"); exit(-1); } } cout << "All Done, Exiting " << endl; cout << endl; return 0; }