Also talk about blocking, non-blocking, synchronous and asynchronous

In recent recruitment, I found that many people have a very vague understanding of BIO, NIO, AIO, etc., and felt it necessary to write articles to correct many people's misunderstandings.
Before talking about these, it is necessary to introduce the five IO models of Unix:
Blocking:
Blocking is the most commonly used IO model, and all file operations are blocked by default. Take socket programming as an example. Call recvfrom in the process space, and its system call does not return until the data message arrives and is copied to the buffer area of ​​the application process (or an error occurs), and it has been waiting during this period. The process is blocked for the entire period from when recvfrom is called to when it returns. There is a very classic picture:
Blocking IO model
non-blocking:
in the process of calling recvfrom from the application layer to the kernel, if there is no data in the buffer, an EWOULDBLOCK error will be returned directly, and the check status will generally be polled to see if the kernel space has There is no data to come. Until there is data, the copy is finally completed. As shown in the figure below:
Non-blocking IO model
IO multiplexing:
select/poll/epoll provided by the Linux system, the process passes one or more FD (file descriptor) to one or more poll/select system calls, blocking in select. Select and poll can help monitor whether many FDs are ready. However, select and poll are sequential scans to check the ready status of the FD, which is relatively inefficient, and the number of FDs supported is limited (if I remember correctly, the default seems to be 1024 or 2048, which is not clear). And epoll is an event-driven way, when the FD is ready, the callback function rollback is immediately called back. Figure:
IO multiplexing model
Speaking of epoll, I have to mention a classic question, the comparison between apache and nginx, why nginx is much more efficient than apache, this is the fundamental reason.
Signal-driven:
This model has very few practical applications. I won't introduce too much here. You can see the picture:
Signal-driven IO model
Asynchronous:
Tell the kernel to start an operation, and let the kernel notify after the entire operation (including copying data from the kernel space to its own buffer). The main feature of asynchronous IO is to actively notify after completing the operation. As shown:
Asynchronous IO model

Well, the above may be a bit abstract. Here the language of the popular point to sum up blocking, non-blocking, synchronous, asynchronous
blocking, non-blocking: the data process / thread is ready to be accessed, whether the process / thread to wait;
synchronous, asynchronous: access data, synchronization needs Actively read and write data, it will still block in the process of reading and writing data;
asynchronous only requires the notification of the completion of the I/O operation, and does not actively read and write data. The operating system kernel completes the reading and writing of data.
Let's take another example that is circulated on the Internet that is very easy to understand:
Lao Zhang loves to drink tea, not to talk nonsense, to boil water.
Characters: Lao Zhang, two kettles (common kettle, referred to as kettle; ringing kettle, referred to as ringing kettle).
1 Lao Zhang put the kettle on the fire and immediately waited for the water to boil. (Synchronization blocking) Lao Zhang feels a little silly
2 Lao Zhang puts the kettle on the fire, goes to the living room to watch TV, and goes to the kitchen from time to time to see if the water is boiling. (Synchronous non-blocking)
Lao Zhang still felt a little stupid, so he became high-end, and bought a kettle that can sound the flute. After the water is boiled, it can make a loud ~~~~ noise.
3 Lao Zhang put the kettle on the fire and waited for the water to boil. (Asynchronous blocking) Lao Zhang feels so stupid that it doesn't make much sense to wait.
4 Lao Zhang put the kettle on the fire and went to the living room to watch TV. He stopped watching it before the kettle rang, and went to get the kettle when it rang. (Asynchronous non-blocking) Zhang thinks he is smart.
The so-called synchronous and asynchronous is only for the kettle. Ordinary kettle, synchronous; ringing kettle, asynchronous. Although they can work, the sound of the kettle can remind Lao Zhang that the water is boiling after he finishes the work. This is beyond the reach of ordinary kettles.
Synchronization can only allow the caller to poll himself (in case 2), causing Zhang's inefficiency. The so-called blocking non-blocking is only for Lao Zhang. The standing old Zhang is
blocking; the old Zhang watching TV is non-blocking. In case 1 and case 3, Lao Zhang was obstructed, and he didn't know when his wife called.
Although the ringing kettle in 3 is asynchronous, it doesn't make much sense to the old Zhang who is waiting. Therefore, generally asynchronous is used in conjunction with non-blocking, so as to play the role of asynchronous.

Guess you like

Origin blog.csdn.net/dinglang_2009/article/details/50461697