Alternative I/O Models 1353
(In order to obtain the definitions of the F_SETSIG and F_GETSIG constants from
Using F_SETSIG to change the signal used for “I/O possible” notification serves
two purposes, both of which are needed if we are monitoring large numbers of I/O
events on multiple file descriptors:
z The default “I/O possible” signal, SIGIO, is one of the standard, nonqueuing
signals. If multiple I/O events are signaled while SIGIO is blocked—perhaps
because the SIGIO handler is already invoked—all notifications except the first
will be lost. If we use F_SETSIG to specify a realtime signal as the “I/O possible”
signal, multiple notifications can be queued.
z If the handler for the signal is established using a sigaction() call in which the
SA_SIGINFO flag is specified in the sa.sa_flags field, then a siginfo_t structure is
passed as the second argument to the signal handler (Section 21.4). This struc-
ture contains fields identifying the file descriptor on which the event occurred,
as well as the type of event.
Note that the use of both F_SETSIG and SA_SIGINFO is required in order for a valid
siginfo_t structure to be passed to the signal handler.
If we perform an F_SETSIG operation specifying sig as 0, then we return to the
default behavior: SIGIO is delivered, and a siginfo_t argument is not supplied to the
handler.
For an “I/O possible” event, the fields of interest in the siginfo_t structure
passed to the signal handler are as follows:
z si_signo: the number of the signal that caused the invocation of the handler.
This value is the same as the first argument to the signal handler.
z si_fd: the file descriptor for which the I/O event occurred.
z si_code: a code indicating the type of event that occurred. The values that can
appear in this field, along with their general descriptions, are shown in Table 63-7.
z si_band: a bit mask containing the same bits as are returned in the revents field
by the poll() system call. The value set in si_code has a one-to-one correspon-
dence with the bit-mask setting in si_band, as shown in Table 63-7.
In an application that is purely input-driven, we can further refine the use of F_SETSIG.
Instead of monitoring I/O events via a signal handler, we can block the nominated
“I/O possible” signal, and then accept the queued signals via calls to sigwaitinfo() or
Table 63-7: si_code and si_band values in the siginfo_t structure for “I/O possible” events
si_code si_band mask value Description
POLL_IN POLLIN | POLLRDNORM Input available; end-of-file condition
POLL_OUT POLLOUT | POLLWRNORM | POLLWRBAND Output possible
POLL_MSG POLLIN | POLLRDNORM | POLLMSG Input message available (unused)
POLL_ERR POLLERR I/O error
POLL_PRI POLLPRI | POLLRDNORM High-priority input available
POLL_HUP POLLHUP | POLLERR Hangup occurred