当前位置:网站首页>The difference between select/poll/epoll
The difference between select/poll/epoll
2022-07-27 16:25:00 【Cute rain】
1. How user mode transfers file descriptors into the kernel
select: establish 3 Create a set of file descriptors and copy them to the kernel , Listen and read separately 、 Write 、 Abnormal movements . This is affected by the fact that a single process can open fd Quantitative restriction , The default is 1024 individual .
poll: Will the incoming struct pollfd Structure array is copied to the kernel for listening .
epoll: perform epoll_create At the high speed of the kernel cache The District establishes a red black tree and a ready list ( The linked list only stores ready file descriptors ). Then the user executes epoll_ctl Adding a file descriptor to the function will add corresponding nodes to the red black tree .
2. Kernel state detects the read and write state of file descriptors
select: By polling , Traverse all of fd, Finally, a descriptor ready for reading and writing mask Mask , According to this mask, give fd_set assignment .
poll: Polling is also used , Query each fd The state of , If ready, add an item to the waiting queue and continue traversing .
epoll: Using a callback mechanism , The kernel is detecting that a file descriptor is readable / Callbacks are called when writable , The callback function places the file descriptor in the ready list .
3. How to find the ready file descriptor and pass it to the user state
select: Put the previously passed in fd_set The total number of file descriptors that copy out to user mode and return ready . The user state does not know which file descriptors are in the ready state , You need to traverse to determine .
poll: Put the previously passed in fd Array copies the outgoing user state and returns the total number of ready file descriptors . The user state does not know which file descriptors are in the ready state , You need to traverse to determine .
epoll:epoll_wait Just observe whether there is data in the ready list , Finally, the linked list data is returned to the array and the ready number is returned . The kernel puts the ready file descriptor in the passed in array , So you just need to traverse the ready , There is no need to traverse all . The file descriptor returned here is adopt mmap() Let the kernel and user space share a piece of memory to realize transmission Of , Reduced unnecessary copies .
4. Repeated monitoring mode
select: Copy the new set of listening file descriptors into the kernel , And continue with the above steps .
poll: New struct pollfd Structure array is passed into the kernel , Continue with the above steps .
epoll: No need to rebuild the red black tree , Just follow the existing ones .
5.epll The reason for being more efficient :
select and poll The movements are basically the same , It's just poll Use linked list to store file descriptors , and select use fd Label bit to store , therefore select Will be limited by the maximum number of connections , and poll Can't .
select、poll、epoll Although the number of ready file descriptors will be returned , however select and poll It is not clear which file descriptors are ready , and epoll Meeting . The difference is , After the system call returns , call select and poll The program needs to traverse the entire listening file descriptor to find out who is in the ready , and epoll It can be handled directly .
select、poll You need to copy the data structure of the relevant file descriptor into the kernel , Finally, copy it out . and epoll The data structure of the created file descriptor itself is stored in the kernel state , When the system call returns, use mmap() File mapped memory acceleration and kernel space messaging : namely epoll Use mmap Reduce replication overhead .
select、poll Polling is used to check whether the file descriptor is in ready state , and epoll Using a callback mechanism . The result is , With fd An increase in ,select、poll The efficiency of the system will decrease linearly , and epoll It won't be affected too much , Unless it's active socket quite a lot .
epoll The edge trigger mode is efficient , The system will not be filled with a large number of ready file descriptors that do not care , although epoll The best performance , But when the number of connections is small and the connections are very active ,select and poll May be better than epoll good , After all epoll The general mechanism of requires many function callbacks .
边栏推荐
猜你喜欢

Time series - use tsfresh for classification tasks

Leetcode234 question - simple method to judge palindrome linked list

Time series ARIMA model

Mapreduce实例(二):求平均值

Characters generated by JMeter function assistant in jmeter5.3 and later versions cannot be copied when they are grayed out

DeFi安全之DEX与AMMs

Solve the problem that Flink cannot be closed normally after startup

For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!

Leetcode25 question: turn the linked list in a group of K -- detailed explanation of the difficult questions of the linked list

JMeter5.3 及以后的版本jmeter函数助手生成的字符在置灰无法复制
随机推荐
Use of arrow function
爬取常见英文名
Mysql5.7 master-slave hot standby settings on CentOS
Servlet基础知识点
Determine the exact type of data
Leetcode 226 flip binary tree (recursive)
centos上mysql5.7主从热备设置
ARIMA model selection and residuals
Taking advantage of 5g Dongfeng, does MediaTek want to fight the high-end market again?
Install MySQL using CentOS yum
DRF learning notes (V): viewset
Example of the task submitted by the Flink packer
Content ambiguity occurs when using transform:translate()
TP5 rewrite paging
Baidu picture copy picture address
时间序列-ARIMA模型
MapReduce instance (I): wordcount
Coding skills - Global exception capture & unified return body & Business exception
Addition of large numbers
centos yum方式安装mysql