IO model (epoll) - Detailed -01

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, nginx, redis, skynet most games and servers use to this multiplexing technology.

  This article will talk from the process of receiving data card series from CPU interrupt, the operating system process scheduling knowledge; and then a step by step analysis of blocking the received data, select the epoll of evolution; and finally explore the epoll implementation details. table of Contents:

First, talking about the card to receive data from
two, how do you know the data is received?
Third, the process of blocking Why not take up cpu resources?
Fourth, the whole process of receiving network data kernel
five simple method of simultaneously monitoring multiple socket
VI design ideas epoll of
seven, epoll principles and processes
eight epoll implementation details of
Nine, conclusion

First, the NIC receive data from Speaking

  The figure is a configuration diagram of a typical computer, the computer CPU, a memory (RAM), a network interface and other components. The first step in understanding the nature of epoll, from the hardware point of view how the computer receives network data.

 

   The following figure shows the process of receiving data card. In the phase ①, the NIC receive data from the network cable; ② hardware circuit through a transmission stage; eventually to write data to the memory an address (phase ③). This process involves the DMA transfer, IO channel selection and other hardware-related knowledge, but we need to know is: will the card data received into memory.

Second, how do you know the data is received?

  Epoll understand the nature of the second step, from the viewpoint of CPU data reception. To understand the problem, we must first understand a concept - interrupt.

  When the computer executes the program, there will be priority needs. For example, when the computer receives the power down signal (capacitance can save a little power, the CPU is running short for a short time), it should immediately go to save the data, save the data of the program have a higher priority.

  In general, the signal generated by the hardware needs cpu immediately respond (otherwise data might be lost), so it's a high priority. cpu should cut off the program being executed, to respond; when cpu complete response to the hardware, and then re-execute the user program. Process interrupts in the following figure, and function calls almost. Just a function call in advance will be a good position, but the position is determined by the interrupt "signal."

  With the keyboard, for example, when the user presses a button keyboard, the keyboard will emit a cpu interrupt pin high. cpu can capture the signal, and then execute the keyboard interrupt program. The following figure shows the various hardware interrupt via interaction with the cpu.

  You can now answer the questions raised in this section: When data is written to the memory card, cpu card issued to an interrupt signal, the operating system will tell you whether there is new data arrives, and then to process the data through the network card interrupt program.

Third, the process of blocking Why not take up cpu resources?

  The third step is to understand the nature of epoll, the process of scheduling the operating system from the point of view of data reception. Occlusion is a key element of the scheduling process refers to the process (e.g., network data received) in the waiting state waiting for an event occurs before, recv, select and epoll are blocking method. Understand "the process of blocking Why not take up cpu resources?", Will be able to understand this step.

  For simplicity, we begin to analyze receives from the ordinary recv, take a look at the code below:

// Create a socket 
int S = socket (AF_INET, SOCK_STREAM, 0 );   
 // Bind 
the bind (S, ...)
 // listen 
the listen (S, ...)
 // accepts client connections 
int c = the Accept ( S, ...)
 // receiving client data 
the recv (C, ...);
 // data to print out 
the printf (...)

 

  This is a most basic network programming code to the new socket object, in turn call bind, listen, accept, and finally call recv to receive data. recv is a blocking method, when the program runs to recv, it waits until it receives the data before execution down.

Work Queue

  In order to support multi-tasking operating system, implement the process of scheduling functions, the process will be divided into "Run" and "wait" and other several states. Cpu running state is the process to obtain the right to use, the code is executing state; wait state is blocked, such as the above-mentioned program runs to recv, the program will run from state to wait state, the data is received and then change back to running. Time-sharing operating system processes executing various operating conditions, due to the fast, looks like a simultaneous execution of multiple tasks.

The figure computer running A, B, C three processes, Process A wherein performing the above-described basic network program, a start, these three processes will be referenced by the operating system job queue, in operation, will be divided execution time.

Work queue has A, B and C three processes

Waiting queue

When the implementation process to create a socket A statement, the operating system creates a socket object managed by the file system (as shown below). This object contains the socket transmit buffer, receive buffer, waiting queues members. Waiting queue is a very important structure, it points to the need to wait for all processes in the socket event.

When the program executes the recv, the operating system will move from process A to the waiting queue of the work queue in the socket (see below). Because only the work queue process B and C, according to the process of scheduling, cpu will in turn execute the program both processes, the program will not execute the process A. So A process is blocked, the code will not execute down, it will not take up cpu resources.

ps: the operating system to add the waiting queue just to add a reference to the "waiting" process, in order to get the process object upon receipt of the data, to wake it up, rather than directly under the management into the process yourself. For convenience of explanation on the FIG., The process directly linked to the waiting queue below.

Wake process

When the socket receiving the data, the operating system processes on the queue waiting socket placed back into work queues, the process becomes operational state, continue to execute code. Since the socket receive buffer may also have data, recv may return the received data.

 

Guess you like

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