The Linux Programming Interface

(nextflipdebug5) #1
Signals: Advanced Features 467

e if (sigaction(SIGINT, &sa, NULL) == -1)
errExit("sigaction");
if (sigaction(SIGQUIT, &sa, NULL) == -1)
errExit("sigaction");


r for (loopNum = 1; !gotSigquit; loopNum++) {
printf("=== LOOP %d\n", loopNum);


/* Simulate a critical section by delaying a few seconds */

printSigMask(stdout, "Starting critical section, signal mask is:\n");
for (startTime = time(NULL); time(NULL) < startTime + 4; )
continue; /* Run for a few seconds elapsed time */

printPendingSigs(stdout,
"Before sigsuspend() - pending signals:\n");
if (sigsuspend(&origMask) == -1 && errno != EINTR)
errExit("sigsuspend");
}

t if (sigprocmask(SIG_SETMASK, &origMask, NULL) == -1)
errExit("sigprocmask - SIG_SETMASK");


y printSigMask(stdout, "=== Exited loop\nRestored signal mask to:\n");


/* Do other processing... */

exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––––– signals/t_sigsuspend.c
The following shell session log shows an example of what we see when running the
program in Listing 22-5:

$ ./t_sigsuspend
Initial signal mask is:
<empty signal set>
=== LOOP 1
Starting critical section, signal mask is:
2 (Interrupt)
3 (Quit)
Type Control-C; SIGINT is generated, but remains pending because it is blocked
Before sigsuspend() - pending signals:
2 (Interrupt)
Caught signal 2 (Interrupt) sigsuspend() is called, signals are unblocked

The last line of output appeared when the program called sigsuspend(), which
caused SIGINT to be unblocked. At that point, the signal handler was called and dis-
played that line of output.
Free download pdf