Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created July 23, 2008 20:20
Show Gist options
  • Save ELLIOTTCABLE/1887 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/1887 to your computer and use it in GitHub Desktop.
DESCRIPTION
Select() examines the I/O descriptor sets whose addresses are passed in readfds, writefds, and errorfds 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. (Example: If you have set two file descriptors "4" and "17", nfds should
not be "2", but rather "17 + 1" or "18".) On return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are
ready for the requested operation. Select() returns 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 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. FD_COPY(&fdset_orig, &fdset_copy) replaces
an already allocated &fdset_copy file descriptor set with a copy of &fdset_orig. 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-nil pointer, it specifies a maximum interval to wait for the selection to complete. If timeout is a nil pointer, the select blocks
indefinitely. To effect a poll, the timeout argument should be non-nil, pointing to a zero-valued timeval structure. 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().
DESCRIPTION
select() and pselect() allow a program to monitor multiple file
descriptors, 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 the corre‐
sponding I/O operation (e.g., read(2)) without blocking.
The operation of select() and pselect() is identical, with three dif‐
ferences:
(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. Those 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 particu‐
lar, a file descriptor is also ready on end-of-file), those in writefds
will be watched to see if a write will not block, and those in
exceptfds will be watched for exceptions. On exit, the sets are modi‐
fied in place to indicate which file descriptors actually changed sta‐
tus. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment