当前位置:网站首页>I / O multiplexing and its related details
I / O multiplexing and its related details
2022-04-23 17:56:00 【Augustu_】
Fd Detailed explanation
Fd: Is a positive integer , It's actually a file descriptor table ( Array ) The index of , The file descriptor table holds the pointer of the opened file .
Every process in PCB(Process Control Block) That is, a file descriptor table is stored in the process control block , The file descriptor is the index of this table , Each entry in the file descriptor table has a pointer to an open file . Now let's make it clear : Open files are used in the kernel file The structure represents , The pointer in the file descriptor table points to file Structure .
file Structure is the structure used to describe file attributes in the kernel .
System call details
The basic concept of system call : Usually , In the operating system A set of subroutines used to realize various system functions , And provide them to the application call .
The program interface is OS Specially set for user programs , It is also obtained by the user program OS The only way to serve . Program interfaces are usually composed of various types of system calls , thus , It can also be said that , System call provides the interface between user program and operating system , The application program realizes its connection with... Through system call OS Communication for , And get its services .
System calls are provided in the operating system , Enables applications to Method through system call , Indirectly call the relevant procedures of the operating system , Obtain corresponding services .
Software interrupt realizes system call :
- Software interrupt : It is an interrupt triggered by a software instruction .Linux System kernel responds to software interrupt , Switch from user state to kernel state , Execute the corresponding system call .
1.Select
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
select Allows applications to monitor a set of file descriptors , Wait for one or more file descriptors to become ready , To complete the I/O operation .
-
fd_set Use arrays to implement , The array size uses FD_SETSIZE Definition , So you can only listen to less than FD_SETSIZE Number of descriptors . There are three types of descriptors :readset、writeset、exceptset, Read separately 、 Write 、 Set of descriptors for exception conditions .
-
timeout For timeout parameters , call select It will block until the event with descriptor arrives or the waiting time exceeds timeout.
-
Successful call, return result greater than 0, Error. The returned result is -1, The timeout result is 0.
2.Poll
int poll(struct pollfd *fds, unsigned int nfds, int timeout);
// When timeout by -1 when ,poll The call will always block , Until something happens ;timeout by 0 when , Return immediately
poll With the function of select similar , It is also waiting for one of a set of descriptors to become ready .
Select And Poll Compare :
- select The maximum file descriptor monitored by default is 1024, But it can be modified ,poll There is no restriction on file descriptors .
- poll Provides more event types , And the reuse of descriptors is better than select high .
3.Epoll
/* 1.epoll Use a set of functions to complete the task , Not a function 2.epoll Put the events on the file descriptor concerned by the user into the event table in the kernel , So there is no need to be like select and poll The file descriptor set or event set is repeatedly passed in each call */
// Create a directive epoll File descriptor of kernel event table , This descriptor will be used for other purposes epoll The first parameter of the system call ,size It doesn't work .
int epoll_create(int size);
// This function is used to operate the events on the file descriptor monitored by the kernel event table : register 、 modify 、 Delete
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
// Wait for events on a set of file descriptors for a timeout period , If successful, the number of ready file descriptions will be returned
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
-
epoll_ctl() Used to register a new descriptor with the kernel or change the state of a file descriptor . Registered file descriptors are maintained in a red black tree in the kernel .
-
epoll Than select and poll More flexible , There is no limit on the number of file descriptors .
-
Working mode :
epoll There are two trigger modes for descriptor events :LT(level trigger) and ET(edge trigger).
-
LT Pattern
When epoll_wait() testing fd After an event occurs on and notifies the application of this event , The process will be informed of this , The process may not handle the event immediately , When the application next calls epoll_wait() when ,epoll_wait This event will also be notified to the application again , Until this event is handled . Support at the same time Blocking and Non-Blocking.
-
ET Pattern
When epoll_wait() testing fd After an event occurs on and notifies the application of this event , The application must process the event immediately , Because later epoll_wait() The event will no longer be notified to the application .
Only support No-Blocking, To avoid blocking read due to a file handle / Blocking writes starve the task of processing multiple file descriptors .
ET The model greatly reduces epoll The number of times the event was triggered repeatedly , therefore ET Mode efficiency ratio LT Mode high .
-
EPOLLONESHOT
In order to avoid multiple threads operating one at the same time Socket, You can register EPOLLONESHOT event .
What we expect is a socket The connection is handled by only one thread at any one time , adopt epoll_ctl Register the file descriptor epolloneshot event , A thread handles socket when , Other threads will not be able to handle , When the thread is finished , Need to pass through epoll_ctl Reset epolloneshot event
-
Same as different
- select and poll The file descriptor is in User mode Add to the file descriptor set , Each call needs to copy the entire collection to the kernel state ; and epoll File descriptors are maintained in Kernel mode , Every time you add a file descriptor, you need to perform a system call .
- select Use a linear table to describe the set of file descriptors , The file descriptor has an upper limit ;poll Use a linked list to describe , There is no upper limit ;epoll The set of file descriptors is described with a red black tree , And there is no upper limit .
- select and poll You need to traverse the entire set of file descriptors , Determine which file descriptor has an event happening ; and epoll Will maintain a ready list, The ready event is added to the list, Every time you call epol_wait When , Observe only list Whether there is data .
Application scenarios
-
select Application scenarios
elect Of timeout The parameter accuracy is microseconds , and poll and epoll For milliseconds , therefore select It is more suitable for scenes with high real-time requirements , For example, the control of nuclear reactors .
select Better portability , Supported by almost all mainstream platforms .
-
poll Application scenarios
poll There is no limit to the maximum number of descriptors , If the platform supports and does not require high real-time performance , You should use poll instead of select.
-
epoll Application scenarios
Just run on Linux On the platform , There are a large number of descriptors that need to be polled at the same time , And these connections are preferably long connections , because Epoll The descriptors of all files exist in the kernel , It needs to be called through the system epoll_ctr To change the file descriptor state , Frequent operation will reduce efficiency .
Need to monitor less than 1000 A descriptor , There is no need to use epoll, Because it can't be reflected in this application scenario epoll The advantages of .
版权声明
本文为[Augustu_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231754306194.html
边栏推荐
- Tensorflow tensor introduction
- MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
- Leak detection and vacancy filling (VII)
- 122. 买卖股票的最佳时机 II-一次遍历
- ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
- [binary number] maximum depth of binary tree + maximum depth of n-ary tree
- 92. Reverse linked list II byte skipping high frequency question
- Notes on common basic usage of eigen Library
- Listen for click events other than an element
- JS implementation private attribute
猜你喜欢

【Appium】通过设计关键字驱动文件来编写脚本
![SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]](/img/60/e4d47d458dd98a0c6ba51874e07c30.png)
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]

Kubernetes service discovery monitoring endpoints

cv_ Solution of mismatch between bridge and opencv

48. Rotate image

Tell the truth of TS

2021长城杯WP

2022年上海市安全员C证操作证考试题库及模拟考试

土地覆盖/利用数据产品下载

Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
随机推荐
Go file operation
_ FindText error
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
Sword finger offer 22 The penultimate node in the linked list - speed pointer
This point in JS
Type judgment in [untitled] JS
圆环回原点问题-字节跳动高频题
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
2022 tea artist (primary) examination simulated 100 questions and simulated examination
92. 反转链表 II-字节跳动高频题
Leak detection and vacancy filling (6)
Fashion classification case based on keras
470. 用 Rand7() 实现 Rand10()
394. String decoding - auxiliary stack
48. 旋转图像
油猴网站地址
Go's gin framework learning
31. 下一个排列
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
2022年流动式起重机司机国家题库模拟考试平台操作