Advanced Programming for UNIX environments: Select, poll, and Epoll

Source: Internet
Author: User
Tags epoll

One, select

The select is currently supported on almost all platforms, and its good cross-platform support is one of its advantages, and it is, in fact, one of the few advantages it has left.

One disadvantage of select is that the number of file descriptors that a single process can monitor has a maximum limit, typically 1024 on Linux, but can be elevated by modifying the macro definition or even recompiling the kernel.

In addition, the data structure maintained by select () that stores a large number of file descriptors increases linearly as the number of file descriptors grows. At the same time, because the latency of network response time makes a large number of TCP connections inactive, calling select () will perform a linear scan of all sockets, so this also wastes a certain amount of overhead.

Second, poll

Poll and select also have the disadvantage that arrays that contain a large number of file descriptors are copied between the user state and the kernel's address space as a whole, regardless of whether the file descriptor is ready, and its overhead linearly increases as the number of file descriptors increases.

In addition, the Select () and poll () tell the process that the file descriptor is ready, and if the process does not IO it, the next time you call Select () and poll (), the file descriptors are reported again, so they generally do not lose the ready message. This approach is called a horizontal trigger (level triggered).

Third, Epoll

It was not until Linux2.6 that the implementation, which was directly supported by the kernel, was epoll, which had almost all the advantages it had previously said, and was recognized as the best multi-channel I/O readiness notification method under Linux2.6 performance.

Epoll can support both horizontal and edge triggers (edge triggered, which only tells the process which file descriptor has just become ready, it only says once, if we don't act, then it will not be notified again, this way is called the Edge Trigger), In theory, EDGE triggers have a higher performance, but the code implementation is quite complex.

Epoll also tells only those file descriptors that are ready, and when we call Epoll_wait () to get the ready file descriptor, the return is not the actual descriptor, but a value representing the number of the ready descriptor. All you have to do is go to epoll the specified number of file descriptors in one array, and the memory Mapping (MMAP) technique is used here, thus eliminating the overhead of these file descriptors being replicated during system calls.

Another essential improvement is that Epoll is based on the event-ready notification method. In Select/poll, the kernel scans all monitored file descriptors only after a certain method has been invoked, and Epoll registers a file descriptor with Epoll_ctl (), once the file descriptor is ready, The kernel uses a similar callback callback mechanism to quickly activate the file descriptor, which is notified when the process calls Epoll_wait ().

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.