The difference between select poll and epoll Linux.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In the Linux Socket server programming, in order to deal with large client connection request, need to use non blocking I/O and reuse, select, poll and epoll I/O multiplexing Linux API, since Linux 2.6 with epoll, and has been widely used in the field of high performance servers, nginx is now more famous the use of epoll to achieve I/O reuse support high concurrency, present in high and primary scenarios, nginx is becoming more and more popular. Here is an article reference. Nginx to become the world's most popular Top1000 website Web server.

According to   w3techs statistics in July 3rd showed that, in the global     Top 1000 sites, 34.9% sites; in the use of Nginx, which makes Nginx beyond the Apache, a high traffic web site the most trusted Web server. The statistical data.

select:

The following is a function of the interface of select:

int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);


The select function to monitor the file descriptor can be divided into 3 categories, respectively is writefds, readfds, and exceptfds. Call select function will be blocked until the description, deputy ready (data readable, writable, or except), or (timeout specifies the timeout waiting time, if return immediately set to null), the function returns. When the select function returns, can traverse the fdset, to find the ready descriptors.

Select currently supports in almost all of the platform, the good cross platform support is also an advantage. One disadvantage of select is that the quantity of each process to monitor the file descriptor has the maximum limit, usually 1024 in the Linux, you can modify the macro definition even recompile the kernel to improve the way this limitation, but it also will reduce the efficiency of the.

poll:

int poll (struct pollfd *fds, unsigned int nfds, int timeout);


Unlike the select using three bitmaps to represent the way the three fdset, the pointer poll using a pollfd implementation.

struct pollfd {
int fd; /* file descriptor */
short events; /* requested events to watch */
short revents; /* returned events witnessed */
};


The pollfd structure contains to monitor event and occurrence of event, no longer use select“ parameter value &rdquo transfer mode. At the same time, pollfd does not restrict the maximum number (but the number is too large performance will also decline). Like the select function, poll returns, need to get ready polling pollfd descriptors.

Seen from above, the select and poll are required in return, by traversing the file descriptor to get ready for socket. In fact, at the same time a large number of client connections may only rarely in the ready state at the moment, so as the number of monitoring descriptor growth, its efficiency will be decreased linearly.

epoll:

The epoll interface is as follows:

int epoll_create(int size), 
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event), 
            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 */
            };

int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);


Mainly epoll_create, epoll_ctl and epoll_wait three functions. The epoll_create function to create the epoll file descriptor, the parameter size is not the limiting epoll can monitor the descriptors of maximum number, just a suggestion to the kernel of initial allocation of internal data structures. Returns the epoll descriptor. -1 creation failed. epoll_ctl control to the specified descriptor FD OP operations, event is associated with the FD monitor events. The op operation has three kinds: add EPOLL_CTL_ADD, remove the EPOLL_CTL_DEL, modify the EPOLL_CTL_MOD. Respectively, add, delete and modify the FD listener event. epoll_wait  for EPFD IO events, up to a maximum of maxevents events.

In the select/poll process, only in the method call to the kernel, it scans all monitoring file descriptor, and epoll in advance through the epoll_ctl () to register a file descriptor, once on a file descriptor is ready, the kernel uses a callback mechanism similar to the callback, the rapid activation of the file descriptor, when the process call epoll_wait () when they notice.

The advantages of epoll are mainly the following aspects:

The number of descriptors 1 surveillance is not restricted, its support for FD limit is the maximum number of open files, this number is far greater than the general 2048, for example, in the 1GB memory on the machine is about 100000 the left and right, the number is cat /proc/sys/fs/file-max look, in general the number and system memory great relationship. The biggest drawback of select is the process of open FD there is a limit to the number of. This is for connecting a number of relatively large server cannot meet. Solution although also can choose more process (Apache is the realization of this), but although Linux above to create the process cost is relatively small, but still can not be ignored, and inter process synchronization is far less than that of inter thread synchronization and efficient, so it is not a perfect solution.

Efficiency is not 2 IO decreases with the number of monitoring FD growth. Epoll is different from select and poll polling, but through each FD defined callback function to achieve. Only the ready FD will execute the callback function.

The 3 support level trigger and edge triggered (just tell what file process descriptor just into the ready state, it will only say, if we do not take action, it will never again to inform, this way is called edge triggered) in two ways, performance theoretically edge trigger is much higher, but the code is complex.

4.mmap accelerated the kernel and user space information transmission. Epoll is the kernel to user space MMAP with a block of memory, avoiding the fearless memory copy.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ricky at December 11, 2013 - 8:37 PM