|
|
Description
The select() function tests three different sets of socket descriptors passed with the readfds, writefds and exceptfds parameters.
More general information on this function is available in the manual "C Library Functions for POSIX Applications".
select() determines
which descriptors in the set passed with readfds are ready for reading,
which descriptors in the set passed with writefds are ready for writing,
for which descriptors in the set passed with exceptfds a pending exception exists.
The descriptor sets are stored as bit fields in Integer strings. The size of the bit fields (and descriptors) is defined by the FD_SETSIZE constant. FD_SETSIZE is defined in <sys/select.h> with a value which is at least as large as the maximum number of descriptors supported by the system.
The width parameter specifies the number of bits to be tested in each bit mask. select() tests bits 0 to width-1 in each bit mask. width normally has the value supplied by the ulimit() function as the maximum number of socket descriptors. The ulimit() function is described in the manual "C Library Functions for POSIX Applications".
select() replaces the descriptor sets passed at the call with corresponding subsets. These subsets each contain all descriptors that are ready for the operation concerned.
You can use the following macros to manipulate or check bit masks or descriptor sets:
FD_ZERO(&fdset)
Initializes the descriptor set fdset as an empty set.
FD_SET(fd, &fdset)
Extends the descriptor set fdset by descriptor fd.
FD_CLR(fd, &fdset)
Removes descriptor fd from descriptor set fdset.
FD_ISSET(fd, &fdset)
Tests whether descriptor fd is a member of descriptor set fdset:
Return value !
Return value == 0: fd is not a member of fdset.
The behavior of these macros is undefined if the descriptor value is <0 or ≥ FD_SETSIZE.
The timeout parameter defines the maximum time that the select() function has for selection of the ready descriptors. If timeout is the null pointer, select() blocks for an infinite time. You can enable polling by passing as timeout a pointer to a timeval object whose components all have the value 0.
If no descriptors are of interest, the null pointer can be passed as the current parameter for readfds, writefds or exceptfds.
If select() determines the read readiness of a socket descriptor after calling listen(), this indicates that a subsequent accept() call for this descriptor will not block.
Return value
>0:
The positive number indicates the number of ready descriptors in the descriptor set.
0:
Indicates that the timeout limit has been exceeded.
-1:
If errors occur. errno is set to indicate the error. The descriptor sets are then not changed in that case.
Errors
EBADF
One of the descriptor sets specified an invalid descriptor.
EFAULT
One of the pointers that were passed points to a non-existent area in the process address range.
EINTR
A signal was received before one of the selected events arrived or before the time limit expired.
EINVAL
A component of the specified time limit is outside the valid range.
The valid range is defined as follows:
0 <= t_sec <= 108
0 <= t_usec < 106
Note
In rare circumstances, select() can indicate that a descriptor is ready for writing while a write attempt would actually block. This can occur If the system resources required for writing are exhausted or not present. If it is critical for your application that writes to a file descriptor do never block, you should set the descriptor to non-blocking input/output with a fcntl() call.
See also
accept(), connect(), listen(), recv(), send(), fcntl(), read(), write(), ulimit() in "C Library Functions for POSIX Applications"