The difference between Nginx server select and epoll

Why is epoll so fast

epoll is a way of multiplexing IO (I/O Multiplexing), but it is only used for kernels above linux 2.6. Before discussing this issue, let's explain why multiplexing IO is needed.

Take a Let ’s explain it with an example in life.

Suppose you are studying in a university and you are waiting for a friend to visit, and this friend only knows that you are in Building A, but doesn’t know where you live, so you make an appointment at the door of Building A Meet.

If you use the blocking IO model to deal with this problem, then you can only wait at the door of building A waiting for your friend to come, during this time you can't do anything else, it is not difficult to know, this way The efficiency is low.

Now the times have changed, and the multiplexing IO model has been used to deal with this problem. You tell your friend to come to Building A to find the building manager, and let her tell you how to go. The building here Guan Aunt plays the role of multiplexing IO.

Further explain the difference between the select and epoll models. The

select version of Aunt does the following things: For example, when a friend of classmate A comes, the select version of Aunt is stupid, she brings her friends Inquire from room to room who is classmate A. The friend you are waiting for is here, so in the actual code, the select version of the aunt does the following things:

int  n = select(&readset,NULL,NULL,100);

for  ( int  i = 0; n > 0; ++i)
{
    if  (FD_ISSET(fdarray[i], &readset))
   {
      do_something(fdarray[i]);
      --n;
   }
}

The epoll version of Auntie is more advanced. She wrote down the information of classmate A, such as his room number, so when classmate A's friend arrives, she only needs to tell the friend which room classmate A is in, and she doesn't need to bring it by herself. The building is full of people looking for someone. So what the epoll version of aunt does can be represented by the following code:
n=epoll_wait(epfd,events,20,500);
    
for (i=0;i<n;++i)
{
    do_something(events[n]);
}

In epoll, the key data structure epoll_event is defined as follows:
typedef union epoll_data {
                 void *ptr;
                 int fd;
                __uint32_t u32;
                __uint64_t u64;
        } epoll_data_t;

         struct epoll_event {
                __uint32_t events;       /*  Epoll events  */
                epoll_data_t data;       /*  User data variable  */
        }; 
It can be seen that epoll_data is a union structure, which is the structure used by the epoll version to save classmate information. It can save many types of information: fd, pointer, etc. With this structure, epoll aunt can not use it You can locate classmate A with a

little effort The building management aunt has to query the students in the whole building, so the processing efficiency will inevitably be low, and there will

be a lot of people at the bottom of the building soon. After multiplexing IO, the program can freely perform its own work except for IO operations. Only when the IO status changes, the multiplexed IO will notify you, and then take the corresponding action, instead of blocking and waiting for the IO status. It has changed.

It can also be seen from the above analysis that the improvement of epoll over select is actually a specific application of the idea of ​​​​using space for time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650895&siteId=291194637