SELECT(2)

NAME
select - synchronous i/o multiplexing

SYNOPSIS
#include <sys/types.h>
#include <sys/time.h>

nfound = select(nfds, readfds, writefds, execptfds, timeout)
int nfound, nfds;
fd_set *readfds, *writefds, *execptfds;
struct timeval *timeout;

FD_SET(fd, &fdset)
FD_CLR(fd, &fdset)
FD_ISSET(fd, &fdset)
FD_ZERO(&fdset)
int fd;
fd_set fdset;

DESCRIPTION
Select examines the i/o descriptors specified by the bit masks readfds, writefds, and execptfds to see if they are ready for reading, writing, or have an exceptional condition pending, respectively. File descriptor f is represented by the bit “1<<f” in the mask. Nfds descriptors are checked, i.e. the bits from 0 through nfds-1 in the masks are examined. Select returns, in place, a mask of those descriptors which are ready. The total number of ready descriptors is returned in nfound.

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 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 nonzero 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-zero pointer, it specifies a maximum interval to wait for the selection to complete. If timeout is a zero pointer, the select blocks indefinitely. To affect a poll, the timeout argument should be non-zero, pointing to a zero valued timeval structure.

Any of readfds, writefds, and execptfds may be given as 0 if no descriptors are of interest.

RETURN VALUE
Select returns the number of descriptors which are contained in the bit masks, or -1 if an error occurred. If the time limit expires then select returns 0.

ERRORS
An error return from select indicates:

[EBADF]
    One of the bit masks specified an invalid descriptor.

[EINTR]
    An signal was delivered before any of the selected for events occurred or the time limit expired.

[EFAULT]
    The memory pointed to by readfds, writefds, exceptfds, or timeout lies outside the valid address space for the process.

SEE ALSO
accept(2), connect(2), read(2), write(2), recv(2), send(2), getdtablesize(2)

BUGS
Although the provision of getdtablesize(2) was intended to allow user programs to be written independent of the kernel limit on the number of open files, the dimension of a sufficiently large bit field for select remains a problem. The default size FD_SETSIZE (currently 256) is somewhat larger than the current kernel limit to the number of open files. However, in order to accommodate programs which might potentially use a larger number of open files with select, it is possible to increase this size within a program by providing a larger definition of FD_SETSIZE before the inclusion of <sys/types.h>.

The descriptor masks are always modified on return, even if the call returns as the result of the timeout.