Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Sometimes, a process needs to simultaneously wait for I/O to become possible on one of a set of file descriptors or for the delivery of a signal. We might attempt to perform such an operation using select(), as shown in Example 63-7.
Code View:
Scroll
/
Show All sig_atomic_t gotSig = 0;
void
handler(int sig)
{
gotSig = 1;
}
int
main(int argc, char *argv[])
{
struct sigaction sa;
...
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGUSR1, &sa, NULL) == -1)
errExit("sigaction");
/* What if the signal is delivered now? */
ready = select(nfds, &readfds, NULL, NULL, NULL);
if (ready > 0) {
printf("%d file descriptors ready\n", ready);
} else if (ready == -1 && errno == EINTR) {
if (gotSig)
printf("Got signal\n");
} else {
/* Some other error */
}
...
}
|