Non-blocking of synchronous asynchronous blocking; analysis of asynchronous Blocking

Source: Internet
Author: User

Non-blocking of synchronous asynchronous blocking; analysis of asynchronous Blocking

Let's talk about the meaning of these words.
Synchronization:Synchronization is a task that depends on another task. Only after the dependent task is completed can the task be completed.
Asynchronous:Asynchronous mode does not need to wait for the dependent task to complete, but only notifies the dependent task to complete what work, as long as the entire task is completed by yourself.
Blocking:Blocking call means that the current thread will be suspended before the call result is returned, and it will remain waiting for Message notification and cannot execute other services.
Non-blocking:Non-blocking call means that the current thread will not be suspended before the call result is returned and other services can be executed.

Key points:
Synchronization and Asynchronization are related to processes. Synchronization and Asynchronization are mainly from the perspective of the Message notification mechanism. Blocking and non-blocking are thread-related, and blocking and non-blocking are the status angles when waiting for message notifications.
Synchronous Response depends on the Synchronous Response of messages. asynchronous response is implemented through status, callback, and notification.
The thread hangs before the message is returned, and the rest of the services cannot be executed (synchronization is only because the logic function does not return the result, and some other operations can be executed)
Before a non-blocking dependent message is returned, the thread will not be suspended and is activated.

Scenario example:

Synchronous and asynchronous:
When I went to the bank to handle the business, the previous situation was that everyone had to queue up in a window (whether it was a storm or not). When it was not there, you had to queue up and couldn't do anything else;
Now you can get a number from the number generator. Before you get it, you can even buy something outside the bank.
Here, each person who handles the business is equivalent to a process, which is synchronous and asynchronous.

Blocking and non-blocking:
If we do not do anything after we queue up and get the number, but wait for the notification to arrive at us to handle the business, it will be blocked.
If we eat a wave of chicken at this time, it is not blocked.

Five Linux I/O models:

 

Basically, I/O operations are divided into two parts: User-layer API calling (kernel); kernel-layer system calling (initiating I/O requests)
Therefore, "Asynchronous/Synchronous" refers to API calls; "blocking/non-blocking" refers to the mode in which the kernel completes I/O calls.

Synchronization blocking:The read or write requests at the user layer are converted to kernel I/O requests until the I/O requests are blocked (blocking, reading data and returning, failing to read and waiting, end user-layer requests.

Synchronous non-blocking (NIO ):The read or write requests at the user layer are converted to kernel I/O requests. The user layer is synchronized and the kernel I/O is not blocked (read the returned byte and the read fails to return-1 ).

Asynchronous blocking:I/O reads are refined into subscribe to I/O events, and the actual I/O reads and writes, in the "subscribe to I/O events" event part will take the initiative to let out the CPU until the event occurs, some kernel I/O requests are blocked.

Signal-driven IO:This is an asynchronous blocking method that converts the above subscribed I/O events into a signal-driven approach.

Asynchronous non-blocking (AIO): User-layer read or write requests are converted to kernel I/O requests. The user-layer is asynchronous and kernel I/O is not blocked. However, this is just a pretty good look. The Implementation Method in Java is still a set of NIO.

Summary:Synchronization and Asynchronization only focus on how messages are notified, while blocking and non-blocking focus on the status when waiting for message notifications.
In other words, in synchronous mode, the message handler waits for the message to be triggered, while in asynchronous mode, the trigger mechanism notifies the message handler.

Postscript:
This is a drag-and-drop essay. In fact, most of the things I want to write are completed and I feel that I have not fully understood these things. I still cannot climb this trap and need to practice it, I hope to update this article in the future.
(I will always find a reason for procrastination. This may be the cause of procrastination. It's not saved ...)

References:
Public Account (Kant writing programs) Article thorough Linux (Unix) Five IO Models
Short book article: https://www.jianshu.com/p/aed6067eeac9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.