select() and
pselect() examine the I/O descriptor sets whose addresses are passed in
readfds,
writefds, and
exceptfds to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The first
nfds descriptors are checked in each set; i.e., the descriptors from 0 through
nfds-1 in the descriptor sets are examined. This means that
nfds must be set to the highest file descriptor of the three sets, plus one. On return,
select() and
pselect() replace the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation.
select() and
pselect() return the total number of ready descriptors in all the sets.
The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulating such descriptor sets:
FD_ZERO(
fdset) initializes a descriptor set pointed to by
fdset to the null set.
FD_SET(
fd,
fdset) includes a particular descriptor
fd in
fdset.
FD_CLR(
fd,
fdset) removes
fd from
fdset.
FD_ISSET(
fd,
fdset) is non-zero if
fd is a member of
fdset, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to
FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system.
If
timeout is a non-null pointer, it specifies a maximum interval to wait for the selection to complete. If
timeout is a null pointer, the select blocks indefinitely. To poll without blocking, the
timeout argument should be non-null, pointing to a zero-valued timeval or timespec structure, as appropriate.
timeout is not changed by
select(), and may be reused on subsequent calls; however, it is good style to re-initialize it before each invocation of
select().
If
sigmask is a non-null pointer, then the
pselect() function shall replace the signal mask of the caller by the set of signals pointed to by
sigmask before examining the descriptors, and shall restore the signal mask of the calling thread before returning.
Any of
readfds,
writefds, and
exceptfds may be given as null pointers if no descriptors are of interest.