当前位置:网站首页>100% understanding of 5 IO models
100% understanding of 5 IO models
2022-06-27 08:17:00 【Little moon 6】
from TCP The process of sending data
We should have an in-depth understanding of various IO Model , Then you must first understand the various IO What's the reason for that , To understand the essence of this problem, we must know how a message is sent from one person to another ;
Take the communication between two applications as an example , Let's take a look at when “A” towards "B" Send a message , In short, it will go through the following process :
First step : application A Send a message to TCP Send buffer .
The second step : TCP Send the buffer and send the message out , After passing through the network , The message will be sent to B Server's TCP Receive buffer .
The third step :B Again from TCP The receiving buffer reads its own data .

According to the above figure, we basically know that the message is sent through application A、 application A Corresponding server TCP Send buffer 、 After network transmission, the message is sent to the application B Corresponding server TCP Receive buffer 、 And then finally B The application reads the message .
give an example :
Sunday afternoon , Just put down the phone , I'm writing a comment for the interviewer just now . Just wrote 『 Yes Linux Basic IO The model is not well understood 』 In this sentence , A girlfriend suddenly appears .
Ha , Why didn't the interviewer know IO The model , I know it all .

How do you know , Tell me something about .

You told me last time .

stay Java in , There are three main types IO Model , They are blocking IO(BIO)、 Non blocking IO(NIO) and asynchronous IO(AIO).
forehead 、 What you are talking about is Java Provided in IO Relevant API ah . Not at the operating system level IO The model .

What's the difference ? Do they have any relationship ?

Java Provided in IO Relevant API, When processing files , In fact, it depends on the operating system level IO The operation realizes . For example Linux 2.6 in the future ,Java in NIO and AIO It's all through epoll To achieve , And in the Windows On ,AIO It's through IOCP To achieve .
You can put Java Medium BIO、NIO and AIO Understand as yes Java Language to operating system IO Encapsulation of models . Programmers are using these API When , Don't care about knowledge at the operating system level , There is no need to write different code according to different operating systems . Just use Java Of API That's all right. .
Oh . I don't understand this , Tell me about it .

ok , Let me give you a brief introduction .

Mm-hmm , well , Well said , I'll cook you braised fish in brown sauce in the evening .

Mm-hmm , well .

stay Linux(UNIX) Operating system , There are five kinds IO Model , Namely : Blocking IO Model 、 Non blocking IO Model 、IO Reuse model 、 Signal driven IO Model as well as asynchronous IO Model .
Since I mentioned eating fish at night , Then explain these five types of fishing through the example of fishing IO Model bar. .
What exactly is IO
What we often say IO, It refers to the input and output of files , But how is it defined at the operating system level IO What about ? What kind of process can be called a IO Well ?
Take a disk file read as an example , The file we want to read is stored on disk , Our goal is to read it into memory . You can simplify this step by removing data from the hardware ( Hard disk ) Read in user space .
In fact, the real file reading also involves cache and other details , Let's not talk about it here . About user space 、 If readers don't understand the relationship between kernel space and hardware , It can be understood from the example of fishing .
When fishing , At first, the fish was in the fish pond , The final sign of our fishing action is that the fish is caught by us from the pond , Put it in the fish basket .
The fish pond inside can be mapped into a disk , The intermediate transition hook can be mapped into kernel space , Finally, the fish basket can be mapped into user space . A complete fishing (IO) operation , It's fish ( file ) From the fish pond ( Hard disk ) Transfer in ( Copy ) To the fish basket ( User space ) The process of .
Blocking IO Model
When we were fishing , There's a way to be comfortable , More relaxed , That is, we sit in front of the fishing rod , We do nothing in the process , Keep holding the fishing rod with both hands , Just wait for the fish to bite . Once you feel the power of the fish in your hand , Just catch the fish and put it in the basket . Then catch the next fish .
Mapping to Linux Operating system , This is one of the simplest IO Model , Or block IO. Blocking I/O It's the simplest I/O Model , Generally, a process or thread waits for a condition , If the conditions are not met , Keep waiting . Conditions met , Then proceed to the next step .

