ptg10805159
Section 14.4 I/O Multiplexing 503
tvptr−>tv_sec!= 0 ||tvptr−>tv_usec!= 0
Wait the specified number of seconds and microseconds. Return is made when
one of the specified descriptors is ready or when the timeout value expires. If
the timeout expires beforeany of the descriptors is ready,the return value is 0.
(If the system doesn’t provide microsecond resolution, thetvptr−>tv_usecvalue
is rounded up to the nearest supported value.) As with the first condition, this
wait can also be interrupted by a caught signal.
POSIX.1 allows an implementation to modify thetimevalstructure, so afterselectreturns,
you can’t rely on the structurecontaining the same values it did beforecallingselect.
FreeBSD 8.0, Mac OS X 10.6.8, and Solaris 10 all leave the structureunchanged, but Linux 3.2.0
will update it with the time remaining ifselectreturns beforethe timeout value expires.
The middle three arguments—readfds, writefds,and exceptfds—arepointers to
descriptor sets.These three sets specify which descriptors we’reinterested in and for
which conditions (readable, writable, or an exception condition). Adescriptor set is
stored in anfd_setdata type. This data type is chosen by the implementation so that
it can hold one bit for each possible descriptor.Wecan consider it to be just a big array
of bits, as shown in Figure14.15.
readfds 0 0 0 ...
writefds 0 0 0 ...
exceptfds 0 0 0 ...
fd 0 fd 1 fd 2
one bit per possible descriptor
fd_setdata type
Figure 14.15 Specifying the read, write, and exception descriptors forselect
The only thing we can do with thefd_setdata type is allocate a variable of this
type, assign a variable of this type to another variable of the same type, or use one of the
following four functions on a variable of this type.
#include <sys/select.h>
int FD_ISSET(int fd,fd_set *fdset);
Returns: nonzeroiffdis in set, 0 otherwise
void FD_CLR(int fd,fd_set *fdset);
void FD_SET(int fd,fd_set *fdset);
void FD_ZERO(fd_set *fdset);