man selectSELECT(2) Linux Programmer's Manual SELECT(2)
NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
multiplexing
SYNOPSIS
/* According to POSIX.1-2001, POSIX.1-2008 */
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
#include <sys/select.h>
int pselect(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, const struct timespec *timeout,
const sigset_t *sigmask);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
pselect(): _POSIX_C_SOURCE >= 200112L
DESCRIPTION
select() and pselect() allow a program to monitor multiple file de‐
scriptors, waiting until one or more of the file descriptors become
"ready" for some class of I/O operation (e.g., input possible). A file
descriptor is considered ready if it is possible to perform a corre‐
sponding I/O operation (e.g., read(2) without blocking, or a suffi‐
ciently small write(2)).
select() can monitor only file descriptors numbers that are less than
FD_SETSIZE; poll(2) does not have this limitation. See BUGS.
The operation of select() and pselect() is identical, other than these
three differences:
(i) select() uses a timeout that is a struct timeval (with seconds
and microseconds), while pselect() uses a struct timespec (with
seconds and nanoseconds).
(ii) select() may update the timeout argument to indicate how much
time was left. pselect() does not change this argument.
(iii) select() has no sigmask argument, and behaves as pselect()
called with NULL sigmask.
Three independent sets of file descriptors are watched. The file de‐
scriptors listed in readfds will be watched to see if characters become
available for reading (more precisely, to see if a read will not block;
in particular, a file descriptor is also ready on end-of-file). The
file descriptors in writefds will be watched to see if space is avail‐
able for write (though a large write may still block). The file de‐
scriptors in exceptfds will be watched for exceptional conditions.
(For examples of some exceptional conditions, see the discussion of
POLLPRI in poll(2).)
On exit, each of the file descriptor sets is modified in place to indi‐
cate which file descriptors actually changed status. (Thus, if using
select() within a loop, the sets must be reinitialized before each
call.)
Each of the three file descriptor sets may be specified as NULL if no
file descriptors are to be watched for the corresponding class of
events.
Four macros are provided to manipulate the sets. FD_ZERO() clears a
set. FD_SET() and FD_CLR() respectively add and remove a given file
descriptor from a set. FD_ISSET() tests to see if a file descriptor is
part of the set; this is useful after select() returns.
nfds should be set to the highest-numbered file descriptor in any of
the three sets, plus 1. The indicated file descriptors in each set are
checked, up to this limit (but see BUGS).
The timeout argument specifies the interval that select() should block
waiting for a file descriptor to become ready. The call will block un‐
til either:
* a file descriptor becomes ready;
* the call is interrupted by a signal handler; or
* the timeout expires.
Note that the timeout interval will be rounded up to the system clock
granularity, and kernel scheduling delays mean that the blocking inter‐
val may overrun by a small amount. If both fields of the timeval
structure are zero, then select() returns immediately. (This is useful
for polling.) If timeout is NULL (no timeout), select() can block in‐
definitely.
sigmask is a pointer to a signal mask (see sigprocmask(2)); if it is
not NULL, then pselect() first replaces the current signal mask by the
one pointed to by sigmask, then does the "select" function, and then
restores the original signal mask.
Other than the difference in the precision of the timeout argument, the
following pselect() call:
ready = pselect(nfds, &readfds, &writefds, &exceptfds,
timeout, &sigmask);
is equivalent to atomically executing the following calls:
sigset_t origmask;
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
The reason that pselect() is needed is that if one wants to wait for
either a signal or for a file descriptor to become ready, then an
atomic test is needed to prevent race conditions. (Suppose the signal
handler sets a global flag and returns. Then a test of this global
flag followed by a call of select() could hang indefinitely if the sig‐
nal arrived just after the test but just before the call. By contrast,
pselect() allows one to first block signals, handle the signals that
have come in, then call pselect() with the desired sigmask, avoiding
the race.)
The timeout
The time structures involved are defined in <sys/time.h> and look like
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
and
struct timespec {
long tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
(However, see below on the POSIX.1 versions.)
Some code calls select() with all three sets empty, nfds zero, and a
non-NULL timeout as a fairly portable way to sleep with subsecond pre‐
cision.
On Linux, select() modifies timeout to reflect the amount of time not
slept; most other implementations do not do this. (POSIX.1 permits ei‐
ther behavior.) This causes problems both when Linux code which reads
timeout is ported to other operating systems, and when code is ported
to Linux that reuses a struct timeval for multiple select()s in a loop
without reinitializing it. Consider timeout to be undefined after se‐
lect() returns.
RETURN VALUE
On success, select() and pselect() return the number of file descrip‐
tors contained in the three returned descriptor sets (that is, the to‐
tal number of bits that are set in readfds, writefds, exceptfds) which
may be zero if the timeout expires before anything interesting happens.
On error, -1 is returned, and errno is set to indicate the error; the
file descriptor sets are unmodified, and timeout becomes undefined.
ERRORS
EBADF An invalid file descriptor was given in one of the sets. (Per‐
haps a file descriptor that was already closed, or one on which
an error has occurred.) However, see BUGS.
EINTR A signal was caught; see signal(7).
EINVAL nfds is negative or exceeds the RLIMIT_NOFILE resource limit
(see getrlimit(2)).
EINVAL The value contained within timeout is invalid.
ENOMEM Unable to allocate memory for internal tables.
VERSIONS
pselect() was added to Linux in kernel 2.6.16. Prior to this, pse‐
lect() was emulated in glibc (but see BUGS).
CONFORMING TO
select() conforms to POSIX.1-2001, POSIX.1-2008, and 4.4BSD (select()
first appeared in 4.2BSD). Generally portable to/from non-BSD systems
supporting clones of the BSD socket layer (including System V vari‐
ants). However, note that the System V variant typically sets the
timeout variable before exit, but the BSD variant does not.
pselect() is defined in POSIX.1g, and in POSIX.1-2001 and POSIX.1-2008.
NOTES
[...]