In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the differences between select, poll and epoll". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the differences between select, poll and epoll".
(1), select== > time complexity O (n)
It only knows that there is an Iamp O event, but it doesn't know which streams (there may be one, more, or even all), and we can only poll all streams indiscriminately to find streams that can read data, or write data, and operate on them. Therefore, select has the indifference polling complexity of O (n). The more streams it processes at the same time, the longer the indifference polling time will be.
(2), poll== > time complexity O (n)
Poll is essentially no different from select in that it copies the array passed in by the user to the kernel space and then queries the device status corresponding to each fd, but it has no limit on the maximum number of connections because it is stored based on linked lists.
(3), epoll== > time complexity O (1)
Epoll can be understood as event poll, and unlike busy polling and indiscriminate polling, epoll notifies us of which stream and what Ibino events have occurred. So we say that epoll is actually event-driven (each event is associated with a fd), and our operations on these streams make sense. (complexity reduced to O (1))
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-ready or write-ready), it can inform the program to read and write accordingly. But select,poll,epoll is essentially synchronous IMab O, 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 is not responsible for reading and writing on its own, and the implementation of Asynchronous IWeiO is responsible for copying data from the kernel to user space.
Both epoll and select can provide the solution of multi-channel I-map O-multiplexing. It can be supported in the current Linux kernel, in which epoll is unique to Linux, while select should be specified by POSIX, which is implemented in general operating systems.
Select:
Select is essentially the next step of processing by setting or checking the data structure that holds the fd flag bits. The disadvantages of this are:
1. The number of fd that can be monitored by a single process is limited, that is, the size of the listening port is limited.
Generally speaking, this number has a lot to do with system memory, and the specific number can be seen by cat / proc/sys/fs/file-max. The default for 32-bit phones is 1024. The default for a 64-bit machine is 2048.
2. Socket is scanned linearly, that is, the polling method is used, and the efficiency is low:
When there are many sockets, select () completes the scheduling by traversing FD_SETSIZE Socket each time, no matter which Socket is active. This will waste a lot of CPU time. If you can register a callback function with sockets and automatically complete the relevant operation when they are active, you avoid polling, which is what epoll and kqueue do.
3. It is necessary to maintain a data structure for storing a large amount of fd, which makes it expensive for user space and kernel space to copy the structure.
Poll:
Poll is essentially no different from select. It copies the array passed in by the user to the kernel space, then queries the device status corresponding to each fd. If the device is ready, it adds an item in the device waiting queue and continues traversing. If no ready device is found after traversing all the fd, it suspends the current process until the device is ready or times out actively, and then it traverses the fd again after being woken up. This process has gone through many unnecessary traverses.
It has no limit on the maximum number of connections because it is stored based on linked lists, but it also has one drawback:
1. A large number of fd arrays are copied between the user mode and the kernel address space, regardless of whether such replication is meaningful or not.
2. Another feature of poll is "horizontal trigger". If the fd is not processed after it is reported, the fd will be reported again the next time poll is reported.
Epoll:
Epoll has two trigger modes: EPOLLLT and EPOLLET, LT is the default mode, and ET is the "high speed" mode. In LT mode, as long as the fd still has data to read, each epoll_wait will return its event to remind the user program to operate, while in ET (Edge trigger) mode, it will only prompt once and will not be prompted again until the next data inflow, regardless of whether there is still data to read in fd. So in ET mode, when you read a fd, you must read its buffer, that is, until the return value of read is less than the requested value, or you encounter an EAGAIN error. Another feature is that epoll uses the ready notification mode of "events" to register the 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 to receive notifications.
Why should epoll have EPOLLET trigger mode?
If you use EPOLLLT mode, once there are a large number of ready file descriptors in the system that you do not need to read or write, they will return every time they call epoll_wait, which will greatly reduce the efficiency of the processor to retrieve the ready file descriptors that you care about. With the edge-triggered mode of EPOLLET, when there are read-write events on the monitored file descriptor, epoll_wait () will notify the processor to read and write. If you do not read and write all the data this time (for example, the read-write buffer is too small), it will not notify you the next time it calls epoll_wait (), that is, it will notify you only once until the second read-write event appears on the file descriptor! This mode is more efficient than horizontal trigger, and the system is not filled with a large number of ready file descriptors that you don't care about.
Advantages of epoll:
1. There is no limit on the maximum number of concurrent connections, and the upper limit of FD that can be opened is much greater than 1024 (about 100000 ports can be monitored on 1G memory).
2. The improvement of efficiency is not a way of polling, and it will not decrease with the increase of the number of FD. Only FD that is active will call the callback function
That is, the biggest advantage of Epoll is that it only cares about your "active" connections, but has nothing to do with the total number of connections, so in the actual network environment, Epoll will be much more efficient than select and poll.
3. Memory copy, using mmap () file mapping memory to accelerate message delivery with kernel space; that is, epoll uses mmap to reduce replication overhead.
Summary of differences among select, poll and epoll:
1. Support the maximum number of connections a process can open
Select
The maximum number of connections a process can open is defined by the FD_SETSIZE macro, which is the size of 32 integers (3232 on 32-bit machines and 3264 on 64-bit machines). Of course, we can modify it and recompile the kernel, but performance may be affected, which 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 there is an upper limit on the number of connections, it is very large. About 100000 connections can be opened on machines with 1G memory, and about 200000 connections can be opened on machines with 2G memory.
2. The problem of IO efficiency caused by the sharp increase of FD.
Select
Because the connection is linearly traversed with each call, the increase of FD will cause a "linear degradation performance problem" with slow traversal speed.
Poll
Same as above
Epoll
Because the implementation in the epoll kernel is based on the callback function on each fd, only active socket will actively call callback, so when there is less active socket, using epoll does not have the linear degradation of the previous two, but when all socket is active, there may be performance problems.
3. Message delivery mode
Select
The kernel needs to pass messages to user space, and kernel copy actions are required.
Poll
Same as above
Epoll
Epoll is implemented by sharing a piece of memory between the kernel and user space.
Summary:
To sum up, the choice of select,poll,epoll should be based on the specific occasions of use and the characteristics of the three ways.
1. 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, the notification mechanism of epoll requires a lot of function callbacks.
Select is inefficient because it needs to be polled every time. But inefficiency is also relative, depending on the situation, and can also be improved through good design.
Today, we compare these three kinds of IO multiplexing, refer to the materials on the Internet and books, and sort them out as follows:
1. Select implementation
The procedure for calling select is as follows:
(1) use copy_from_user to copy fd_set from user space to kernel space
(2) register the callback function _ _ pollwait
(3) iterate through all fd and call their corresponding poll method (for socket, this poll method is sock_poll,sock_poll will call tcp_poll,udp_poll or datagram_poll according to the situation)
(4) take tcp_poll as an example, its core implementation is _ _ pollwait, which is the callback function registered above.
(5) the main job of _ _ pollwait is to hang the current (current process) in the waiting queue of the device. Different devices have different waiting queues. For tcp_poll, the waiting queue is sk- > sk_sleep (note that hanging the process to the waiting queue does not mean that the process is asleep). After the device receives a message (network device) or fills in the file data (disk device), the device wakes up the process waiting for sleep on the queue, and the current is awakened.
(6) when the poll method returns, it returns a mask mask that describes whether the read and write operation is ready, and the fd_set is assigned a value according to this mask mask.
(7) if you have finished traversing all the fd and have not returned a read-write mask mask, the process that calls schedule_timeout (that is, current) that calls select will go to sleep. When the device driver has its own resources readable and writable, it will wake up the process of sleeping in the waiting queue. If no one wakes up after a certain timeout (specified by schedule_timeout), the process calling select will be awakened to get the CPU, and then re-traverse the fd to determine if there is a ready fd.
(8) copy fd_set from kernel space to user space.
Summary:
Several disadvantages of select:
(1) every time you call select, you need to copy the fd collection from the user mode to the kernel state. This overhead will be very large in many cases of fd.
(2) at the same time, each call to select requires traversing all the fd passed in the kernel, which is also very expensive in many cases when fd is called.
(3) the number of file descriptors supported by select is too small. The default is 1024.
2 poll implementation
The implementation of poll is very similar to select, except that the fd collection is described in a different way. Poll uses the pollfd structure instead of select's fd_set structure, and the others are similar. Managing multiple descriptors is also polled and processed according to the status of the descriptors, but poll has no limit on the maximum number of file descriptors. Poll and select also have the disadvantage that an array containing a large number of file descriptors is copied as a whole between the user state and the kernel address space, and its overhead increases linearly with the number of file descriptors, regardless of whether these file descriptors are ready or not.
3 、 epoll
Since epoll is an improvement on select and poll, it should be able to avoid the above three shortcomings. So how does epoll solve it? Before that, let's take a look at the differences between epoll and the calling interfaces of select and poll. Both select and poll provide only one function-- the select or poll function. Epoll provides three functions, epoll_create,epoll_ctl and epoll_wait,epoll_create to create an epoll handle, epoll_ctl to register the type of event to listen for, and epoll_wait to wait for the event to be generated.
For the first disadvantage, epoll's solution is in the epoll_ctl function. Each time a new event is registered into the epoll handle (specify EPOLL_CTL_ADD in epoll_ctl), all fd is copied into the kernel instead of duplicated during epoll_wait. Epoll guarantees that each fd will be copied only once during the entire process.
For the second disadvantage, unlike select or poll, 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 the ready list (using schedule_timeout () to sleep for a while and judge the effect of a while, similar to step 7 in the select implementation).
For the third disadvantage, epoll does not have this limit. The upper limit of FD it supports is the maximum number of files that can be opened. This number is generally much greater than 2048. For example, on a machine with 1GB memory, it is about 100000. The specific number can be seen by cat / proc/sys/fs/file-max. Generally speaking, this number has a lot to do with system memory.
Summary:
(1) the select,poll implementation needs to poll all fd collections on its own until the device is ready, during which sleep and wake may be alternated several times. In fact, epoll also needs to call epoll_wait to continuously poll the ready list, and may alternate sleep and wake up many times during this period, but it calls the callback function when the device is ready, puts the ready fd into the ready list, and wakes up the process that goes to sleep in the epoll_wait. Although both sleep and alternate, select and poll traverse the entire fd collection when they are awake, while epoll only needs to determine whether the ready list is empty while awake, which saves a lot of CPU time. This is the performance improvement brought about by the callback mechanism.
(2) each time select,poll calls, it copies the fd collection from user mode to kernel mode, and hangs the current in the device waiting queue once, while epoll only copies once, and hangs current on the waiting queue only once (at the beginning of epoll_wait, note that the waiting queue here is not a device waiting queue, but a waiting queue defined internally by epoll). It can also save a lot of money.
Thank you for your reading. The above is the content of "what's the difference between select, poll and epoll". After the study of this article, I believe you have a deeper understanding of the difference between select, poll and epoll, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.