Then draw on the theory in the UNIX Network programming Volume:
The 2 main objects involved in IO operations are program processes, system cores. As an example of a read operation, when an IO read occurs, it typically goes through two steps:
1, waiting for data preparation
2. Copy data from the system kernel to the operation process
For example, in a read operation on the socket, step 1 waits for the network packet to arrive, then copies it to the system kernel's buffer, and step 2 copies the packet from the kernel buffer into the buffer of the program process.
Blocking (blocking) and non-blocking (non-blocking) IO
IO blocking, non-blocking is mainly manifested in an IO operation, if some operations are slow, such as the need to prepare data for the read operation, then the current IO process is waiting for the operation to complete, or know that temporarily unable to operate after the first to do something else? Always wait and do nothing until finished, this is blocking. Take the time to do something else, which is non-blocking.
Non-blocking IO responds immediately after the IO request is made, and returns an error ID if the packet is not ready, making the operation process not blocked there. The operation process passes through multiple requests until the data is ready to return a successful identity.
Think of the following two scenarios:
A Xiao Ming and small just two people are very upright introverted, one day Xiao Ming to find small just borrow a book: "Small Just ah, you that xxx borrowed me to see." So small just went to find books, Xiaoming waited, find a half-day found, the book to Xiao Ming.
B Xiao Ming and small just two people are very lively outward, a day Xiao Ming came to find small just borrow books: "Hey little just, you that xxx lend me to see." Xiao Gang said: "I have to find a while," Xiao Ming went to play ball. After a while, the book was found and the book was given to Xiaoming.
Conclusion: A is blocked and B is non-blocking.
From the CPU point of view, non-blocking significantly improves CPU utilization, and the process will not wait there. But it also leads to an increase in thread switching. Increased CPU usage can compensate for the switching cost of the system needs to be evaluated well.
Synchronous (synchronous) and asynchronous (asynchronous) IO
To take a look at the formal point definition, the POSIX standard divides the IO model into two types: synchronous IO and asynchronous Io,richard Stevens in the UNIX Network programming Volume:
A synchronous I/O operation causes the requesting process to being blocked until that I/O operation completes;
An asynchronous I/O operation does not cause the requesting process to be blocked;
As you can see, the standard for judging synchronization and asynchrony is whether an IO operation is done until it is complete, causing the program process to block. If blocking is synchronous, no blocking is asynchronous. The IO operation here refers to the real IO operation, which is the process of copying data from the kernel to the system process (read).
To continue the example of borrowing in front of you, asynchronous borrowing is like this:
C Xiao Ming is very lazy, a day Xiao Ming to find small just borrow books: "Hey little just, you that xxx borrowed me to see." Xiao Gang said: "I have to find a while," Xiao Ming went out to play and let small just if found the book to him. Xiao Gang is a responsible person, found the book sent to Xiao Ming's hand.
A and B of the borrowing methods are synchronized, someone to ask B is not a non-blocking, how to sync?
The previous 2 steps of the IO operation: preparing the data and copying the data from the kernel to the program process. Map to this example, the book is the preparation of data, small just is the kernel, Xiao Ming is the program process, small just give the book to xiaoming this is copy data. In the B way, small just find book this time Xiao Ming really is not idle, what to do, but small just find book to Xiao Ming's This process is to copy data This step, Xiao Ming still have to come back waiting for small just handed over the book. So here it's blocked, according to the definition above, so it's synchronous.
When it comes to IO processing, you typically encounter a choice between synchronous and asynchronous processing. Synchronization guarantees the reliability of the program, and asynchronous can improve the performance of the program. Xiao Ming himself to pick up the book no matter waiting for the book sooner or later, expect to find a small just sent to, in case the small just forget or have urgent busy other, that book is gone.
Discuss
There are really a lot of similarities between synchronous and asynchronous, blocking, and non-blocking, and it's easy to confuse. Wikis also equate asynchronous with non-blocking, and more people think they are different. There may be many reasons, each person's knowledge background is different, set the context is also different.
My view is: in Io, according to the concept of synchronous asynchronous above, it can also be seen that synchronous and asynchronous is often expressed in blocking non-blocking form, and is through an intermediate processing mechanism to achieve the effect of asynchrony. Synchronization and Asynchrony are often the mode of collaboration between the IO operation requester and the respondent during the IO actual operation phase, while blocking nonblocking is more exactly a state of its own state, the current process, or a thread.
After an IO read request is made, the blocking IO waits for data to be readable, while the data is readable, waiting for the data to be copied from the kernel to the system process, and non-blocking IO will return immediately. As for how data is handled is the process of its own things, unrelated to synchronous and asynchronous.
Blocking non-blocking IO and synchronous asynchronous IO in Java