Compare the difference between the Epoll model and the Select model under Linux

Source: Internet
Author: User
Tags epoll

A. Select model (common for Apache)

1. maximum concurrency limit , because the FD (file descriptor) opened by a process is limited, set by Fd_setsize, and the default value is 1024/2048, so the maximum number of concurrent Select models is limited. Change this fd_setsize yourself? Although the idea is good, let's look at the following ...

2. Efficiency issues , select each call will be linear scan all of the FD set, so efficiency will appear linear decline, the result of fd_setsize change is, everybody slowly, what? have timed out.

3. kernel/User space memory copy problem , how to let the kernel to notify the FD message to user space? In this issue, select takes a memory copy method, which is very time-consuming when FD is very numerous.

Summarized as: 1.  limited number of connections 2. Find pairing Speed is 3 slower. data is copied from the kernel to user state consumption time

Two. Epoll model upgrade (Nginx use)

Take a look at the improvement of epoll, in fact, the shortcomings of the select in turn that is epoll advantage.

①. Epoll does not have the maximum concurrent connection limit, the maximum number of open files , this number is generally greater than 2048, in general, this number and system memory relationship is very large , the specific number can be cat/proc/sys/fs/ File-max.

②. efficiency improvement, Epoll The biggest advantage is that it is just your "active" connection , but not the total number of connections, so in the actual network environment, epoll efficiency is much higher than select and poll.

③. memory Sharing , Epoll uses " shared memory " at this point, and this memory copy is omitted.

Three. Why is Epoll efficient?

The efficiency of Epoll and the design of its data structure are inseparable, as this will be mentioned below.

Recall the Select model first, when an I/O event arrives, the select notifies the application that there is an event to go Fast , and the application must poll all the FD collections, test each FD for events, and handle the events; The code looks like this:

1 intres =Select(maxfd+1, &readfds, NULL, NULL, -);2 if(Res >0)3 {4      for(inti =0; i < max_connection; i++)5     {6         if(Fd_isset (allconnection[i), &Readfds))7         {8 handleevent (Allconnection[i]);9         }Ten     } One } A //if (res = = 0) handle Timeout, res < 0 handle error

Epoll not only tells the application that a i/0 event is coming, but also tells the application about the information that is populated by the application, so that the application can navigate directly to the event, without having to traverse the entire FD collection. A similar code might look like this:

1 int  -  - ); 2  for (int0; i < res;i++) 3 {4    handleevent (Events[n]); 5 }

Epoll Key Data Structures

The key data structure of the Epoll speed and its data structure is:

1 structepoll_event {2__uint32_t events;//Epoll Events3epoll_data_t data;//User Data Variable4 };5 typedef Union EPOLL_DATA {6     void*ptr;7     intFD;8 __uint32_t u32;9 __uint64_t U64;Ten} epoll_data_t;

Visible Epoll_data is a union structure that can hold many types of information with the help of its application: FD, pointers, and so on. With it, the application can locate the target directly.

The kernel of the Select model must traverse all the monitored descriptors, and the application must traverse all descriptors to check which descriptors are ready. When the descriptors are hundreds of thousands, they become very inefficient-this is

The root cause of the inefficiency of the Select (poll) model. Considering these conditions, the Epoll model was introduced into the kernel after 2.6.

Compare the difference between the Epoll model and the Select model under Linux

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.