Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of the differences among select, poll and epoll

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Select,poll,epoll is the mechanism of IO multiplexing. Icano multiplexing uses a mechanism to monitor multiple descriptors, and once a descriptor is ready (usually read or write ready), it can tell the program to read and write accordingly.

But in essence, select,poll,epoll is synchronous IWeiO, because they all need to be responsible for reading and writing after the read-write event is ready, that is to say, the read-write process is blocked, while asynchronous IWeiO does not have to be responsible for reading and writing on its own. The implementation of Asynchronous IWeiO is responsible for copying data from the kernel to user space.

Comparison of three groups of IO Multiplexing functions

Select:

The purpose of the select system call is to listen for readable, writable and abnormal events on the file descriptor of interest to the user for a specified period of time

Disadvantages:

1. The number of file descriptors that can be monitored is limited. Sizeof (fd_set) = 128, indicating that the maximum number of file descriptors that can be monitored is 128-8-1024.

two。 At the same time, every time you call select, you need to traverse all the fd passed in the kernel, and the performance will degrade when there is a lot of fd.

3. Since three event sets are modified after select returns when an event occurs, the fd collection needs to be copied from the user area to the kernel area each time, and performance degrades as the number of fd to be monitored increases.

Applicable scenarios:

Suitable for scenarios with a small number of monitored file descriptors

Poll:

Poll system calls, like select, poll a certain number of file descriptors within a specified time to test whether there are ready file descriptors.

Advantages:

1. Compared with select, there is no limit on the number of fd. Theoretically, the number of fd opened is related to the system internal.

two。 Instead of copying the fd collection from the user area to the kernel every time, it uses a struct pollfd structure to maintain each fd

Disadvantages:

It is essentially the same as selece, except that the way it describes the fd collection is different. Poll uses the pollfd structure instead of select's fd_set structure, and everything else is similar.

Applicable scenarios:

It is also suitable for scenarios with few descriptors of monitored files.

Epoll:

Epoll is an IO reuse function peculiar to Linux and is considered to have the best performance.

It is very different from select and poll in implementation and use:

1. Use a set of functions to do this instead of a single

two。 Put the events on the file descriptor that the user is concerned about in an event table in the kernel. There is no need to repeat the file descriptor set or event set for each call like select and poll, but epoll needs an additional file descriptor to represent the event table in the kernel.

The epoll function is very simple. There are epoll_create,epoll_ctl,epoll_wait3 functions. First, epoll_create creates a handle to epoll, then registers the event through epoll_ctl, and then epoll_wait detects the occurrence of the event.

Advantages:

Have all the advantages that select does not have.

1. There is no limit on the number of fd. The upper limit of fd it supports is the maximum number of files that can be opened. The specific number can be viewed by cat/proc/sys/fs/file-max. Generally speaking, this number has a lot to do with the memory of the system.

Every time 2.epoll_ctl registers a new event to the epoll handle (specify EPOLL_CTL_ADD in epoll_ctl), it copies all fd into the kernel instead of repeating it during epoll_wait. Epoll ensures that each fd is copied only once in the whole process.

Unlike select or poll, 3.epoll 's solution takes turns adding current to the device waiting queue corresponding to fd, but only hangs the current once during epoll_ctl (this time is essential) and specifies a callback function for each fd, which is called when the device is ready to wake up the waiters on the waiting queue, and this callback function adds the ready fd to a ready list. Epoll_wait 's job is actually to see if there is a ready fd in this ready list.

Applicable scenarios:

When there are more active connections, epoll_wait may not be more efficient than select and poll, because callback functions are triggered too frequently, so epoll_wait is suitable for situations with a large number of connections but few active connections.

Epoll operates on file descriptors in two modes: LT and ET

LT (horizontal trigger): this mode is the default working mode, in which case epoll is equivalent to a more efficient poll

When an EPOLLET event on a file descriptor is registered with the epoll kernel event table, epoll will manipulate the file descriptor in ET mode

ET (Edge trigger): ET mode is an efficient working mode of epoll, which greatly reduces the number of times that the same epoll event is triggered repeatedly.

Summary:

All three sets of IO multiplex system calls can listen for multiple file descriptors at the same time, and they will wait for the timeout specified by the timeout parameter until they return when there is time on one or more file descriptors. The return value is the number of file descriptors ready, and a return of 0 indicates that no event has occurred.

All three sets of functions use some structure variable to tell the kernel to listen for those events on which file descriptors and use parameters of that structure type to get the results of the kernel processing.

The parameter type fd_set of select does not bind the file descriptor collection event, it is only a file descriptor collection, so select needs to provide three parameters of this type to input and output readable and writeable exception events respectively, which on the one hand makes select unable to handle more types of events, on the other hand, due to the kernel's online modification of the fd_set collection. The application has to reset the three fd_set collections before calling select next time.

Poll's parameter type pollfd, which defines both file descriptors and events, and any event is handled uniformly, making the programming interface much simpler. And the kernel modifies the revents member of the pollfd structure each time, while the events member remains the same, so the application does not have to reset the event set of type pollfd the next time poll is called.

Epoll uses a separate system call epoll_ctl to control adding, modifying, and deleting events to it. In this way, each epoll_wait call takes the time of user registration directly from the kernel event table, without repeatedly reading these events from user space, and the events parameter of the epoll_wait system call is only used to return ready events, which makes the time complexity of the application index ready file descriptor up to O (1).

Poll and epoll_wait use nfds and maxevents parameters to specify the maximum number of file descriptors and events to listen for. Both of these values can reach the maximum number of file descriptors allowed to be opened by the system. In my system, it is 48428.

On the other hand, the number of file descriptors that select allows to listen is usually limited, and the test result is 12881024.

Both select and poll can only work in relatively inefficient LT mode, while epoll can work in ET efficient mode, and epoll also supports EPOLLONESHOT events, which further reduce the number of times readable, writable, and abnormal events are triggered.

Select uses polling, that is, the entire set of registration file descriptors is scanned for each call, and epoll_wait uses a callback method. When the kernel detects a ready file descriptor, the callback function triggers the callback function, and the callback function inserts the corresponding events on the file descriptor into the kernel ready event queue. The kernel finally copies the contents of the ready event queue to user space at the appropriate time, so epoll does not have to poll the entire set of file descriptors to detect which events are ready. When there are many active connections, epoll_wait may not be more efficient than select and poll, because callback functions are triggered too frequently, so epoll_wait is suitable for situations with a large number of connections but few active connections.

"finish"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report