10 select, poll and epoll

IO multiplexing: To explain this term, at first to understand the concept of reuse, reuse is common sense, this understanding is still somewhat abstract, for the next we come to understand the use of multiplexing in the communications field, 
  in the field of communication in order to make full use of the physical medium connected to the network, often using time division multiplexing or frequency division multiplexing techniques on the same network link so that transmission of multiple signals on the same link, here we understand complex substantially meaning used,
  namely the public a "medium" as much as possible to do the same type (nature) of things, what "medium" that IO multiplexing is it? To this end we first look at server programming model, the client sent a request to the server process will have to be a service,
  whenever a client request is to produce a process to serve, but the process can not be unlimited produce, so in order to solve the problem of a large number of client access, the introduction of IO multiplexing, namely: a process can simultaneously service multiple client requests.
  That "media" IO multiplexing is the process (to be exact multiplexing is select and poll, because the process is by calling select and poll realized), a multiplexing process (select and poll) to multiple IO services performed,
  although the client is sent to the IO but concurrent read and write data at the required IO in most cases is not ready, so you can use a function (select and poll) to monitor the data required for IO the state, once the IO data can be read and written, the
  process will be carried out to such IO service.
After complete understanding of IO multiplexing, we distinguish IO multiplexing at the three API (select, poll and epoll) implementation and links view   select, mechanisms poll, epoll IO are multiplexed, the I
/ O multiplexing is achieved by a mechanism, can monitor multiple descriptors, descriptor once a ready (ready generally read or write-ready) ,
  possible to notify the application corresponding read and write operations. But they are synchronous I / O the select, poll, epoll essence, because they need to read and write after the event is ready responsible for their own reading and writing, that the reading and writing process is blocked,
  and asynchronous I / O is no need to own responsible for reading and writing, asynchronous the I / O responsible for implementation of the data copied from the kernel space to the user. Three prototype is as follows:     int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
    int poll(struct pollfd
*fds, nfds_t nfds, int timeout);     epoll_wait int (the epfd int, struct epoll_event * Events, maxevents int, int timeout); The first parameter is fdset nfds 1.select set the maximum value plus the descriptor. 1, fdset is a bit array size is limited to __FD_SETSIZE (1024 ), the number of bits of each bit group corresponding to the representative descriptors need to be checked.
  The second parameter indicates thirty-four need to focus on reading, writing and file descriptor digit error event group, these parameters both an input parameter is output parameters, may be used to identify modified kernel event of interest has occurred on which descriptors,
  so every You need to re-initialize fdset times before calling select. as timeout timeout parameter, the core structure is modified, the value timeout remaining.   select the call as follows:   (
1 ) using copy_from_user copied from user space to kernel space fdset   ( 2 ) to register a callback function __pollwait   ( 3 ) through all fd, which calls the corresponding poll method (for socket, the poll is sock_poll, sock_poll some cases calls to tcp_poll, udp_poll or datagram_poll)   ( 4 ) to tcp_poll example, its core implementation is __pollwait, which is registered above the callback function.   ( 5) __pollwait main job is to Current (current process) linked to the waiting queue devices, different devices have different waiting queue for tcp_poll, its queue is sk-> sk_sleep (note that the process to hang waiting in the queue does not mean that the process has been sleeping). If the device receives a message (network equipment) or filling out the file data (disk device), the device will wake up sleeping processes waiting on the queue, then the current will be awakened.   ( . 6 ) describes a method returns poll whether a read or write operation is ready to return when the mask mask, the mask according to the mask fd_set assignment.   ( 7 ) If completed through all fd, also did not return a readable and writable mask mask will be called schedule_timeout calling select process (that is, current) goes to sleep. When the device driver can read and write their own resources occurs, it will wake up the sleeping process waiting on the queue. If more than a certain timeout (schedule_timeout specified), or no one wakes up, the process is called re-select the wake up get CPU, and then re-iterate fd, judges have no ready fd.   ( 8 ) fd_set copied from the kernel space to user space. Summary of several major disadvantages under select:   ( 1 ) each call to select, need to copy fd collection from user mode to kernel mode, this overhead will be great when many fd   ( 2 ) at the same time each call fd select all need to traverse passed in the kernel, this overhead is also great when many fd   ( 3 ) the number of file descriptors select support is too small, the default is 1024 2 . poll and select different events through a concern pollfd array transmitted to the kernel, it does not describe the number of qualifiers, events and the revents pollfd field for indicating respectively the events of interest and events,
 so that the array requires only pollfd It is initialized once. poll and select implementation mechanism similar to the corresponding kernel sys_poll, but passed to the kernel poll pollfd array, then each pollfd descriptors poll, compared fdset process, the more efficient poll.
 After the poll returns, you need to check the value of revents pollfd each element, comes refers to whether the event occurred.
