Io blocking and io non-blocking reprint

Source: Internet
Author: User

 

Synchronous and asynchronous Io, blocking and non-blocking Io

Many times we often see synchronous and asynchronous, blocking, and non-blocking. In some cases, the synchronization and blocking operations are placed with equal signs. Asynchronous and non-blocking draw an equal sign. In fact, this is not correct. Synchronization is not equal to blocking, while Asynchronization is not equal to non-blocking. Next, let's take a closer look at the conceptual differences between synchronization and Asynchronization, blocking and non-blocking, and their combined applications.
  Synchronization:The so-called synchronization means that when a function call is sent, the call will not return until the result is not obtained. According to this definition, most functions are called synchronously (such as sin and isdigit ). But in general, when we talk about synchronization and Asynchronization, we are referring to the tasks that require the collaboration of other components or a certain amount of time. The most common example is
Sendmessage. This function sends a message to a window. This function does not return a message before the recipient finishes processing the message. After the other party completes processing, this function returns the message processing function.
The lresult value is returned to the caller.
  Asynchronous:Asynchronous concept and synchronization relative. When an asynchronous process is called, the caller cannot obtain the result immediately. After the call is completed, the caller is notified by status, notification, and callback. To
The casycsocket class is used as an example (note that csocket is derived from casyncsocket, but the function has been converted from Asynchronous to synchronous). When a client calls
After the connect function sends a connection request, the caller's thread can immediately run down. After the connection is established, the socket underlying layer sends a message to notify the object.
Here \ it is mentioned that the execution part and the caller can return results in three ways: Status, notification, and callback. Which one can be used depends on the implementation of the execution part, unless the execution part provides multiple options, it is not controlled by the caller. If the execution part is notified by the status, the caller needs to check the status at intervals, which is very inefficient. (Some new programmers prefer to use a loop to check the value of a variable, this is actually a very serious error ). If you use the notification method, the efficiency is very high, because the execution of components almost do not need to do additional operations. As for the callback function, it is actually not much different from the notification.
   Blocking:Blocking call means that the current thread will be suspended before the call result is returned. The function is returned only after the result is obtained.
Some people may equate blocking calls with synchronous calls. In fact, they are different. For synchronous calls, the current thread is still activated in many cases, but the current function does not return logically. For example
Csocket calls the receive function. If there is no data in the buffer, this function will wait until there is data to return. At this time, the current thread will continue to process a variety of messages. If the main window and the calling function are in the same thread, the main interface should be refreshed unless you call the function in special interface operations.
Another function Recv used by socket to receive data is an example of blocking calls. When the socket works in blocking mode, if the function is called without data, the current thread will be suspended until there is data.
   Non-blocking:The concept of non-blocking corresponds to blocking, which means that the function will not block the current thread and return immediately before the result cannot be obtained immediately.

The above concepts are all textbook concepts. Next I will talk about my personal understanding. The so-called synchronization means that when a process initiates a function (task) call, it will always be completed by the function (task. The process continues to run. This is not the case in asynchronous mode. In asynchronous mode, when a process initiates a function (task) call, it does not wait for the function to return, but continues to execute it, functions return status, notification, and event. Process Task completion.

The concept of blocking and non-blocking is much clearer. Blocking means that the process is suspended when the request cannot be met. If the request is not blocked, the system returns the result directly.

Their combination: (network load)

Figure
2. The traditional blocking I/O model is provided, which is also the most commonly used model in applications. Its behavior is very easy to understand, and its usage is very effective for typical applications.Before calling
When the read system is called, the application is blocked and context switches are performed on the kernel. Then, the read operation is triggered. When the response is returned (from the device we are reading from), the data is moved to the user space buffer. Then the application unblocks (read call back ).

Figure
2. Typical process of synchronous blocking I/O model
 

From the application perspective, the read call will last for a long time. In fact, when the Kernel performs read operations and other work, the application is indeed blocked.

PS.
I understand that the Read Request is blocked and does notAsynchronousNotification mechanism, because the application has been waiting in this process, that is, it has been actively querying, so it isSynchronization. (Sequence)

SynchronizationNon-blocking
I/O

SynchronizationBlocking
One of the lower efficiency variants of I/O isSynchronizationNon-blocking
I/O. In this model, devices are opened in a non-blocking manner. This means that the I/O operation will not be completed immediately, and the read operation may return an error code, indicating that this command cannot be met immediately (eagain or
Ewouldblock), as shown in 3.

Figure
3. Typical process of synchronizing non-blocking I/O models

 

   

The non-blocking implementation is
The I/O command may not be immediately met, and the application needs to call it many times to wait until the operation is completed. This may be inefficient,In many cases, when the kernel executes this command, the application must wait until the data is available or try to execute other work.Zheng
As shown in 3, this method can introduce the latency of I/O operations, because the data becomes available to the user for calling in the kernel.
There is a certain interval between the data returned by read, which will reduce the overall data throughput.

PS.
I understand that the Read Request is non-blocking, but there is noAsynchronousNotification mechanism, but the application needs to actively query, so yesSynchronization. (Multiple tests)

AsynchronousBlocking
I/O

Another blocking solution is non-blocking with blocking notifications.
I/O. In this model,Configure non-blocking I/O, and then use Blocking
Select system call to determine when an I/O descriptor has an operation
. Enable
It is interesting that a select call can be used to provide notifications for multiple descriptors, not just for one descriptor. For each prompt, we can request that this descriptor can write data, read data is available, and whether an error occurs.

Figure
4. Typical process of asynchronous blocking I/O model
(Select)

 

The main problem with select calling is that it is not very efficient. Although this isAsynchronousNotification is a convenient model, but for high-performance
I/O operations are not recommended.

PS.
I understand that the read request is not blocked,AsynchronousIn the way of notification, the slect system call is blocked, which causes the application to be blocked.Asynchronous, But it is blocked. (Wait for the notification and complete it by yourself)

AsynchronousNon-blocking
I/O (AIO)

Finally,AsynchronousNon-blocking
The I/O model is a model that processes overlapping I/O.Read requests are returned immediately.
The read request has been initiated successfully. When the read operation is completed in the background, the application then performs other processing operations. When
When the read response arrives, a signal is generated or a thread-based callback function is executed to complete the I/O processing process.

Figure
5. Typical asynchronous non-blocking I/O model process


 

To execute multiple
The overlapping processing of computing operations and I/O processing for I/O Requests utilizes the processing speed and
I/O speed difference. When one or more I/O requests are suspended, the CPU can execute other tasks.
I/O operations on completed I/O at the same time.

PS.
I understand that the read request is not blocked,AsynchronousThe callback function is used in the notification method, and no application is required to process the callback function. (Commissioned)

Asynchronous
I/O
Motivation

From front
In the classification of the I/O model, we can see the motivation of AIO. The blocking model must be
The application is blocked at the beginning of an I/O operation, which means that it is impossible to overlap processing and I/O operations at the same time.SynchronizationThe non-blocking model allows processing and
I/O operations overlap, but this requires the application to check the status of I/O operations based on the reproducible rules. This leaves usAsynchronousNon-blocking
I/O, which allows overlapping processing and I/O operations, including
I/O operation completion notification.

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.