当前位置:网站首页>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 .
边栏推荐
- Excel extract duplicates
- 嵌入式面试
- Some queries of TP5
- DRF learning notes (preparation)
- DRF learning notes (V): viewset
- Script file ‘D:\programs\anaconda3\Scripts\pip-script.py‘ is not present.
- The new JMeter function assistant is not under the options menu - in the toolbar
- ARIMA model selection and residuals
- 4位数的随机数据
- centos yum方式安装mysql
猜你喜欢
随机推荐
DEX and AMMS of DFI security
const小结
ADAMS中转动整个模型
Log management
gpt-2 文本生成
Some queries of TP5
Baidu picture copy picture address
firefox旧版本
ARIMA模型选择与残差
Mapreduce实例(一):WordCount
Sudden! 28 Chinese entities including Hikvision / Dahua / Shangtang / Kuangshi / ITU / iFLYTEK have been blacklisted by the United States
The solution to the memory exhaustion problem when PHP circulates a large amount of data
CCF-201312-1
时间序列-ARIMA模型
Pointer summary
Samsung closes its last mobile phone factory in China
ARIMA model selection and residuals
The image displayed online by TP5 is garbled
企业运维安全就用行云管家堡垒机!
MySQL index









