IO model (epoll) - Detailed -02

EDITORIAL

  In the service-side development, and ultimately, to the contact network programming. epoll under linux as the necessary technology is critical high-performance network server, most servers use to this game multiplexing technology. The core idea of ​​the article is: let the reader clearly understand why EPOLL good performance.

Fourth, the whole process of data received by the core network

  This step, through the card, interrupts, process scheduling knowledge, narrative at blocking recv, whole kernel process the received data.

  As shown below, the blocking process during recv, the computer receives terminal data transfer (Step ①). Data transfer via the memory card (Step ②), then an interrupt is signaled by the card data arrives cpu, cpu interrupt program is executed (Step ③). Here interrupt program has two main functions, data is written to the corresponding first network receiving buffer inside the socket (Step ④), and then wake up process A (Step ⑤), again the process A into the work queue.

 

   Wake process process is shown below.

 

  The above is the whole process of the data received by the core

    Two left here thinking questions, first think about it.

    How First, the operating system knows which network data corresponding to the socket? Since a socket corresponding to a port number, and the network data packet contains information ip and port, the kernel can be found via the corresponding socket port number. Of course, in order to improve the processing speed, the operating system maintains a port number to the index structure of the socket, to quickly read.

    Second, how to simultaneously monitor multiple socket of the data? Multiplexing is a top priority, it is the focus of the second half of this article!

Fifth, a simple method while monitoring a plurality of socket

  The server need to manage multiple client connections, and recv can only monitor a single socket, this contradiction, people began looking for ways to monitor multiple socket of. epoll is the essence of effective monitoring multiple socket. From a historical perspective of the development of perspective, a less efficient way inevitably comes first, people coupled with improvements. Only first understand the less efficient way, to be able to understand the nature of epoll.

  If you can pass a socket pre-list, if the list is no data socket, suspend the process until there is a socket to receive data, wake up processes. This method is very direct, but also select the design.

  To facilitate understanding, we first review the use of select. In the following code, prepare an array (fds in the following code), so that all fds kept the need to monitor the socket. Then call select, if all socket fds is no data, select blocks until there is a socket receives data, select returns, wake up process. FDS user may traverse, which socket receives the data through the specific FD_ISSET determination and to deal with it.

Socket S = int (AF_INET, SOCK_STREAM, 0 );   
the bind (S, ...) 
the listen (S, ...) 

int FDS [] =   storage needs to be listened Socket 

the while ( . 1 ) { 
    int n- = SELECT (.. ., FDS, ...) 
    for (int I = 0 ; I <fds.count; I ++ ) { 
        IF (FD_ISSET (fds [i], ...)) {
             // the data processing fds [i] in 
        } 
    } 
}

select process

  Select realization of ideas is straightforward. If a program while monitoring sock1 following figure, sock2 sock3 and three socket, then after the call select, the operating system queue processes A were added in three socket.

When a socket to receive any data, the interrupt routine will evoke the process. The following figure shows the processing flow sock2 received data. recv and select interrupt callback can be set to different content. sock2 received the data, process the interrupt routine arouse A

The so-called evoke the process, the process is removed from all the waiting queue, added to the work queue inside. As shown below. A process will be removed from all the waiting queue, then add to the work queue inside

  Via these steps, when process A wakes up, it knows at least one socket has received the data. Just iterate over the list of programs socket, you can get ready socket.

  This simple and effective way to achieve in almost all operating systems have corresponding.

 But the simple methods often have drawbacks, mainly:

  First, each call to select the process needs to be added to all monitoring socket waiting queue, need to wake up each time removed from each queue. This involves traversing twice, and each time to pass an entire fds list to the kernel, there is some overhead. It is because of the overhead of large traversal operation, due to considerations of efficiency, the provisions will be monitored select the maximum number of default can only monitor the 1024 socket.

  After the Second, the process is awakened, the program does not know what socket receive data, but also need to traverse once.

  So is there a way to reduce the traversal? There are no ready socket of preservation methods? These two issues to be addressed is the epoll technology.

Additional information: This section explains only a situation of select. When the program calls select, the kernel will first traverse the socket again, if there is more than one socket receive buffer has data, then select the direct return, without blocking. This is why the return value of select one of the reasons there may be greater than 1. If there is no socket data, the process will be blocked.

Sixth, the design ideas of epoll

  epoll is N appear in select years after the invention, it is an enhanced version of the select and poll. epoll to improve efficiency through the following measures.

A measure: Functional separation

  One reason for inefficient is to select "Maintenance waiting queue" and "blocking the process of" two steps into one. As shown below, each call will need to select these two operations, but most application scenarios, need to be monitored relatively fixed socket, you do not need to modify every time. Epoll separate these two operations, first with epoll_ctl maintain a waiting queue, then call epoll_wait block the process. Obvious, efficiency can be enhanced.

  To facilitate the understanding of the content of the follow-up, we first review under the usage of epoll. The following code to create an object with epoll_create a epoll epfd, and then added to the epfd by epoll_ctl will need to monitor the socket, wait for the final call epoll_wait data.

Socket S = int (AF_INET, SOCK_STREAM, 0 );    
the bind (S, ...) 
the listen (S, ...) 

int the epfd = epoll_create (...); 
epoll_ctl (the epfd, ...); // The All required listening socket in epfd added to 

the while ( . 1 ) { 
    int n- = epoll_wait (...) 
    for (socket received data) {
         // processing 
    } 
}

 

  Functional separation, making it possible to optimize the epoll have.

Measures II: the ready list

  Another reason is that inefficient select does not know which socket receives the data, only one traversal. If the kernel maintains a "ready list" references socket receive data, you can avoid traversal. Shown, there are three computer socket as shown below, and the received data sock2 sock3 are rdlist (ready list) referenced. When the process is awakened, as long as access to content rdlist, it is possible to know which socket receive data. Ready schematic list

 

Guess you like

Origin www.cnblogs.com/chihirotan/p/11521065.html