From <<Linux programming unleash>>
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);
doesn’t have to waste time checking a bunch of unused bits). The second, third, and
fourth parameters are pointers to file descriptor sets (one bit per possible file descriptor)
that indicate which file descriptors you would like to be able to read, write, or receive
exception notifications on, respectively. The last parameter is a timeout value. All but the
first parameter may be null. On return the file descriptor sets will be modified to indicatewhich descriptors are ready for immediate I/O operations. The timeout will also be modified
on return, although that is not the case on most systems other than Linux. The return
value itself will indicate a count of how many descriptors are included in the descriptor
sets. If it is zero, that indicates a timeout. If the return value is -1, errno will be set to
indicate the error (which may include EINTR if a signal was caught).
The macros FD_ZERO(), FD_SET(), FD_CLEAR, and FD_ISSET() help manipulate file
descriptor sets by erasing the whole set, setting the bit corresponding to a file descriptor,
clearing the bit, or querying the bit. All but FD_ZERO() take a file descriptor as the first
parameter. The remaining parameter for each is a pointer to a file descriptor set.
Application:
1) Call macro FD_ZERO clear fd_set;
2) Call macro FD_SET to add specified fd into fd_set;
3) Call function select to test all fds in teh fd_set. (note: set timeout)
4) Call macro FD_ISSET to test if the fd is still in fd_set after calling select。