Select Module function: Select System call is used to detect multiple file description state changes, the program will always wait in the select until the time-out or the monitored file descriptor in one or more state changes.
Select function: Select (Rlist,wlist,xlist[,timeout]), return value: (rlist,wlist,xlist)
Rlist: Read the socket list to determine if there is a readable socket;
Wlist: Write the socket list to determine if there is a socket that can be written;
Xlist: Abnormal socket list, to determine if there is an abnormal socket;
If a socket can be read, writable, or abnormal, select returns the corresponding socket list
Select how to determine the readable:
- Add the detected socket to the rlist, and then call Select to wait for the data;
- If there is a client connection or data, then select will return immediately;
- If it is a new link call accept accepts a new socket and counts the socket into rlist or wlist;
- If there is data, then receive the data;
Select how to determine what can be written:
- The detected socket is added to the wlist, and the select Wait is called;
- If the socket can be written, return the list of sockets that can be written;
- Call the Send method to send the data;
Select how to determine the exception:
- The detected socket is added to the xlist, and the select Wait is called;
- If the socket has an exception, the Xlist list of exceptions is returned;
- handle abnormal sockets;
Select Multiplexing server Advantages and disadvantages:
Advantages:
- There is no need to create and destroy processes and threads frequently, which saves the overhead and burden of the system.
- Select uses rotation method to process the data, processing efficiency is higher than multi-process and multi-threading model;
Disadvantages:
- Maximum file descriptor for a process monitor (system default of 1024)
- Need to maintain a list of file descriptors
- The scan for file descriptors is linear, and the time is increased each time the structure is scanned
- The kernel notifies the user space of the file descriptor message to be copied;
Epoll mode
- is an enhanced version of the Select/poll MUX IO interface in Linux
- The maximum number of files that can be opened is the upper limit of the file descriptors it supports
- Epoll only operates on "active" sockets and does not result in a linear decrease in efficiency due to the increase in file descriptors
- Epoll is implemented by the kernel in the user space mmap the same piece of memory, using MMAP to accelerate the kernel and user space message delivery;
Multiplexed Server Model