Linux Select, poll and Epoll IO models for detailed __linux

Source: Internet
Author: User
Tags epoll int size

Turn from: http://blog.csdn.net/tianmohust/article/details/6677985

i). Epoll Introduction

Epoll is currently in Linux under the development of large-scale concurrent network programs popular candidates, Epoll in the Linux2.6 kernel formally introduced, and select similar, in fact, I/O multiplexing technology just , and nothing mysterious. In fact, the design of concurrent network programs in Linux, has always been a lack of methods, such as the typical Apache model (Process per Connection, referred to PPC), TPC (Thread per Connection) model, as well as select model and poll model, then why do we have to introduce epoll this stuff again? There's still a few words to talk about ...

II). Disadvantages of common models

If you do not put out the shortcomings of other models, how can you compare the advantages of epoll?

①PPC/TPC model

The two model ideas are similar, that is, let each of the incoming connection side of the work, do not bother me again . Just PPC is for it to open a process, while TPC opens a thread. But do not annoy me to have the price, it wants the time and the space Ah, after the connection many, so many process/the thread switch, this expense comes up; therefore, this kind of model can accept the maximum number of connections is not high, generally around hundreds of.

②select model

1. Maximum concurrency limit, because the FD (file descriptor) opened by a process is limited, set by Fd_setsize, the default value is 1024/2048, so the maximum concurrent number of the Select model is limited accordingly. Change the fd_setsize yourself. Although the idea is good, but first look at the following bar ...

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

3. Kernel/user space memory copy problem, how to let the kernel of the FD message to the user space. On this issue, select takes a memory copy method.

Summarized as: 1.  Number of Connections Limited 2. Find pairing Speed is 3 slower. data copied from kernel to user state

③poll model

Basically, the efficiency and select are the same, and the 2 and 3 of the Select faults do not get rid of them.

III). The ascension of the Epoll

Critique the other models one by one, and look at the improvement of epoll, in fact, the disadvantage of the select is that it is the epoll advantage.

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

②. The greatest advantage of epoll is that it does not only have an "active" Connection , but it is not connected to the total number of connections, so the efficiency of epoll is much higher than that of select and poll in the actual network environment.

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

IV). Epoll Why Efficient

Epoll's efficiency is inextricably linked to the design of its data structure, which is mentioned below.

First, recall the Select model, when an I/O event arrives, the select notifies the application that there is an event to be processed quickly, and the application must poll all the FD collections, test each FD for events and handle the event, and code like this:

[CPP]   View plain  copy int res = select (Maxfd+1, &readfds,  null, null, 120);   if  (res > 0)    {        for  (int i = 0; i < max_connection; i++)         {           if  (Fd_isset ( Allconnection[i], &readfds))            {               handleevent (AllConnection[i]);            }       }  }   // if (res == 0)  handle timeout, res < 0 handle  error   Epoll not only tells the application that there are i/0 events coming, but also tells the application about the information that is populated by the application, so that the information application can navigate directly to the event without having to traverse the entire FD collection.

[CPP] view plain copy int res = epoll_wait (EPFD, events, 20, 120);   for (int i = 0; i < res;i++) {handleevent (events[n]); } v). Epoll Key Data Structures

As mentioned earlier, the Epoll speed is closely related to its data structure, and its key data structure is:

[CPP] view plain copy struct Epoll_event {__uint32_t events;      Epoll events epoll_data_t data;   User data variable};       typedef union Epoll_data {void *ptr;       int FD;       __uint32_t u32;   __uint64_t U64;   } epoll_data_t; Visible Epoll_data is a union structure with which applications can hold many types of information: FD, pointers, and so on. With it, the application can locate the target directly.

VI). Using Epoll

Since Epoll is so good compared to select, how to use it. Will it be very cumbersome ah ...  Let's take a look at the following three functions, you know epoll is easy to use. int epoll_create (int size);

Generate a Epoll dedicated file descriptor, in fact, to apply for a kernel space to store the socket FD you want to focus on whether and what happened. Size is the maximum number of socket FD You can focus on this epoll FD, as long as the memory is sufficient.

int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);

Controls the events on a epoll file descriptor: Registering, modifying, deleting. Where the parameter EPFD is Epoll_create () creates a Epoll-specific file descriptor. Relative to the Fd_set and FD_CLR macros in the Select model.

Int

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.