The relationship between cpu and io

Before talking about the five models of io, let’s talk about where Io moved the files from to where

My own understanding:
According to the work or the business encountered. It is impossible for the file to exist in the cache or in the memory, because the cache and memory cannot store things permanently.

Files need to be stored permanently. Therefore, files are stored in the hard disk of the computer, or in their hard disk of the cloud server.

we io file, 

The first case: modify the content of the file

It is to go to the hard disk to read the file first, then read the file to the memory, then modify the content of the file, and then save the file to the hard disk

The second case: do not modify the contents of the file

It is to go to the hard disk of the cloud server to read the file first, then read the file to the memory of the cloud server, then upload the file in the memory of the cloud server to the web server, and then we download the file from the web server to the hard disk of our own computer inside.

The third case: do not modify the contents of the file

It is to go to the hard disk of your computer to read the file first, then read the file to the memory of your computer, then upload the file in the memory of your computer to the web server, and then we download the file from the web server to the hard disk of our own computer inside.

five models of io

When it comes to the I/O model, the words synchronous, asynchronous, blocking, and non-blocking will be involved. The concepts of these words are explained below.

blocking and non-blocking

Blocking and non-blocking refer to whether to return the result after the operation is completed when performing an operation, or to return the result immediately.

Blocking: refers to the IO operation needs to be completely completed before returning to the user space. Before the call result is returned, the caller is suspended (the current thread enters a non-executable state. In this state, the CPU will not allocate time slices and the thread will be suspended. run) only enters the active state when the result is reached;

Blocking example: Haidilao's server orders for you. When you finish ordering, the waiter sends the message to the back kitchen. At this time, you wait at the table until the chef prepares the soup pot and side dishes and delivers them to your table. On, you can start eating. You can't leave during the serving process, because after you left, the waiter served the food but couldn't find you, so you can wait. At this time, you are in a blocked waiting state, which is what I said before, you are the caller , you are suspended into a non-executable state.

Non-blocking (nonblocking): means that a status value is returned to the user immediately after the I/O operation is called, without waiting for the I/O operation to be completely completed, and the caller will not be suspended until the final call result is returned;

Non-blocking example: Haidilao’s server orders for you. When you finish ordering, the waiter sends the message to the back kitchen. After three minutes, you go to the back kitchen and ask if my pot bottom or beef roll is ready. ? The back kitchen said it was not good, and then you went to deal with other things, and then after another five minutes, you ran to the back kitchen and asked if one of my dishes was ready, if not, you continued to do other things, and then waited Ask again, this time is at the same time as the I/O operation, you are not suspended, you can operate other things, but if the I/O operation is completed, you need to accept it immediately.

Let's learn it tomorrow!!!!!!, this is a bit too crying

This is a good question about concurrent/parallel systems. The short answer is: IO requires very little CPU resources. Most of the work is delegated to the DMA.

For disk IO, the real scenario may be as follows:

The CPU said: Brother Hard Disk, help me copy a copy of the short movie I want to watch to the main memory, thank you, dear.
The hard disk said: OK! I'll call you after the exam.
The CPU said: What! Then I'm going to play games!
...
The CPU is slapping and slapping (100 nanoseconds have passed)
...
The hard disk said: I have finished the small C test.
The CPU said: Teacher Cang, I am coming!

Of course, we can also download Teacher Cang's works from the Internet, which is Internet IO. But the situation is basically the same. While waiting for a small movie, CPU children's shoes played a round of masturbation.

Therefore, it is precisely because of this process of dispatching tasks, communicating, and waiting that the concurrent system shows its significance. Of course, the actual process may be ten thousand times more complicated than this. For example, the CPU will not directly talk to the hard disk, there is a middleman between them, called DMA (Direct Memory Access) chip .

CPU calculates the file address ⇒ delegates DMA to read the file ⇒ DMA takes over the bus ⇒ CPU A process blocks and hangs ⇒ CPU switches to B process ⇒ DMA notifies CPU after reading the file (an interrupt exception) ⇒ CPU switches back to A process to operate the file

This process corresponds to the following picture (source: "UNIX Network Programming" ), have you seen the timeline of application? After the aio_read operation, it is all blank, and the CPU doesn't care, and you can do other things.

Assume that the original CPU needs to wait 50 nanoseconds to read the file. Now though, two context switches take 5 nanoseconds each. The CPU still earns 40 nanosecond time slices.

The above is the general process of "asynchronous IO" in the traditional five major IO models .

---------------------------------------------------------------------------------------------------------------------

This article is from the link below.

Will I/O take up the CPU all the time? - Fat Jun's answer - Zhihu https://www.zhihu.com/question/27734728/answer/127095338

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/132240962