Select/poll/epoll Contrast

Source: Internet
Author: User
Tags epoll

The first is to look at common Select and poll. For network programming, it is generally considered that poll is more advanced than Select, which is mainly due to the following reasons:

    1. Poll () does not require the developer to calculate the maximum file description Fugai size.
    2. Poll () is faster when dealing with a large number of file descriptors because the kernel needs to check each bit in the fd_set of a large number of descriptors for select (), which is time consuming.
    3. Select can monitor the number of file descriptors is fixed, relatively less (1024 or 2048), if you need to monitor a large number of file descriptors, even if the monitoring descriptor is very small, if the distribution is very sparse and inefficient, for the poll () function, You can create an array of a specific size to hold the monitor descriptor, not the size of the file descriptor value, and the number of files that poll () can monitor is much larger than select.
    4. For select, the monitored fd_set changes after the select is returned, so the Fd_set,poll () function that needs to be monitored will need to be reinitialized before the next time it enters select () to separate the monitored input and output events. Allows the monitored array of files to be reused without reinitialization.
    5. The time-out parameter of the Select () function is undefined on return, and with portability in mind, the timeout parameter needs to be reset each time after the time-out before entering to select.

Of course, it is not that select has no merit:

    1. Select () is more portable and does not support poll () on some UNIX systems
    2. Select () provides better precision for timeout values: microseconds, while poll is milliseconds.

Advantages of Epoll:

1. Support a process to open a large number of socket descriptors (FD)

Select the most unbearable is a process opened by the FD is a certain limit, set by Fd_setsize, the default value is 1024/2048. It is obviously too small for the number of connected IM servers that need to be supported. At this time you can choose to modify the macro and then recompile the kernel. However, Epoll does not have this restriction, it supports the FD limit is the maximum number of open files, this number is generally far greater than 2048, for example, in 1GB memory of the machine about 100,000, the specific number can be cat/proc/sys/fs/file-max to see, In general, this number is very much related to system memory.

2.IO efficiency does not decrease linearly with increasing number of FD

Another Achilles heel of traditional select/poll is when you have a large socket set, but because of network latency, only some of the sockets are "active" at any one time, but select/poll each call will scan the entire collection linearly. resulting in a linear decrease in efficiency. However, Epoll does not have this problem, it only operates on "active" sockets---This is because Epoll is implemented in the kernel implementation based on the callback function above each FD. Then, only the "active" socket will be active to call the callback function, the other idle state socket will not, at this point, Epoll implemented a "pseudo" AIO, because this time the driving force in the Linux kernel.

3. Use Mmap to accelerate message delivery between the kernel and user space.

This actually involves the concrete implementation of the Epoll. Both Select,poll and epoll need the kernel to inform the user of the FD message, how to avoid unnecessary memory copy is very important, at this point, epoll through the kernel and user space mmap the same piece of memory implementation.

For poll, it is necessary to copy the user's incoming POLLFD array to the kernel space, because the copy operation is associated with the length of the group, which is an O (n) operation, when the event occurs, the poll returns the data that is obtained to the user space and performs the aftercare work, such as freeing the memory and stripping the waiting queue, The time complexity of copying data to the user space and stripping the wait queue is also O (n).

These two days to see a cloud wind their bug is because the author of the Open Source Library used the non-blocking connect using Select () to wait for the timeout, but did not check the fd_setsize, when the number of file descriptors is greater than this number, there will be a memory out of bounds error, Cause coredump.

Select/poll/epoll Contrast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.