And select the difference epoll.poll

The difference select.poll and epoll

select just tell you there are a certain number of flow events, as to which stream an event, you have to go one by one poll, and the event will take place epoll tell you, by the incident, which flow naturally navigate to the .
1, select realization

Select the calling procedure is as follows:
Here Insert Picture Description

(1) copy copy_from_user from user space to kernel space fd_set

(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 put current (the current process) hang in the queue waiting to 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) poll described method returns whether a read or write operation readiness return 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.

to sum up:

select several major disadvantages:

(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 achieve

poll and select implementation is very similar, but different ways described fd collection, use poll pollfd structure rather than select fd_set structure, the other are similar.

Achieved on select and poll analysis, refer to the following several blog posts:

http://blog.csdn.net/lizhiguo0532/article/details/6568964#comments

http://blog.csdn.net/lizhiguo0532/article/details/6568968

http://blog.csdn.net/lizhiguo0532/article/details/6568969

http://www.ibm.com/developerworks/cn/linux/l-cn-edntwk/index.html?ca=drs-

http://linux.chinaunix.net/techdoc/net/2009/05/03/1109887.shtml

3、epoll

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, select and poll on the call interface and select epoll and poll the 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) select, poll achieve continuously polls all need their own fd set 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. This is caused by a 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 hanging only once (at the beginning of epoll_wait, Note that the device is not waiting queue waiting queue, but inside a defined waiting queue epoll). This can save a lot of overhead.

Reference links:

http://www.cnblogs.com/Anker/p/3265058.html

Published 100 original articles · won praise 0 · Views 1190

Guess you like

Origin blog.csdn.net/Aplox/article/details/104238864