image
Application process through system call recvfrom receive data , But because the kernel is not ready for datagrams , The application process will block , Until the kernel is ready for datagrams ,recvfrom Complete datagram replication , The application process can end the blocking state .
This fishing method is relatively simple , For fishermen , No special fishing rod is needed , Take a long enough stick and you can start fishing leisurely ( Implement a simple ). The disadvantage is that it takes more time , It is more suitable for the situation where the demand for fish is small ( Low concurrency , Low timeliness requirements ).
This fisherman is really stupid , You can do something else when the fish bite .

image
Um. , What you said is one of two IO Model .

image
Non blocking IO Model
When we were fishing , While waiting for the fish to bite , We can do something else , For example, playing a king's glory 、 Watch an episode 《 Delay jubilee strategy 》 wait . however , We have to look at the fishing rod from time to time , Once you find a fish on the hook , Just catch the fish .
Mapping to Linux Operating system , This is non blocking IO Model . The application process interacts with the kernel , Before the goal is achieved , No longer just waiting , I'm going straight back . And then by polling , Keep asking if the kernel data is ready . If a poll finds that the data is ready , Then copy the data into user space .

image
Application process through recvfrom Call constantly to interact with the kernel , Until the kernel is ready for data . If not ready , The kernel will return error, The application process is getting error after , Send it later recvfrom request . In the time period when two requests are sent , The process can do something else first .
Fishing this way , And blocking IO Than , The tools used have not changed , But there are other things you can do when fishing , Increase time utilization .
It's really better . I can go to Taobao to pick up two skirts before the fish take the bait .

image
forehead , But you still have to pay attention to the movement of the fishing rod from time to time .

image
This is not easy to solve , Just buy a fishing rod with a reminder function .

image
Um. , You are talking about another kind of IO Model .

image
Signal driven IO Model
When we were fishing , To avoid checking the fishing rod over and over again , We can install an alarm on the fishing rod . When a fish bites the hook, call the police immediately . Then we get the alarm , Go catch the fish .
Mapping to Linux Operating system , This is signal driven IO. The application process notifies the kernel when reading a file , If a socket When an event of , Please send me a signal . After receiving the signal , The processing function corresponding to the signal will carry out subsequent processing .

image
The application process registers a signal processing function with the kernel in advance , Then the user process returns , And it doesn't block , When the kernel data is ready, it sends a signal to the process , The user process starts copying the data into the user space in the signal processing function .
Fishing this way , Compared with the first few , There have been some changes in the tools used , Need some customization ( The implementation is complex ). But the fisherman can do something else completely before the fish bite . Just wait for the alarm to go off .
Um. , This is the easiest way .

image
Yes . I asked you , Do you have any good ways to catch more fish in the shortest time ?

image
Can this still embarrass me , Put several fishing rods at the same time and fish at the same time .

image
So smart , You are talking about another kind of IO Model .

image
IO Reuse model
When we were fishing , In order to catch the most fish in the shortest time , We place multiple fishing rods at the same time , Fishing at the same time . Then which fishing rod has a fish bite , We'll catch the fish on the fishing rod .
Mapping to Linux Operating system , This is it. IO Reuse model . Multiple processes IO Can be registered on the same pipe , This pipeline will interact with the kernel in a unified way . When the data required by a request in the pipeline is ready , The process copies the corresponding data to the user space .

image
IO Multiplexer is one more select function , Multiple processes IO You can register with the same select On , When the user process calls the select,select Will listen to all registered IO, If all the monitored IO When the required data is not ready ,select The calling process will block . When any one IO When the required data is ready ,select The call returns , Then the process passes recvfrom To copy data .
there IO Reuse model , No signal processing functions are registered with the kernel , therefore , He is not non blocking . The process is issuing select after , Wait until the select Listen to all IO At least one of the required data in the operation is ready , There will be a return , And you also need to send a request again to copy the file .
Fishing in this way , By adding a fishing rod , Can effectively improve efficiency .
Mr. , I'm so smart . I understand all the above .

image
Do you really understand , Then I will test you : Which of the above is asynchronous , Which is synchronized ?

image
This is not difficult for me 、 Signal driven is asynchronous , Everything else is synchronized .

image
Wrong wrong wrong , All of the above are synchronized .

