Blocking/non-blocking/synchronous/asynchronous detailed

Source: Internet
Author: User

Welcome reprint, Reproduced please specify the original address: http://blog.csdn.net/majianfei1023/article/details/45314803


0. Introduction

Are blocking not synchronous, non-blocking is not asynchronous, what is their relationship? Students who have just come into contact with network programming often ask such questions and confuse them, and here I use my own understanding to explain them in detail.


Thought for a long time, decided to first talk about blocking and non-blocking, for what, because their timing in front (at least I understand).

Let's conclude with a summary and end with another sentence.


the difference between blocking IO and nonblocking io is that they are blocking/non-blocking, which is the state of the program waiting for a message (called a function) and whether the application's call returns immediately!


Synchronous IO and asynchronous IO are different : synchronous/asynchronous, they are the notification mechanism of the message, synchronous IO is called after the completion of the notification, asynchronous is called after the completion of the return, do their own things, and then the message will naturally have its way (state, notification, callback, etc.) to notify it.


So, the points they focus on are different, and the blocking and non-blocking concerns are whether the call returns immediately, and the synchronous and asynchronous concerns are how the message is notified to the program.


First, blocking and non-blocking
blocking/non-blocking, they are programs in Waiting for Messages (no synchronous or asynchronous) state.


1. Blocking the I/O model:
A blocking call means that the current thread is suspended until the call results are returned. Functions are returned only after the result is obtained.

Below I use the UNP1 diagram to explain:


6.1:

The process calls Recvfrom to the kernel to request data, and the kernel data is not returned to the process when it is not ready. The process then blocks the Recvfrom system call to wait for the data to be ready.

The blocking function does not allow the program to call another function until it finishes its specified task. For example, when a program executes a function call that reads data, the next program statement will not be executed until the function completes the read operation. When the server is running to the Recvfrom statement, and no Client connection service request arrives, the server stops waiting for the connection service request to arrive on the Recvfrom statement. This condition is called blocking (blocking). The CPU does not allocate a time slice to the thread when it is blocked, that is, the thread pauses, but the CPU can be used by other threads (or processes) without wasting CPU performance.

When the data is not ready and no indication of success (or failure) is returned, the card is called at Recvfrom and does not continue to go down. Until the call returns success (or failure).


2. Non-blocking I/O model:
The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.


6.2:

When no data is ready in the kernel, the kernel immediately returns a Ewouldblock error to the application, and the application does not need to put the process into sleep. In this way, the application (process) needs constant system calls, that is, polling (polling). The application continuously polls the kernel to see if the data is ready. Doing this often consumes a lot of CPU time (of course you just want to see if the data is ready and not ready to poll).

What's the difference between seeing the two figures above? While waiting for the data phase,


Really look at the picture than I balabala to speak a lot of useful, we look carefully, system calls recvfrom divided into two stages,

1. Wait for the data ready stage (this determines whether it is blocking or non-blocking);

2. The phase of the data being copied from the kernel to the user space (this we'll talk about later, it determines whether it's synchronous or asynchronous).

As can be seen from the graph, the difference between blocking and non-blocking is mainly when waiting for data to be ready or returning immediately.

So: blocking/non-blocking, which is the state of the program while waiting for a message (it doesn't matter whether it's synchronous or asynchronous). (Blocking is always blocking until data is ready, non-blocking is returning immediately).


Second, synchronous and asynchronous

synchronous/asynchronous, which is the notification mechanism for messages:


1. Synchronous IO Model:
The so-called synchronization is that when a function call is made, the call does not return until the result is obtained.
2. Asynchronous IO Model:
When an asynchronous procedure call is made, the caller does not get the result immediately.
The part that actually handles this call is after the call is issued,

Notifies the caller by status, notification, or through a callback function to handle the call.

(that is, the blocking concern is to return immediately or later, the asynchronous return is not required, he does not care about this, will automatically notify him after processing)


We continue to look at figures 6.1 and 6.2, before studying blocking and non-blocking, just to study their differences, so now we look at their similarities: see the bottom half, when the data is copied from the kernel to the user space, until the replication is completed, returned to the process, the process continues.


So let's look at one more picture:


6.5, this is the asynchronous I/O model, I don't have to say, everybody saw it. So what's the difference between this and the two graphs above (that is, copying data from the kernel to the user space, and then the process continues to execute)?

There is only one truth:

After the process has been called, it continues (that is, we say, it does not care about returning), the data is copied to the user space, the process continues to execute, then how to tell the process after the data is copied to the user space? Anyway, the kernel has a way to inform you that the data has been processed, you can choose to deal with.

This is asynchronous, the above is naturally synchronous, we summarize the difference between synchronous and asynchronous :

Synchronous I/O operation: Causes the request process to block until the I/O operation is complete (you are not finished, I will watch you do, then do nothing, when you have finished, give me the things);

asynchronous I/O operation: does not cause the request process to block. And when you finish the copy, find a way to notify the process is, it is not lazy to wait for you (so, asynchronous I/O is very proud, I am the boss, I rely on what you, I first do other things, wait for you to deal with, make a phone call to tell me)


The difference between blocking IO and non-blocking IO is that the application's call returns immediately !


The difference between synchronous IO and asynchronous IO is that the process is blocked when the data is copied !


So, to put it simply, blocking and non-blocking refers to waiting for the data phase, whether the call returns immediately, while synchronous async refers to, when copying the data, I was blocking here waiting for you to return, or I first busy other, wait for you to deal with, tell me.


I will continue to take a picture to summarize:



If we want to observe blocking and non-blocking, observe 1 and 2 to be able to:

Returns immediately after a call is made to the program.

If we want to observe both synchronous and asynchronous, observe 3 and 4 to be able to:

Whether the process is blocked when the function finishes (is passively waiting to be returned, or you are actively notified).

The first column is: synchronous blocking I/O model

The second column is: Synchronous non-blocking I/O model

The fifth column is: asynchronous I/O model.


For UNIX: Blocking I/O (default), non-blocking I/O (nonblock), I/O multiplexing (select/poll/epoll) are synchronous I/O because they are blocked when data is copied back to the process buffer by kernel space (nothing else). Only the asynchronous I/O model (AIO) is consistent with the meaning of asynchronous I/O operations, that is, when the 1 data is ready, 2 is notified of the process after the kernel space is copied back to the buffer, and you can do something else while waiting for the notification.









Blocking/non-blocking/synchronous/asynchronous detailed

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.