Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

select - synchronous I/O multiplexing

&pagelevel(4)&pagelevel

Syntax

#include <sys/time.h>

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

void FD_CLR(int fd, fd_set *fdset);

int FD_ISSET(int fd, fd_set *fdset);

void FD_SET(int fd, fd_set *fdset);

void FD_ZERO(fd_set *fdset);

Description

select() checks the I/O descriptor sets that are transferred in readfds, writefds and execptfds to see whether one of their descriptors is ready for reading or writing or has an error condition pending. nfds is the number of bits to be checked in each bit mask that displays a file descriptor set. The descriptors of the descriptor sets are checked from 0 through nfds-1. On return, select replaces the given descriptor set with subsets comprising descriptors that are ready for the desired operation. The return value of the select() call is the number of descriptors that are ready.

The descriptor sets are stored as bit fields in ascending order. The following macros are available for the manipulation of such descriptor sets:

FD_ZERO(&fdset)

initializes a descriptor set fdset with the null set.

FD_SET(fd,&fdset)

inserts a descriptor fd in fdset.

FD_CLR(fd,&fdset) 

removes fd from fdset.

FD_ISSET(fd,&fdset)

is not zero if fd is an element from fdset, otherwise it is zero.

The behavior of these macros is not defined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE. FD_SETSIZE is a constant that is defined in sys/select.h and is normally at least as high as the maximum number of descriptors available from the system.

If timeout is not a null pointer, it specifies a maximum time to be waited until the selection is complete. If timeout is a null pointer, the select blocks until one of the queried events occurs. select does not block if a structure containing only null values is transferred. readfds, writefds and execptfds can be specified as null pointers if none of the descriptors is of interest.

Return val.

Number of ready descriptors in the descriptor sets



if successful.


-1

if an error occurs.


0

if the time limit was exceeded.

Errors

An error return from select can be:


EBADF

One of the I/O descriptor sets has an invalid I/O descriptor.


EINTR

A signal was issued before one of the desired events occurred, or the time limit was exceeded.


EINVAL

A component of the time limit that is referenced is outside the permitted range: t_sec must be between 0 and 100000000 inclusive. t_usec must be greater than or equal to 0 and less than or equal to 1000000.

Notes

The default value for FD_SETSIZE (currently 2048) is the same as the default limit for the number of open files. To adjust programs which use a larger number of open files with select, it is possible to increase this size within a program by defining a higher value for FD_SETSIZE before including <sys/types.h>.

In future versions of the system, select could return the time remaining from the original time limit (if there is any) if the time value is changed at the right place. It is therefore not advisable to assume that the value of the time limit will remain unchanged as a result of the select call.

The descriptor sets are always changed on return, even if the call returns as the result of a time limit.

See also

poll(), read(), write().