image
Why are the above four types synchronized
We say blocking IO Model 、 Non blocking IO Model 、IO Multiplexing model and signal driven IO The models are all synchronized IO Model . The reason is because , Whatever the model above , The real data copy process , It's all synchronous .
Isn't signal driven asynchronous ? Signal driven , The kernel notifies the process when the data is ready , Then the process passes recvfrom Operation for data copy . We can think of the data preparation phase as asynchronous , however , Data copy operations are synchronized . therefore , Whole IO Processes cannot be considered asynchronous .
You confused me , Take fishing for example .

image
well .

image
We put the fishing process , Can be split into two steps :1、 Fish bite ( Data preparation ).2、 Put the fish in the basket ( Data copy ). No matter which fishing method mentioned above , In the second step , They all need people to take the initiative , The fishing rod didn't do it by itself . therefore , The fishing process is actually synchronized .
What's the difference between this and boiling water , Didn't you tell me that the kettle with the alarm is asynchronous ?

image
It is also an alarm , Boiling water and fishing are two different things .

image
When the alarm for boiling water goes off , The whole water boiling process is completed . The water is already boiling .
When the fishing alarm goes off , It only means that the fish have bitten the hook , But there is no real catch yet .
therefore , Use a kettle with an alarm to boil water , The boiling process is asynchronous .
And fishing with a fishing rod with an alarm , The fishing process is synchronous .
This time I understand , Is there really asynchronous IO Well ?

image
There is .

image
asynchronous IO Model
When we were fishing , Using a high-tech fishing rod , That is, fully automatic fishing rod . It can automatically sense the fish on the hook , Automatic rod retraction , Even more powerful, it can automatically put the fish into the fish basket . then , Inform us that the fish has been caught , He went on to catch the next fish .
Mapping to Linux Operating system , This is asynchronous IO Model . The application process puts IO After the request is passed to the kernel , The kernel operates the file copy completely . After the kernel completes the relevant operations , It will send a signal to tell the application process this time IO Already completed .

image
User process initiation aio_read After the operation , Pass descriptors to the kernel 、 Buffer pointer 、 Buffer size, etc , Tell the kernel when the entire operation is complete , How to notify the process , Then I went to do something else immediately . When the kernel receives aio_read after , Will return immediately , Then the kernel starts waiting for the data to be ready , When the data is ready , Copy the data directly to the user control , Then inform the process of this IO Already completed .
Fishing in this way , It's undoubtedly the easiest . Don't care about anything , Just give it to the fishing rod .
Um. , This time I understand , So this is called asynchronous IO.

image
Yes , That's all Linux Operating system 5 Kind of IO Models .

image
5 Kind of IO Model comparison
image
It seems that this problem is really difficult . It is understandable that the child didn't answer .

image
The problem seems complicated , But it really depends on whether a person really understands IO The best question .

image
Line line , You are all right .

image
forehead

If you feel interesting , Just go to the original book and praise it
author : all _ Little fire
link :https://www.jianshu.com/p/8316ff4d740d
边栏推荐
- SPARQL basic introductory exercise
- 【论文阅读】Intrinsically semi-supervised methods
- Lvgl usage demo and instructions 2
- How can I import data from Oracle into fastdfs?
- ACM course term summary
- Redis的持久化机制
- JS example print the number and sum of multiples of all 7 between 1-100
- What is futures reverse documentary?
- Code source AQS sous - jacent pour la programmation simultanée juc
- 【原创】TypeScript字符串utf-8编码解码
猜你喜欢

MySQL锁详解

Blind survey shows that female code farmers are better than male code farmers

野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資

JS find the number of all daffodils

【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办

win10-如何管理开机启动项?

Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率

Game asset reuse: a new way to find required game assets faster

Filter filter

Win10 how to manage startup items?
随机推荐
The 6th Blue Bridge Cup
JVM常见的垃圾收集器
LVGL GUI GUIDER移植代码到STM32
JS output shape
Futures reverse Documentary - training for traders
【云原生】2.3 Kubernetes 核心实战(上)
[notes on c++ primer] Chapter 3 string, vector and array
Coggle 30 days of ML July competition learning
ACM course term summary
Linux下Redis的安装
win命令行中导入、导出数据库相关表
C how to call line and rows when updating the database
Import and export database related tables from the win command line
SQL attendance query interval: one hour
Time function calculation efficiency of C
Set the address book function to database maintenance, and add user name and password
lvgl 说明3关于lvgl guider的使用
05 观察者(Observer)模式
JS example print the number and sum of multiples of all 7 between 1-100
[11. two dimensional difference]
