当前位置:网站首页>About i/o -- the relationship between memory and CPU and disk
About i/o -- the relationship between memory and CPU and disk
2022-06-25 08:32:00 【Jason? thirteen】
About I/O
One sentence sums up what is I/O: Simple data Copy. Between memory and external devices copy Namely I/O(input/output)
I/O And CPU
Usually CPU Speed : Nanosecond level ( Command execution speed )
Usually I/O Speed : Millisecond level ( Once on a disk seek)
Thousands of times the speed difference , How to match... Needs to be solved I/O and CPU Speed 、 Dispatch CPU etc. .
Suppose you're an acute (CPU), Need to wait for an important file , Unfortunately, this document can only be sent by express (I/O), Then you choose not to do anything , Staring at the door affectionately, just like Hani who is looking forward to you, he is concentrating on waiting for the express ? Let's leave the express service alone for the time being , Play a game, watch a movie, brush a short video and wait for the express to come ? Obviously , A better way is to do something else first , When the express comes .
Therefore, the key point here is that the matter at hand can be suspended first , Switch to other tasks , Switch back when the express arrives .
perform I/O What's going on at the bottom
1. About Block:

In the above figure, there are two processes in memory , process A And processes B, The current process A Running , process A There is a code to read the file in , No matter what language it is in, it is common to customize a for loading data buffer, And then call read Functions like that , like this :
read(buff);
This is a typical I/O operation , When CPU When this code is executed, a read request is sent to the disk , Pay attention to and CPU It's faster to execute instructions than ,I/O The operation is very slow , Therefore, it is impossible for the operating system to waste valuable computing data on unnecessary waiting , At this time, the external equipment performs I/O The operation is quite slow , So in I/O The process cannot move forward until the operation is completed , That's what's called Blocking , That is what is usually said block. The operating system has detected that the process is running towards I/O After the device initiates the request, it pauses the process , You need to record the running status of the current process and put CPU Of PC Registers point to instructions from other processes .
2. About I/O Blocking queue and ready queue :
When a process is paused, it will have Carry on , Therefore, the operating system must save the suspended process for subsequent execution , Obviously we can use queues to hold processes that are suspended , As shown in the figure , process A Is suspended and put in the blocking queue ( Be careful , Different operating systems have different implementations , Maybe each I/O Every device has a corresponding Blocking queues , But this difference in implementation details does not affect our discussion ).
The disk is equivalent to reading asynchronously , And the number of times will put the read end callback into I/O queue ,CPU I went to do other things .
In fact, in addition to blocking queues, there are also Ready queue , The so-called ready queue means that the processes in the queue are ready to be CPU Yes , You may ask why you don't have to have a ready queue to execute directly ? The answer is simple , That's it cannot meet the needs of the people , The only 2 Thousands of processes can be created on a core machine ,CPU It's impossible to execute so many processes at the same time , So there must be such a process , Even if it's all ready, it can't be allocated to computing resources , Such a process is put on the ready queue . Process now B Right in the ready queue , Everything is ready CPU, As shown in the figure :
Pay attention to the picture above , here process B In being CPU perform , The disk is running to the process A In memory space of copy data , data copy And instruction execution at the same time , Under operating system scheduling CPU、 The disk is fully utilized , That's the wisdom of programmers .
3. interrupt
After that, the disk finally got all the data copy In the process A The memory of the , At this time, the disk informs the operating system that the task is completed , This is it. interrupt .
The operating system finds the data after receiving the disk interrupt copy complete , process A Re qualify for continued operation , At this time, the operating system carefully put the process A From the blocking queue to the ready queue , As shown in the figure :

Be careful , The operating system does not run processes directly A Of , process A Must be put in the ready queue to wait , It's fair to everyone . After that, the process B Carry on , process A Continue to wait for , process B After a while, the operating system thinks that the process B It took long enough to execute , So put the process B Put it in the ready queue , And put the process A Remove to continue . Note that the operating system puts the process B Put it in the ready queue , So the process B It's suspended just because the time slot is up, not because it's started I/O Request blocked , As shown in the figure :
process A Carry on , here buff It's full of the data you want , process A It's going on happily , It's like it's never been suspended , The process knows nothing about being suspended , That's the magic of the operating system .
Zero copy :
Bypass the operating system , Direct data copying is called zero copy .
In fact, in general I/O Data is the first thing copy Inside the operating system , And then the operating system copy Into process space
Secrets from the bottom of the computer —— breeze , official account : Manon's island for survival
This book is easy to read , A style similar to the big talk design pattern , Sense of humor , illustrated , simple , It is much better than the crumpled text description and dry code in the textbook .
边栏推荐
- 开户券商怎么选择?在线开户是安全么?
- leetcode.13 --- 罗马数字转整数
- 钱堂教育商学院给的证券账户安全吗?能开户吗?
- How to calculate the distance between texts: WMD
- C language "Recursion Series": recursively realizing the n-th power of X
- Getting to know the generation confrontation network (11) -- using pytoch to build wgan to generate handwritten digits
- Exchange:管理日历权限
- Stimulsoft Ultimate呈现报告和仪表板
- Rank sum ratio (RSR) index calculation
- What do various optimizers SGD, adagrad, Adam and lbfgs do?
猜你喜欢

Index analysis of DEMATEL model

How to calculate the independence weight index?

Find out the possible memory leaks caused by the handler and the solutions

How to calculate the fuzzy comprehensive evaluation index? How to calculate the four fuzzy operators?

Use pytorch to build mobilenetv2 and learn and train based on migration

Use Adobe Acrobat pro to resize PDF pages

Meaning of Jieba participle part of speech tagging

初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字

GPU calculation

Unity addressable batch management
随机推荐
Overview of image super score: the past and present life of image super score in a single screen (with core code)
4 reasons for adopting "safe left shift"
关于I/O——内存与CPU与磁盘之间的关系
Almost taken away by this wave of handler interview cannons~
Want to open an account, is it safe to open an online stock account?
How to analyze the grey prediction model?
4個不可不知的采用“安全左移”的理由
What is SKU and SPU? What is the difference between SKU and SPU
Wechat applet opening customer service message function development
Jdbc-dao layer implementation
How to calculate critical weight indicators?
GPU calculation
Scanpy(七)基于scanorama整合scRNA-seq实现空间数据分析
Find out the possible memory leaks caused by the handler and the solutions
Index analysis of DEMATEL model
NIPS 2014 | Two-Stream Convolutional Networks for Action Recognition in Videos 阅读笔记
Daily question brushing record (III)
Similarity calculation method
What do various optimizers SGD, adagrad, Adam and lbfgs do?
Common SRV types