IO model (epoll) - Detailed -03

EDITORIAL

  linux epoll is to develop the necessary technology to high-performance servers, epoll nature, must master knowledge of server-side programmers.

Seven, epoll principles and processes

  This section will be examples and diagrams to explain epoll principles and processes.

Objects created epoll

  As shown below, when a process calls epoll_create method, the kernel creates a eventpoll objects (ie represented by the object program epfd). eventpoll the object is a file system, and socket as it will have to wait queue. Kernel objects create eventpoll

 

 

   The object is to create a representative of eventpoll epoll is necessary, because the kernel to maintain "ready list" data, "ready list" as a member of eventpoll.

Maintenance Watchlist

  After you create epoll object, you can use epoll_ctl additions or deletions to listen socket. Socket to add as an example, as shown below, is added to monitor if sock1, sock2 and sock3 by epoll_ctl, the kernel will eventpoll added to the queue in three socket. Adding to be listening socket

 

 

   When the socket receives the data, the program will interrupt the operation eventpoll object, rather than directly manipulate the process.

Receive data

  When the socket receives the data, interrupt program will eventpoll the "ready list" add socket reference. The following figure shows that sock2 and sock3 after receiving the data, interrupt program lets rdlist references both socket. Add a reference to the ready list

 

   

  eventpoll target is equivalent to an intermediary between the socket and processes data received socket does not directly affect the process, but to change the process by changing the state of the ready list eventpoll.

  When the program execution to epoll_wait, if rdlist already cited socket, then epoll_wait direct return, if rdlist is empty, block the process.

Blocking and wake-up process

  Assuming that your computer is running processes A and B, run to a statement at some point epoll_wait process A. As shown below, Process A is placed in the kernel queue eventpoll, the blocking process. epoll_wait blocking process

 

 

  When the socket receives the data, on the one hand to modify the interrupt program rdlist, on the other hand eventpoll wakeup process waiting queue, the process again enters the running state A (below). But also because of the presence of rdlist, process A can know which socket has changed. epoll wake-up process

 

 

Eight, the implementation details of epoll

At this point, I believe readers nature epoll already have some knowledge. But we also left a question, the data structure eventpoll what it was like?

To stay two questions, what should the ready queue data structure should be used? eventpoll What should you use to manage data structures by adding or removing epoll_ctl the socket?

 As shown below, eventpoll includes lock, mtx, wq (wait queue), rdlist like members. rdlist and rbr is our concern.

 

 

Ready list data structure

  Ready ready list of references with the socket, so it should be able to quickly insert data.

  Epoll_ctl program may call at any time to add monitoring socket, also subject to deletion. When you delete, if the socket is already stored in the list is ready, it should be removed.

  It should be a ready list to quickly insert and delete data structure. Doubly linked list is a data structure, epoll be implemented using a doubly linked list in the ready queue (corresponding to rdllist on the drawing).

Index Structure

  Since the epoll "maintenance monitoring queue" and "blocking the process of" separation, but also implies the need to have a data structure to hold the socket monitored. At least to easily add and remove, but also easy to search, to avoid duplication added. Red-black tree is a self-balancing binary search tree, search, insert, and delete time complexity is O (log (N)), the efficiency is better. epoll used as a red-black tree index structure (corresponding to FIG on the RBR).

 

ps: because the operating system to take into account a variety of functions, as well as by the need to save more data, rdlist not directly referenced socket, but indirectly referred to by epitem, node red-black tree is epitem object. Similarly, the file system is not a direct reference to the socket. To facilitate understanding, herein we omit some indirect structure.

IX. Conclusion

the epoll (poll and select basically the same, there are minor modifications) in the select and poll basis eventpoll introduced as an intermediate layer, uses advanced data structures, it is an efficient multiplexing technique.

To stay a little work!

The following table is a common table describes the differences select, poll and epoll of. After reading this article, readers can explain why the time complexity select and epoll is O (n) and O (1)?

 

 

Guess you like

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