An fd_set is a fixed size buffer. Executing FD_CLR
() or FD_SET
()
with a value of fd that is negative or is equal to or larger than
FD_SETSIZE
will result in undefined behavior. Moreover, POSIX
requires fd to be a valid file descriptor.
The operation of select
() and pselect
() is not affected by the
O_NONBLOCK
flag.
On some other UNIX systems, select
() can fail with the error
EAGAIN
if the system fails to allocate kernel-internal resources,
rather than ENOMEM
as Linux does. POSIX specifies this error for
poll(2), but not for select
(). Portable programs may wish to
check for EAGAIN
and loop, just as with EINTR
.
The self-pipe trick
On systems that lack pselect
(), reliable (and more portable)
signal trapping can be achieved using the self-pipe trick. In
this technique, a signal handler writes a byte to a pipe whose
other end is monitored by select
() in the main program. (To
avoid possibly blocking when writing to a pipe that may be full
or reading from a pipe that may be empty, nonblocking I/O is used
when reading from and writing to the pipe.)
Emulating usleep(3)
Before the advent of usleep(3), some code employed a call to
select
() with all three sets empty, nfds zero, and a non-NULL
timeout as a fairly portable way to sleep with subsecond
precision.
Correspondence between select() and poll() notifications
Within the Linux kernel source, we find the following definitions
which show the correspondence between the readable, writable, and
exceptional condition notifications of select
() and the event
notifications provided by poll(2) and epoll(7):
#define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN |
EPOLLHUP | EPOLLERR)
/* Ready for reading */
#define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT |
EPOLLERR)
/* Ready for writing */
#define POLLEX_SET (EPOLLPRI)
/* Exceptional condition */
Multithreaded applications
If a file descriptor being monitored by select
() is closed in
another thread, the result is unspecified. On some UNIX systems,
select
() unblocks and returns, with an indication that the file
descriptor is ready (a subsequent I/O operation will likely fail
with an error, unless another process reopens file descriptor
between the time select
() returned and the I/O operation is
performed). On Linux (and some other systems), closing the file
descriptor in another thread has no effect on select
(). In
summary, any application that relies on a particular behavior in
this scenario must be considered buggy.
C library/kernel differences
The Linux kernel allows file descriptor sets of arbitrary size,
determining the length of the sets to be checked from the value
of nfds. However, in the glibc implementation, the fd_set type
is fixed in size. See also BUGS.
The pselect
() interface described in this page is implemented by
glibc. The underlying Linux system call is named pselect6
().
This system call has somewhat different behavior from the glibc
wrapper function.
The Linux pselect6
() system call modifies its timeout argument.
However, the glibc wrapper function hides this behavior by using
a local variable for the timeout argument that is passed to the
system call. Thus, the glibc pselect
() function does not modify
its timeout argument; this is the behavior required by
POSIX.1-2001.
The final argument of the pselect6
() system call is not a
sigset_t * pointer, but is instead a structure of the form:
struct {
const kernel_sigset_t *ss; /* Pointer to signal set */
size_t ss_len; /* Size (in bytes) of object
pointed to by 'ss' */
};
This allows the system call to obtain both a pointer to the
signal set and its size, while allowing for the fact that most
architectures support a maximum of 6 arguments to a system call.
See sigprocmask(2) for a discussion of the difference between the
kernel and libc notion of the signal set.
Historical glibc details
Glibc 2.0 provided an incorrect version of pselect
() that did not
take a sigmask argument.
In glibc versions 2.1 to 2.2.1, one must define _GNU_SOURCE
in
order to obtain the declaration of pselect
() from <sys/select.h>.