The select () mechanism provides an fd_set data structure, which is actually a long array. Each array element can be associated with an open file handle (whether it is a Socket handle, or other files, named pipelines, or device handles. The programmer completes the connection establishment. When select () is called, the content of fd_set is modified by the kernel according to the IO status, this notifies the Socket or file that the select () process is executed. I. select function # include <sys/types. h> # include <sys/times. h> # include <sys/select. h> int select (int maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeval * timeout); ndfs: number of file handles monitored by select. Readfds: a collection of readable file handles monitored by select. Writefds: a set of writable file handles monitored by select. Except TFDs: a collection of abnormal file handles monitored by select. Timeout: the timeout end time of this select () operation. 1 s = 000000us (1 s = 10 ^ 6us 2) Detailed description of select function parameters 2.1 The number of file handles monitored by ndfs select depends on the number of files opened in the process, it is generally set to add 1 to the maximum file number in each file to be monitored. The maximum number of file descriptors that can be accommodated is specified by the constant FD_SETSIZE. 2.2 fd_set * readfds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the read changes of these file descriptors, that is, we are concerned about whether data can be read from these files. If a file in this collection is readable, select returns a value greater than 0, indicating that the file is readable, if there is no readable file, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, select returns 0. If an error occurs, a negative value is returned. You can pass in a NULL value, indicating that you do not care about any file read changes. 2.3 fd_set * writefds is the same as above. It is used to monitor whether a file can be written. 2.4 fd_set * errorfds is similar to the preceding one to monitor file error exceptions. 2.5 struct timeval * timeout is used to represent the time value. There are two members: one is the number of seconds and the other is the number of milliseconds. It is the select timeout time. This parameter is crucial. It can make the select statement in three states: 2.5.1 if NULL is input as a form parameter, that is, no time structure is input, that is, the select statement is placed in the blocking state. It must wait until a file descriptor in the collection of monitored file descriptors changes. 2.5.2 if the time value is set to 0 s and 0 ms, it becomes a pure non-blocking function. no matter whether the file descriptor changes or not, it immediately returns to continue execution. If the file does not change, 0 is returned, and a positive value is returned. 2.5.3timeout is greater than 0, this is the waiting timeout time, that is, the select statement is blocked within the timeout time, and an event will be returned within the time-out time. Otherwise, no matter how long it will be returned, the return value is the same as the above. NOTE: If timeout occurs, all Descriptors will be cleared. Therefore, it is recommended that FD_SET () be used in each while (1) loop (). 3. Several macros related to the select function FD_ZERO (fd_set * fdset): clears the contact between fdset and all file handles. FD_SET (int fd, fd_set * fdset): Establishes the connection between the file handle fd and fdset. FD_CLR (int fd, fd_set * fdset): clears the contact between the file handle fd and fdset. FD_ISSET (int fd, fd_set * fdset): checks whether the file handle fd associated with fdset can be read and written. If it is greater than 0, the file handle fd can be read and written. Struct fd_set can be understood as a collection in which the file descriptor (file descriptor) is stored, that is, the file handle. This can be what we call a common object, of course, any device, pipeline, and FIFO in Unix are all in the file format, so there is no doubt that a socket is a file, and a socket handle is a file descriptor. 4. negative value returned by the select function: select error. For details, see ERRORS. Positive Value: some files can be read and written, or ERRORS 0: wait for timeout, there is no readable or wrong file ERRORS (negative value): EBADF An invalid file descriptor was given in one of the sets. (Per-haps a file descriptor that was already closed, or one on which an error has occurred .): An invalid file descriptor is set. Eintr a signal was caught: After capturing A signal, I encountered A signal sent by the timer and failed to return. EINVAL nfds is negative or the value contained within timeout is invalid: the number of file handles is invalid or invalid during the time-out period. ENOMEMunable to allocate memory for internal tables: failed to allocate memory to the internal table 5. A simple select function example int main (void) {int sock; struct fd_set fdreads, fdwrites, fdexcepts; struct timeval timeout; char buffer [256] = {0}; // creates a socket and returns sock. ............ While (1) {// The set must be cleared for each loop. Otherwise, the descriptor changes cannot be detected (in case of timeout) FD_ZERO (& fdreads); FD_ZERO (& fdwrites ); FD_ZERO (& fdexcepts); // Add the descriptor FD_SET (sock, & fdreads); FD_SET (sock, & fdwrites); FD_SET (sock, & fdexcepts ); // set the timeout value to 1 s timeout. TV _sec = 1; timeout. TV _usec = 0; switch (select (sock + 1, & fdreads, & fdwrites, & fdexcepts, & timeout) {case-1: // select error, exit perror ("select err"); goto _ out; case 0: // select times out and polls printf ("t" Ime out! \ N "); break; default: // test whether sock is readable, writable, or fdexcepts is abnormal. Three FD_ISSET functions can be written if (FD_ISSET (sock, & fds )) {send/recv (sock, buffer, sizeof (buffer), 0); // or error handling fdexcepts }}}_ out: // clear the descriptor FD_CLR (sock, & fdreads); FD_CLR (sock, & fdwrites); FD_CLR (sock, & fdexcepts); close (sock); return-1;} Of course, functions such as poll and epoll are monitored for the set of interfaces. However, in our work, the most commonly used functions are the select function, and the last two functions are hardly touched, I didn't know the differences between them either.