3. Linux2.6 until there came the realization of direct support by the kernel, that is, epoll, is recognized as the best performance under Linux2.6 multi-channel I / O readiness notification method. epoll can simultaneously support level trigger and edge trigger
  (Edge Triggered, just tell which file descriptors process has just become ready, only to say it again, if we do not take action, it will not be told again, this is called edge-triggered),
  theoretically trigger edge performance to be higher, but the code is quite complicated to achieve. epoll file descriptor is also only inform those ready, and when we call epoll_wait () to get ready file descriptors, not the actual return of descriptor,
  but on behalf of a number of ready descriptors value, you just need to specify the epoll in order to obtain an array corresponding to the number of file descriptors, which also uses memory mapping (mmap) technology,
  this will completely dispense with the overhead of copying these file descriptors in the system call. Another improvement is that the nature of the use of ready epoll event-based notification methods. In select / after poll, the calling process only certain way,
  the kernel fishes monitor all file descriptors to scan, prior to registering a epoll file descriptor by epoll_ctl (), based on a file descriptor once ready , the kernel will use a callback mechanism similar to the callback, the
  rapid activation of this file descriptors, when the process calls epoll_wait () will be notified. Since it is improved epoll select and poll, it should be able to avoid the above-mentioned three drawbacks. That epoll is how to solve it? Prior to this, we look at the different, the call interface and select epoll and poll the
    select and poll only provides a function --select or poll function. The epoll provides three functions, epoll_create, epoll_ctl and epoll_wait, epoll_create is to create a epoll handle;
    epoll_ctl is registered to listen for the event type; epoll_wait is a wait event.   For the first drawback, epoll solutions epoll_ctl function. Every time a new event to register (designated EPOLL_CTL_ADD in epoll_ctl in) when epoll handle, the fd will all be copied into the kernel,
    rather than epoll_wait when a duplicate copy. epoll fd ensure that each copy only once in the whole process.   For the second drawback, like the epoll solutions select or poll every time the same current device turns corresponding to fd added waiting queue only when epoll_ctl hang over the current (which again is essential)
    and for each a fd specify a callback function when the device is ready, waiting for those who wake up waiting on the queue, it will call the callback function, and this callback function will be ready to join a ready list of fd).
    epoll_wait actually works is to look at the ready list, there is no ready fd (use schedule_timeout () to achieve sleep for a while, step 7 will be the effect of a judgment, and select implementations are similar).   For the third disadvantage, epoll not have this limitation, FD it supports the upper limit is the maximum number of files can be opened, this number is generally much larger than 2048, for example, on the 1GB memory machines is about 100,000,
    the specific number can CAT
/ proc / SYS / FS / File- max view, in general, this is a big number and the relationship between the system memory. to sum up: ( 1 ) the SELECT, needs its own poll achieve continuously polls all fd collection until the device is ready, you may want to sleep and wake up several times during the alternate. And in fact, need to call epoll epoll_wait constantly polling the ready list,
  it may alternately repeatedly during sleep and wake, but it is when the device is ready, call the callback function, fd ready to put in the ready list, goes to sleep and wakes up in the epoll_wait process. Although every sleep and alternately,
  but select and poll in the "awake" time to traverse the entire collection fd, and epoll in the "awake" when just determine what the ready list is empty on the line, which saves a lot of CPU time, which is to bring the callback mechanism to enhance performance. (
2) SELECT, poll each call must be set to fd from user mode to kernel mode Copy Once, current to the device and make a queue waiting to hang, and epoll just one copy,
  but the current queue waiting to be hung only hang once (at the beginning of epoll_wait, pay attention to where the equipment is not waiting queue waiting queue, just a epoll defined within the waiting queue), which can save a lot of overhead.

  All three models have a different IO multiplexing of different platforms to support, but do not support epoll under windows, but fortunately we have selectors module, to help us choose a default under the most appropriate platform for the current, we just write monitor who, then how to send messages to receive messages, but how to listen, choose the select or poll or epoll, this is the selector to help us automatically selected.

 1 #服务端
 2 from socket import *
 3 import selectors
 4 
 5 sel=selectors.DefaultSelector()
 6 def accept(server_fileobj,mask):
 7     conn,addr=server_fileobj.accept()
 8     sel.register(conn,selectors.EVENT_READ,read)
 9 
10 def read(conn,mask):
11     try:
12         data=conn.recv(1024)
13         if not data:
14             print('closing',conn)
15             sel.unregister(conn)
16             conn.close()
17             return
18         conn.send(data.upper()+b'_SB')
19     except Exception:
20         print('closing', conn)
21         sel.unregister(conn)
22         conn.close()
23 
24 
25 
26 server_fileobj=socket(AF_INET,SOCK_STREAM)
27 server_fileobj.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
28 server_fileobj.bind((' 127.0.0.1 ' , 8088 ))
 29 server_fileobj.listen (. 5 )
 30 server_fileobj.setblocking (False) # set as nonblocking socket interface 
31 is sel.register (server_fileobj, selectors.EVENT_READ, Accept) # corresponds to the select network append a list read file handle server_fileobj, and binds a callback function Accept 
32  
33 is  the while True:
 34 is      Events sel.select = () # detect all fileobj, for the completion of the wait data 
35      for sel_obj, mask in Events :
 36          the callback = sel_obj.data # the callback accpet = 
37 [         callback(sel_obj.fileobj,mask) #accpet(server_fileobj,1)
38 
39 #客户端
40 from socket import *
41 c=socket(AF_INET,SOCK_STREAM)
42 c.connect(('127.0.0.1',8088))
43 
44 while True:
45     msg=input('>>: ')
46     if not msg:continue
47     c.send(msg.encode('utf-8'))
48     data=c.recv(1024)
49     print(data.decode('utf-8'))
selector

 

Guess you like

Origin www.cnblogs.com/a2534786642/p/11095427.html