当前位置:网站首页>IO model review
IO model review
2022-07-07 10:26:00 【Zong_ 0915】
IO Model review
Preface
This piece of ice river teacher's article , Do a review .
One . IO The basic concept of
First of all, what is IO: It involves the process of data migration between computer core and other devices , Namely IO. For example, disk IO:
- Input : Read data from disk to memory .
- Output : Write data in memory to disk .
And the operating system initiates once IO The operation generally includes two stages :
- IO call : The application process goes to Operating system kernel A call .
- IO perform : Operating system kernel complete IO operation .
among ,IO The implementation stage is divided into two stages :
- Data preparation stage : Kernel wait I/O The device is ready for data .
- Copy data phase : Copy data from kernel buffer to user process buffer .
Pictured :
Two . IO Model
IO There are five types of models :
2.1 Blocking type IO(BIO)
Application process initiation IO call , however Kernel data is not ready . therefore The application process has been blocking and waiting , Until the kernel data is ready .
shortcoming : If the kernel data is never ready , Then the user process will always be blocked , Waste performance .
2.2 Non-blocking type IO(NIO)
In view of the blocking IO The shortcomings of , On its basis , If the kernel data is not ready , Non-blocking type IO Meeting First, return the error message to the user process , Make it unnecessary to wait , Then request by polling . advantage : Compared with blocking IO, Users don't have to wait , It will not enter the blocking state because the kernel data is not ready .
shortcoming : Frequent polling , Lead to frequent system calls , Consume a lot of CPU resources .
2.3 IO Multiplexing (BIO)
Since frequent polling leads to CPU It costs a lot . Let the kernel data be ready , Actively notify the application to make system calls . That is to say IO Multiplexing .
Concept : File descriptor (File Descriptor
)
The file descriptor is a non negative integer . When opening an existing file or creating a new file , The kernel returns a file descriptor . Reading and writing files also need to use file descriptors to specify the files to be read and written .
IO The core idea of reuse model : The system provides us with a class of functions , They can monitor multiple at the same time fd
The operation of , ren Which one returns kernel data ready , The application process then initiates a system call . Be careful , Here, the application process is still required to initiate system calls .
IO There are three ways of multiplexing :select、poll、epoll
.
2.3.1 select
The application process can pass select
function , Come on Monitor multiple at the same time fd
, stay select
Function monitoring fd
In the process of , As long as any data state is ready .select
It will return to the readable state , At this time, the application process will initiate a request to read kernel data . Pictured :
Drawbacks as follows :
- Monitoring IO The maximum number of connections has an upper limit .
select
When the function returns , By traversingfd
aggregate , Find ready descriptorfd
.( namely Traverse all streams )
2.3.2 poll
Whereas select
Disadvantages of the way , It came up. poll
. Compared with the former ,poll
It solves the problem of limiting the number of connections . however poll
You still need to traverse the file descriptor to get the ready socket
.
2.3.3 epoll
So in order to solve the problem select
and poll
The problem is , And then there is IO Multiplexing (epoll
) Model , Use event driven to realize :
Looks like select
There is no difference in the flow chart , Add some words here :
epoll
First, throughepoll_ctl()
Function to register a file descriptor .- Once based on a
fd
When it's ready , The kernel uses a callback mechanism , Quickly activate thisfd
. - When the process calls
epoll_wait()
I'll be informed when . By adopting Listen for event callbacks Mechanism to Avoid traversing all text descriptors .
although IO Multiplexing this way for non blocking IO, There is no need to make frequent calls , But through callback . But when the process calls epoll_wait()
when , It can still be blocked .
Important things are to be repeated for 3 times , Multiplexing IO It still is : Synchronous blocking ! Synchronous blocking ! Synchronous blocking !
Therefore, we hope to have such a function in design ( I think it would be better to understand this way ):
- Multiplexing IO, Although you can specify the corresponding IO flow . Avoid traversing all IO.
- Although the results are obtained through callback , But the process of waiting for the result , It needs blocking to wait .
- Therefore, the design hopes that the user process can not wait , Do something else first . Wait for the callback result , I can feel it again .
Then comes the signal drive IO Model .
2.4 Signal driven IO(NIO)
On the basis of multiplexing . Send a signal to the kernel . At this time, the application process does not need to be blocked , You can do other things . When the kernel data is ready , Re pass SIGIO
Signal the application process . Once the process gets the signal , Immediately call to get kernel data . Pictured :
Of course , The data status inquiry process here is asynchronous, right , But the data replication part , It is still synchronously blocked , Therefore, the whole signal drives IO The process of is not asynchronous .
2.5 asynchronous IO(AIO)
You only need to send a request to the kernel once , All operations of data status inquiry and data copy can be completed , And don't block waiting for results .
Here's the explanation BIO、NIO、AIO
:
- Synchronous blocking (
blocking-IO
) abbreviationBIO
. - Synchronous nonblocking (
non-blocking-IO
) abbreviationNIO
. - Asynchronous non-blocking (
asynchronous-non-blocking-IO
) abbreviationAIO
.
3、 ... and . summary
3.1 select、poll、epoll The three difference *
Comparative study | select | poll | epoll |
---|---|---|---|
Underlying data structure | Array | Linked list | Red and black trees + Double linked list |
Get ready fd The way | Traverse all of | Traverse all of | Event callback |
The complexity of the event | O(n) | O(n) | O(1) |
maximum connection | 1024(Linux ) | unlimited | unlimited |
fd Data copy mode | Every time you call select , We need to fd Copy from user space to kernel space | Every time you call poll , We need to fd Copy from user space to kernel space | Through memory mapping (mmap ), There is no need to make frequent copies fd, At a time can be . |
3.2 Five kinds IO Model
IO Model | Blocked state | sync |
---|---|---|
Blocking type IO | Blocking | Sync |
Non-blocking type IO | Non blocking | Sync |
IO Multiplexing | Blocking | Sync |
Signal driven IO | Non blocking | Sync |
asynchronous IO | Non blocking | asynchronous |
Blocking , The concept of non blocking distinguishes : It can be simply understood as whether you need to do something to get a reply immediately , If you cannot get a return immediately , Need to wait , That's blocking . Prefer whether to respond immediately .
Sync , The concept of asynchrony : You always do one thing before you do another , Whether it takes time to wait , This is synchronization . Otherwise, it is asynchronous . I prefer whether I can do two things in parallel .
Then look back at the table above :
Non-blocking type IO The explanation in this respect :
- Non blocking : Because user processes can get results immediately ( It may be the data that the end user wants , It may also be an error message ).
- Sync : Because the data replication phase is always executed after the data query phase .
IO Explanation of multiplexing :
- Blocking : The user process needs to wait for the callback result to return . The process is blocked .
- Sync : Because the data replication phase is always executed after the data query phase .
Signal driven IO The explanation in this respect :
- Non blocking : The user process can get the return immediately in the data query stage .
- Sync : You need to wait for the kernel to send a signal , Express
fd
eureka . Let the user process getfd
, Then the user process initiates a request for data copy .
asynchronous IO The explanation in this respect :
- Non blocking : Users can also get results immediately .
- asynchronous : The whole data waiting and copying operation are handed over to the operating system , Not users , Users do not need to block waiting .
In the end, it can be found that , For these five IO Model , The difference between asynchronous and synchronous is nothing more than :
- Sync : The data waiting and copying process is divided into two stages , Initiated by the application process . It needs to be initiated twice .
- asynchronous : The data waiting and copying operations are handed over to the operating system . The application process can initiate a request .
边栏推荐
- Slurm资源管理与作业调度系统安装配置
- mysql插入数据创建触发器填充uuid字段值
- [email protected]能帮助我们快速拿到日志对象
- Appx code signing Guide
- 反射效率为什么低?
- Can I open a stock trading account online? Is it safe
- The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
- Use the fetch statement to obtain the repetition of the last row of cursor data
- Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
- Appx代码签名指南
猜你喜欢
IIC基本知识
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
mysql插入数据创建触发器填充uuid字段值
String formatting
SQLyog数据库怎么取消自动保存更改
The method of word automatically generating directory
浅谈日志中的返回格式封装格式处理,异常处理
Prototype object in ES6
Remote meter reading, switching on and off operation command
Serial communication relay Modbus communication host computer debugging software tool project development case
随机推荐
IIC基本知识
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
对存储过程进行加密和解密(SQL 2008/SQL 2012)
555电路详解
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Inno Setup 打包及签名指南
Review of the losers in the postgraduate entrance examination
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
STM32产品介绍
【acwing】786. 第k个数
Fiddler simulates the interface test
Guid主键
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
How to cancel automatic saving of changes in sqlyog database
Hdu-2196 tree DP learning notes
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Multisim--软件相关使用技巧
JMeter loop controller and CSV data file settings are used together
PDF文档签名指南