1344 Chapter 63
of the connection. The use of this flag allows an application that uses the epoll edge-
triggered interface to employ simpler code to recognize a remote shutdown. (The
alternative is for the application to note that the POLLIN flag is set and then perform
a read(), which indicates the remote shutdown with a return of 0.)
63.2.4 Comparison of select() and poll()
In this section, we consider some similarities and differences between select() and poll().
Implementation details
Within the Linux kernel, select() and poll() both employ the same set of kernel-
internal poll routines. These poll routines are distinct from the poll() system call
itself. Each routine returns information about the readiness of a single file descrip-
tor. This readiness information takes the form of a bit mask whose values corre-
spond to the bits returned in the revents field by the poll() system call (Table 63-2).
The implementation of the poll() system call involves calling the kernel poll routine
for each file descriptor and placing the resulting information in the corresponding
revents field.
To implement select(), a set of macros is used to convert the information
returned by the kernel poll routines into the corresponding event types returned
by select():
#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
/* Ready for reading */
#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
/* Ready for writing */
#define POLLEX_SET (POLLPRI) /* Exceptional condition */
These macro definitions reveal the semantic correspondence between the informa-
tion returned by select() and poll(). (If we look at the select() and poll() columns in the
tables in Section 63.2.3, we see that the indications provided by each system call are
consistent with the above macros.) The only additional information we need to
complete the picture is that poll() returns POLLNVAL in the revents field if one of the
monitored file descriptors was closed at the time of the call, while select() returns –1
with errno set to EBADF.
API differences
The following are some differences between the select() and poll() APIs:
z The use of the fd_set data type places an upper limit (FD_SETSIZE) on the range of
file descriptors that can be monitored by select(). By default, this limit is 1024
on Linux, and changing it requires recompiling the application. By contrast, poll()
places no intrinsic limit on the range of file descriptors that can be monitored.
z Because the fd_set arguments of select() are value-result, we must reinitialize
them if making repeated select() calls from within a loop. By using separate
events (input) and revents (output) fields, poll() avoids this requirement.