The grievances and disputes of those years-synchronous IO, asynchronous IO, blocking IO, non-blocking IO, vivid description + illustration

The grievances and disputes of those years-synchronous IO, asynchronous IO, blocking IO, non-blocking IO, vivid description + illustration

Prior Knowledge

Long-winded sentence:

In fact, I want to write this article for a long time, because recently I heard that my friends encountered similar questions in interviews:, 同步IO和阻塞IO的区别 什么是IO多路复用and such questions they told me that the total answer was not satisfactory, and finally entered the talent pool with tears . Actually, it is necessary to understand these few Concepts, some people have some misunderstandings: for example, either adopt self-healing memory method (without considering brain capacity, in an attempt to persist memory) or just come up and tell the interviewer (this is good, at least a few words can be said, but once the interviewer Go deeper, your big knife may be taken out)

So the author's suggestion is that we need to understand some prior knowledge about IO before we understand the concept: Although it is a bit boring, but if you really want to guide the country not to be asked, it is essential

Overview-Operating System Kernel

​ For the operating system kernel, I believe that non-professional children's shoes have at least heard of it in some aspects. The author strongly recommends to look at the theory in this area. Its concept is more complicated. The author introduces the kernel from the question raised at the beginning: It has three supporting functions:

  • Interrupt processing, such as your keyboard access commands
  • Process management, including process creation and cancellation state transition, etc.
  • Resource management : Among them is today's core topic-IO device management

Kernel space and user space

Generally for us in terms of processes and threads are often divided into user-level process (thread) kernel space and user space and system-level processes (threads), commonly known as correspondence must be clearly kernel space can not sense the presence of the user space and kernel space only associated with core , The user space has nothing to do with the kernel. So when it happens 系统调用, the user space must cut into the kernel space. At this time, the process of the user space will be blocked. Only after the kernel space completes the system call and returns to the user space, can the improved process be awakened to continue operation

System call: The user space initiates an operation that requires the kernel to complete: For example, you get the data of the IO device through keyboard input and display it on your interface.

OK, after we have the above foundation, let's talk about the initial problem, and we will solve it easily. First illustrate and explain


Synchronous IO and blocking IO

Let's understand a picture first, and then elaborate on the relationship between the two

Insert picture description here

The so-called 同步IOis that the user perceives the user operation through the keyboard (not necessarily the keyboard), and the user space perceives the user operation and then initiates an IO operation to the kernel space for the kernel to handle it for us. The kernel starts to synchronize the data to the user space through the buffer after getting the data of the IO device. This period is called the data preparation phase, and at this stage the kernel will block other subsequent IO requests, and only after all data is synchronized to the user space will the user space process be awakened. Continue to process other operations

After talking about blocking IO, let's take a look at the difference between the two

The so-called 阻塞IOinput acquisition user space initiates a system call and waits for the kernel to complete before the user wants to use the application to complete other operations, but at this time because the kernel is synchronizing data, the user is forced to wait. So this is called blocking IO

Difference and connection

From the above explanation, you should realize that synchronous IO and blocking IO are actually the same thing. Both are blocking but the objects we discuss are different:

That emphasis from synchronous IO 被调用对象is to explore from blocking and blocking IO 调用着to explore the nature of obstruction angle is a system

Think about what happened after you log in to QQ and enter your username and password. Your memory of sync IO will be more profound.

Insert picture description here

One point to note: the above-mentioned waiting for the QQ processing result does not refer to the waiting stage when the QQ server verifies the data, but the waiting stage for the user when QQ needs to pass the input QQ number and password information from the parameters to be verified.


Asynchronous IO and non-blocking IO

Still look at the picture first

Insert picture description here

Asynchronous IO is relative to synchronous IO, that is, when the user enters and the user space initiates an IO operation request, the operating system is no longer required to refresh the buffer to the user space synchronously, but only treats the kernel as an aisle, and the IO data is directly Through the kernel to the user space, so on the one hand, the response speed is faster, on the other hand, there is no need to block the user space

Non-blocking IO means that when the user initiates an IO call, the user does not need to wait and thinks that the IO operation is processed in real time, so other operations can be performed directly.

Therefore, it can also be found that asynchronous IO and non-blocking IO are actually the same thing, but the discussion objects are different

The difference between synchronous and asynchronous

After talking about the respective scenarios, let's take a look at the difference between synchronization and asynchrony. Before, we studied the scenes of different objects on the same problem. Now we are studying different implementations of the same problem and the same object of discussion. We need to use a story. Explanation: Children's shoes who have seen martial arts novels or TV dramas must know such an institution

Scenes

Now a bank owner A wants to deliver a batch of goods to his business partner B. At this time, A is worried about the safety of the goods, so he asks the escort C of the Longmen Escort (it seems to be the most famous) to escort it to ensure safety. It’s a master of martial arts, let’s analyze the results of using synchronous and asynchronous processing

If it is synchronized :

Then after A and B have negotiated a good business, A starts to ship, so B waits for the arrival of the goods, and the two parties agree to pay for the goods and deliver the goods, that is, as long as B does not receive the goods, they will not pay,
so the business is also Unable to complete, and for A, when he delivers the goods to the escort, he just needs to wait for the escort to deliver the goods, and then collect the money (toast to celebrate...)
So the key to complete the transaction at this time The reason
is that the escort
synchronizes the goods from A to B. If the goods are not delivered, then B is waiting unconditionally. C is actually waiting for B. The reason is obvious and C is because once the escort receives the order, it means that on the one hand, I now have to prepare for manual escort of the goods. On the other hand, you must list that you are already at work and cannot accept new orders .
This process is同步

One more point: The escort can't feel the existence of C, they only know that the goods are delivered to the specified location, as for who will pick up the goods, they don't really care (don't be horny)

If it is asynchronous

: That's simple, just don't consider the escort, but it does not mean that the escort does not exist, but that A is confident enough to safely deliver the goods to C (the robber hehe smiles),
so it is not necessary to communicate with the escort and wait for the escort to ship. Save the preparation time for that part of the escort and communicate directly with C unilaterally,
so the delivery speed must be much faster. At the same time, for B, because he knows that C will ship directly, he can be sure that this business will become a business. , He does not need to wait until the goods are delivered, he
can continue to talk about new business with DEF, etc., so as to expand his business.
This process is异步

Asynchronous decision In the case of a lot of business (the frequency of thieves will increase), it is impossible to determine whether the order can be made after A and B.

If you haven't read martial arts novels, then come to an actual business scene

Insert picture description here

For example, we submit a form on a page:

If it is synchronized : Then after we enter the form information, pass

<input type = "submit" value="提交"/>

Submit directly, then before requesting the service processing result, your page should be in a circle (or other things that display progress), that is, if you want to view other information or do other page operations, it is impossible. This is called同步

If it is asynchronous : then you should submit via Ajax

$.ajax(){
    
    }

Then the whole page will not turn in a circle during the submission process. You will fully use this time to view other information on the page, and you will not be affected. This is异步


Notice

After digesting this article, you can further understand the relevant knowledge about the IO model

Next article IO multiplexing and Reactor mode, stay tuned!

Use this time to view other information on the page without any impact. This is异步


Notice

After digesting this article, you can further understand the relevant knowledge about the IO model

Next article IO multiplexing and Reactor mode, stay tuned!

Guess you like

Origin blog.csdn.net/lkg5211314/article/details/109963465