Overview:
Select, poll, and epoll are three sets of I/O multiplexing system calls. These three sets of system calls can listen to multiple file descriptors at the same time. They will wait for the timeout time specified by the timeout parameter until an event occurs on one or more file descriptors. The returned value is the number of ready file descriptors. If the return value is 0, no event has occurred and the operation times out.
We further compare the similarities and differences between event sets, the maximum number of Supported file descriptors, the working mode, and specific implementations.
Event Set:
All three functions use a struct variable to tell the kernel which file descriptors are monitored and which events are processed by the kernel.
Select model: The parameter type is fd_set. Three such structures are provided to indicate listening to three different types of event readable, writable, and abnormal. This prevents select from listening to more types of events. On the other hand, because the kernel modifies the output result of fd_set, the SELECT statement must be reset when the SELECT statement is called again.
Poll Model: Define file descriptors and events in a struct pollfd at the same time. All events are processed in a unified manner, so that the programming interface is more standardized. Each time the kernel returns a modified revents member of pollfd, the events member remains unchanged, so the next call to poll does not need to be reset.
HoweverCommon disadvantages of select and poll: Each call must return the entire user-registered event set, including both ready and not ready. This makes it necessary to traverse and judge the entire event set one by one. The time complexity is O (n ).
Epoll ModelIt uses a completely different method from select and poll to manage user registration events. it maintains an event table in the kernel and provides an independent system to call epoll_ctl to add, delete, and modify events. At the same time, the events parameter of the epoll_wait function is only used to return ready events, which makes the event complexity of the directly indexed ready file descriptor O (1 ).
Number of file descriptors:
The maximum number of file descriptors that the select can listen to at the same time is 1024, which makes the server often lacks large-scale user login. Poll and epoll are relatively unlimited, and can usually reach 65535.
Working Mode:
The Select and poll models only work in the relatively inefficient <mode, while epoll can work in the efficient mode of ET.
Implementation principle:
Both select and poll useRound RobinThat is, each call scans the entire registered file descriptor set and returns the ready file descriptor set. Epoll usesCallbackWhen the kernel detects a ready file descriptor, the callback function is triggered, and the callback function inserts the corresponding event on the file descriptor into the kernel ready event queue.
Comparison of Three I/O multiplexing Models