In high-performance I / O system design, there are several concepts often makes us puzzled. details as follows:

1 What is sync?

2 What is asynchronous?

3 What is blocked?

4 What is non-blocking?

5 What is a synchronous blocking?

6 What is synchronous non-blocking?

7 What is the asynchronous blocking?

8 What is asynchronous non-blocking?

First to give an example of real life:

If you want to eat a kung pao chicken rice bowl,

Synchronous blocking: you go to a restaurant meal, then there waiting, but also shouted: Well, not ah!

Non-blocking synchronization: End point meal in a restaurant, went to walk the dog. But linger for a while, he went back to the restaurant to shout: Well, not ah!

Asynchronous blocking: walking the dog when the restaurant received a phone call that the rice well, allowing you to personally pick up.

Asynchronous non-blocking: the restaurant called to say, we know your location to send you a while to come, feel at ease walking the dog on it.

 

Before a few questions to find out more, we first have to understand what is synchronous, asynchronous, blocking, non-blocking, only a single understanding of these concepts clearly, and then combined to understand, it is relatively easy.

1 is for synchronous and asynchronous interactive applications and the kernel of terms.

2 blocking and non-blocking are different ways for accessing data in the process, according to I / O operations ready to take that white is the way to achieve a read or write operation function. Under blocking mode, read or write function will wait while non-blocking mode, read or write function returns a status value immediately.

 

Described above can be summarized in 10 words: synchronous and asynchronous is the purpose of blocking and non-blocking is implementation.

1 Sync: refers to the user process triggered I / O operations and wait for polling or go see if I / O operation is ready. Their street to buy clothes, personally doing this, can not do anything else.

2 Asynchronous: Asynchronous refers to the user process triggered I / O operations after they started doing their own thing, and when I / O operation to complete will be notified (asynchronous feature is the notification) "I / O completion" of. When told their friends the right clothes size, color, style, commissioned a friend to buy, then they can shop and go to something else (using asynchronous I / O, Java the I / O read and write entrusted to the OS process, the need for data buffers address and size passed OS).

3 blocking: the so-called blocking mode means that, when trying to read and write to the file descriptor, if things did not readable, or is temporarily unavailable written, the program goes to a waiting state until there is something to read or write until the . To the subway station recharge, recharge found this time were absent (probably went to the toilet), and then we just wait and wait until the recharge comes home so far.

Nonblocking 4: non-blocking state, if not something to read, write, or not, the function returns immediately read without waiting. When do business in the bank, to receive a small ticket, then we can play with the phone, or chat with others, when it is our turn, the speaker will notify the bank, this time we will be running a business a.

 

An I / O operation actually divided into two steps: initiating I / O requests and the actual I / O operations.

Synchronous I / O and asynchronous I / O difference is that the second step is blocked, if the actual I / O read and write requests blocking the process, then that is synchronous I / O.

Blocking I / O and the difference between non-blocking I / O is that the first step in initiating I / O request whether it will be blocked, if blocked until the I / O completion, it is the traditional blocking I / O, if not blocked, that is, Non-blocking

I / O.

 

 

Synchronous and asynchronous interaction is for applications and kernel in terms of synchronization refers to the user process triggered I / O operations and wait for polling or go see the I / O operation is in place, and trigger asynchronous refers to the user process I / O after the operation began to do their own thing, and when I / O operation to complete will be notified I / O completed.

Blocking and non-blocking for different ways to access data in the process, according to a ready state I / O operation to take it plainly read or an implementation manner, blocking the way to write functions to read and write function has been waiting, while non-blocking mode, a write function returns the status value immediately.

Therefore, I / O operations can be divided into three categories: synchronous blocking (i.e., early operation BIO), synchronous non-blocking (the NIO), non-blocking asynchronous (AIO).

Synchronous blocking (BIO):

In this way, the user process after initiating an I / O operation must wait for the completion of I / O operations, only when you feel really complete I / O operation, a user process to run. JAVA conventional I / O patterns belong in this way.

Synchronous non-blocking (NIO):

In this way, the user initiates a process I / O operations can return later to do other things, but users need to process from time to time to ask whether I / O operations in place, which requires the user periodically to ask questions, thereby introducing unnecessary CPU resources wasted. Currently JAVA's NIO belongs synchronous non-blocking I / O.

Asynchronous non-blocking (AIO):

In this mode, a user initiates a process I / O operations in the future, do not wait for the kernel I / O operation is completed, the process will notify users after the completion of the core I / O operations.

 

Synchronous blocking I / O (JAVA BIO):

Synchronization and blocking, server implements a model for a connecting thread, that client has a connection request to the server you need to start a thread for processing, if the connection does not do anything it will cause unnecessary overhead of thread. Of course, it can be improved by a thread pool mechanism.

 Synchronous non-blocking I / O (JAVA NIO): non-blocking synchronization, the server requests a mode of realization of a thread, i.e. the connection request sent by the client are registered to the multiplexer, the multiplexer is connected to the poll there are I / O requests only start a thread for processing. A user process needs to ask from time to time I / O operations if ready, which requires the user process periodically to ask questions.

Blocking asynchronous I / O (JAVA NIO):

In this way refers to the application initiates an I / O operation in the future, without waiting for the I / O operation is completed, the application will notify the kernel after the completion of I / O operations, which is actually the most crucial synchronous and asynchronous difference, synchronization must wait or take the initiative to ask I / O completion, then why is it blocked? Because at this time is through select system call to complete, and select the function itself is blocking implementation, and use the select function has the advantage that it can monitor handles multiple files at the same time (if viewed from the perspective of UNP, select belong to synchronize operation, because then select, the process also need to read and write data), thereby increasing the concurrency of the system!

Non-blocking asynchronous I / O (JAVA AIO (NIO.2)):

In this way, the user needs to initiate a process of I / O operations and returns immediately, and other I / O operations after really complete, the application will be notified of I / O operations to complete, then the user only needs to process the data like treatment, does not require the actual I / O read and write operations, because the real I / O read and write operations have been completed by the kernel.

 

 

BIO, NIO, AIO application of scene analysis:

The number of connected mode applies to relatively small BIO and fixed architecture, server resources in this way is relatively high, limited concurrent applications, the JDK1.4 previously only option, but the program intuitive and easy to understand.

NIO connection number suitable for multi-mode and connected to short (light operation) architecture, such as chat server, limited concurrent applications, more complex programming, starts the JDK1.4 support.

AIO method is applicable to more than the number of connections and the connection is relatively long (heavy operation) architecture, such as the album server, call the OS to fully participate in concurrent operation, programming is more complex, JDK7 began to support.

 

 


 Reprinted to link: https: //my.oschina.net/u/553266/blog/3022046.