Understanding of synchronous asynchronous, blocking and non-blocking

About synchronization and asynchronous, blocking and non-blocking, I have always been confused. I have a general understanding and sorting out. If there is any error, please correct me. Thank you!

What are synchronous IO and asynchronous IO, blocking IO and non-blocking IO, and what is the difference?

 

1. Synchronous and asynchronous

Synchronous and asynchronous, focusing on the message communication mechanism, mainly for the client side

Synchronization: After the client performs an operation, it needs to wait for the result to return. Only after waiting for the result to return, can the next operation be performed.

Asynchronous: After the client performs an operation, it can return to perform other operations (without waiting for the current execution result to return), and then wait for the notification and come back to perform the operation that was not completed just now. (ajax, node.js asynchronous programming)

 

2. Blocking and non-blocking

Blocking and non-blocking focus on the state of the program while waiting for the result of the call (message, return value). Mainly for the server side

Blocking: Blocking means that before the call result returns, the current thread will be suspended (the thread enters a non-executable state, in this state, the CPU will not allocate a time slice to the thread, that is, the thread is suspended), and the calling thread will only get the result. return.

Non-blocking: A non-blocking call means that the call will not block the current thread until the result is not immediately available.

The difference between calling non-blocking I/O and blocking I/O is that the call returns immediately after the call. After returning, the time slice of the CPU can be used to process other transactions, and the performance is improved at this time.

 

3. Difference

The difference between synchronous IO and asynchronous IO is: whether the process is blocked when data is accessed!

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

Both synchronous and asynchronous are only for the native SOCKET.

Synchronous and asynchronous, blocking and non-blocking, some are mixed, in fact, they are not the same thing at all, and the objects they modify are not the same.
Blocking and non-blocking refer to whether the process needs to wait when the data accessed by the process on the server side is not ready. Simply put, this is equivalent to the implementation difference inside the function, that is, whether to return directly or wait for ready when it is not ready;

Synchronization and asynchrony refer to the mechanism by which the client side accesses data. Synchronization generally refers to the method of actively requesting and waiting for the completion of the I/O operation. When the data is ready, it must be blocked when reading and writing (distinguish the two stages of ready and read and write, synchronous The read and write must be blocked), asynchronous means that after actively requesting data, it can continue to process other tasks, and then wait for I/O, the notification of the completion of the operation, which can make the process not block when reading and writing data. (waiting for "notification")

 

Fourth, give a chestnut:

Lao Zhang drinks tea and boils water

1. Lao Zhang filled the kettle with water and heated it on the gas stove, and waited for the water to boil (synchronized blocking)

2. Lao Zhang filled the kettle with water and put it on the gas stove to heat it, then left, and came back from time to time to see if the water was boiling (synchronous non-blocking)

 

 

Reference link:

http://blog.csdn.net/historyasamirror/article/details/5778378

https://www.zhihu.com/question/19732473

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324455984&siteId=291194637