When it comes to Io, blocking, non-blocking, asynchronous, synchronization is a topic that cannot be bypassed. To tell the truth, I also did not understand that the online search of a lot of information, we have divergent opinions, a more reliable statement is: "When processing Io, blocking and non-blocking are synchronous IO, using a special API is asynchronous IO." The answers are relatively high, so let's just look at:
http://www.zhihu.com/question/19732473
Most of this information is reasoned, and I still want to use some examples to understand the relationship between blocking, non-blocking, and asynchronous synchronization in a way that we can see in touch. So this series will be my reading notes, because I also stones, and so on after the river I am sorting io this series of articles.
Blocking and non-blocking
Blocking and non-blocking is a concept, there must be a lot of content behind the concept, we started it all. First of all, the subject is the operating system (OS), the object is the process , that is, the blocking is directed at the process; we know that congestion and non-blocking are two ways of dealing with the same problem, so what is the problem? Answer is when I process to read the data, the data is not ready , in this case the operating system policy. User-state process want to go down, need to read the data collected on the hardware, the user state to the distance between the hardware, through an operating system, the operating system at this time to provide two solutions to the user state: ① blocking, the process in the waiting queue to sleep, wake you up; ② non-blocking, back to you a code word, to tell you You might want to ask again.
A little grounding gas. The following program is very simple, constantly read the data from the standard input, and then display.
while (1) {PNS ntowrite = Read (0, buf, sizeof (BUF)), the printf ("ntowrite:%d\n", Ntowrite), and the sleep (2 ); ;
By default, Io is blocked, so you will find that the program will be innocently stuck there, waiting to enter:
[Email protected]:~/f2fs/share_aarch64/filemap$./block_test
However, the above procedure is modified as follows, that is, by fctnl the read operation of the stdin becomes non-blocking, the result will become very different.
-Flag = Fcntl (0, F_GETFL,0); -Flag |=O_nonblock; -Error = Fcntl (0, F_SETFL, flag); - if(Error <0) inprintf"std stdion to non-block fails\n"); - while(1){ theNtowrite = Read (0, BUF,sizeof(BUF)); $printf"ntowrite:%d\n", Ntowrite); Panax NotoginsengSleep2); - the};
This time the result is, the program no longer silly wait, -1 is the operating system to give you information: there is no egg data at present.
[Email protected]:~/f2fs/share_aarch64/filemap$./block_testntowrite:-1ntowrite:- 1ntowrite:-1ntowrite:-1ntowrite:-1ntowrite: -1
.....
Does it have a perceptual understanding of blocking non-blocking? When blocking, the program at the read call silly waiting, because the operating system let you sleep, non-blocking, the operating system will not let you wait, there is data to return data, no data to tell you, but will always return.
Blocking and non-blocking IO step by step