Select, epoll, and poll comparison (network resource summary)

Source: Internet
Author: User
Tags epoll

From: http://www.cnblogs.com/aga-j/archive/2011/08/26/2153943.html

Introduction to select, poll, and epoll

Select

In essence, select performs the next step by setting or checking the data structure that stores the FD flag. The disadvantage is:

1. The number of FD monitored by a single process is limited.

2. Maintain a Data Structure to store a large amount of fd data, which will cause high replication overhead when the user space and kernel space transmit the structure.

3. linear scanning is used for socket scanning.

Poll

Poll is essentially no different from select. It copies the input array to the kernel space and queries the device status of each fd, if the device is ready, add an item to the device waiting queue and continue the traversal. If no ready device is found after traversing all fd, the current process will be suspended until the device is ready or timed out, after being awakened, it will traverse fd again. This process has undergone many unnecessary traversal.

It has no limit on the maximum number of connections because it is stored based on a linked list, but it also has a disadvantage:

A large number of fd arrays are fully replicated between the user State and the kernel address space, regardless of whether such replication is meaningful.

Another feature of poll is "horizontal triggering". If fd is not processed after reporting, the next poll will report the fd again.

Epoll

Epoll supports horizontal triggering and edge triggering. The biggest feature of epoll is edge triggering. It only tells the process which fd has just changed to the desired state and will only notify once.

In terms of replication, epoll uses mmap to reduce replication overhead.

Another feature is that epoll uses the event-ready notification method to register fd through epoll_ctl. Once the fd is ready, the kernel will use a callback mechanism similar to callback to activate the fd, epoll_wait will receive a notification

1. Maximum number of connections that a process can open

Select

The maximum number of connections that a single process can open is defined by the FD_SETSIZE macro, which is the size of 32 integers (on 32-bit machines, the size is 32*32, similarly, the FD_SETSIZE on 64-bit machines is 32*64). Of course, we can modify and re-compile the kernel, but the performance may be affected. This requires further testing.

Poll

Poll is essentially no different from select, but it has no limit on the maximum number of connections because it is stored Based on linked lists.

Epoll

Although the maximum number of connections is exceeded, connections of about 0.1 million can be opened on machines with 1 GB of memory, and connections of about 0.2 million can be opened on machines with 2 GB of memory.

2. Io efficiency problems arising from the dramatic increase in FD

Select

Because the connection is linearly traversed during each call, increasing FD will lead to a "linear performance degradation problem" with slow traversal speed ".

Poll

Same as above

Epoll

The epoll kernel is implemented based on the callback function on each fd. Only the active socket can actively call callback. Therefore, when the number of active sockets is small, epoll is used without the linear decline of the previous two. However, when all sockets are very active, performance problems may occur.

3. message transmission mode

Select

The kernel needs to copy messages to the user space.

Poll

Same as above

Epoll

Epoll is implemented by sharing a piece of memory with the user space through the kernel.

To sum up, select, poll, and epoll should be selected based on the specific use cases and the characteristics of these three methods. On the surface, epoll has the best performance, but when the number of connections is small and the connections are very active, the performance of select and poll may be better than epoll. After all, epoll's notification mechanism requires a lot of function callbacks. (Not complete. To be continued)

By aga. J.
Source: http://www.cnblogs.com/aga-j
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.
Personal study notes are only used by me to record knowledge and are not published articles

Introduction to select, poll, and epoll

Select

In essence, select performs the next step by setting or checking the data structure that stores the fd flag. The disadvantage is:

1. The number of fd monitored by a single process is limited.

2. Maintain a Data Structure to store a large amount of fd data, which will cause high replication overhead when the user space and kernel space transmit the structure.

3. linear scanning is used for socket scanning.

Poll

Poll is essentially no different from select. It copies the input array to the kernel space and queries the device status of each fd, if the device is ready, add an item to the device waiting queue and continue the traversal. If no ready device is found after traversing all fd, the current process will be suspended until the device is ready or timed out, after being awakened, it will traverse fd again. This process has undergone many unnecessary traversal.

It has no limit on the maximum number of connections because it is stored based on a linked list, but it also has a disadvantage:

A large number of FD arrays are fully replicated between the user State and the kernel address space, regardless of whether such replication is meaningful.

Another feature of poll is "horizontal triggering". If FD is not processed after reporting, the next poll will report the FD again.

Epoll

Epoll supports horizontal triggering and edge triggering. The biggest feature of epoll is edge triggering. It only tells the process which Fd has just changed to the desired state and will only notify once.

In terms of replication, epoll uses MMAP to reduce replication overhead.

Another feature is that epoll uses the event-ready notification method to register FD through epoll_ctl. Once the FD is ready, the kernel will use a callback mechanism similar to callback to activate the FD, epoll_wait will receive a notification

1. Maximum number of connections that a process can open

Select

The maximum number of connections that a single process can open is defined by the fd_setsize macro, which is the size of 32 integers (on 32-bit machines, the size is 32*32, similarly, the fd_setsize on 64-bit machines is 32*64). Of course, we can modify and re-compile the kernel, but the performance may be affected. This requires further testing.

Poll

Poll is essentially no different from select, but it has no limit on the maximum number of connections because it is stored Based on linked lists.

Epoll

Although the maximum number of connections is exceeded, connections of about 0.1 million can be opened on machines with 1 GB of memory, and connections of about 0.2 million can be opened on machines with 2 GB of memory.

2. Io efficiency problems arising from the dramatic increase in FD

Select

Because the connection is linearly traversed during each call, increasing FD will lead to a "linear performance degradation problem" with slow traversal speed ".

Poll

Same as above

Epoll

The epoll kernel is implemented based on the callback function on each fd. Only the active socket can actively call callback. Therefore, when the number of active sockets is small, epoll is used without the linear decline of the previous two. However, when all sockets are very active, performance problems may occur.

3. message transmission mode

Select

The kernel needs to copy messages to the user space.

Poll

Same as above

Epoll

Epoll is implemented by sharing a piece of memory with the user space through the kernel.

To sum up, select, poll, and epoll should be selected based on the specific use cases and the characteristics of these three methods. On the surface, epoll has the best performance, but when the number of connections is small and the connections are very active, the performance of select and poll may be better than epoll. After all, epoll's notification mechanism requires a lot of function callbacks. (Not complete. To be continued)

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.