A Preliminary Study of PHP socket-Some Boring Theories about IO

  Blocking, io, php, socket, Synchronization

[Original Address:https://blog.ti-node.com/blog …]

To better understand socket programming, there is a link that cannot be bypassed is io.
In Linux, everything is a file. What do you actually want files for? Isn’t it just reading and writing? Therefore, the essence of this sentence is “IO is king”. Use php fopen to open files and close files for reading, writing and writing. This is called local file IO. In socket programming, the essence is network IO.
Therefore, before starting further socket programming, we must know io conceptually.
If you still don’t have any concept of IO here, I will give you a general impression through several words: synchronous, asynchronous, blocking, non-blocking, even synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking. Are you dizzy? So far, you can simply think that if you understand the meaning and difference of these nouns, even if you understand IO, at least you can continue to look down.
First, there is a wave of mechanical memory: IO is divided into two types, synchronous and asynchronous.

SynchronizatIOn io:
  • Blocking IO
  • Non-blocking IO
  • IO multiplexing (including select, poll, epoll)
  • Signal driven IO
Asynchronous IO

So how do you understand the difference between these concepts? Especially synchronous and blocking, asynchronous and non-blocking, look the same.
Let me give you an example to illustrate with my own understanding:

  1. When you went to sweet heart steamed bread shop to buy tai chi steamed bread, ah mui said, “not for the time being, it’s steaming. watch it yourself!” “… so you just stood by and waited for steamed bread. at this time, you,It is blocked,Is synchronizedThe obstruction is manifested in that you do nothing but wait for steamed bread. The synchronization is manifested in that Ah Mui does not provide notification service while waiting for steamed bread. You have to wait for the news of “steamed bread coming out”.
  2. When you went to sweet heart steamed bread shop to buy tai chi steamed bread, ah mui said, “not for the time being, it’s steaming. watch it yourself!” “… so you just stood by and sent WeChat and asked,” Are you ready? “and then send QQ, and then ask,” are you ready? “… you at this time,Non-blocking,Is synchronizedThe non-blocking performance is that you do other things besides waiting for steamed buns, and from time to time you will take the initiative to ask whether the steamed buns are good or not. The synchronous performance is that ah mui does not provide notification service while waiting for the steamed buns, and you have to wait for the news of “steamed buns coming out”.
  3. When you went to sweet heart steamed bread store to buy tai chi steamed bread, ah mui said, “not for the time being, it’s still steaming. I’ll call you when it’s steamed! “… but you are still standing by waiting for steamed buns. At this time, you,It is blocked,It is asynchronousThe obstruction is manifested in that you do nothing but wait for steamed bread. Asynchronous performance is manifested in the process of waiting for steamed bread. Ah Mui provides the news of “steamed bread coming out of the oven” by telephone. You only need to wait for Ah Mui’s telephone.
  4. When you went to sweet heart steamed bread store to buy tai chi steamed bread, ah mui said, “not for the time being, it’s still steaming. I’ll call you when it’s steamed! “… so you left, bought a pair of new sneakers and looked at the martial arts school. in short, you stopped asking about steamed bread and waited for ah mui’s phone call.Non-blocking,It is asynchronousThe non-blocking performance is that you do other things besides waiting for steamed buns, and from time to time you will take the initiative to ask whether steamed buns are good or not. Asynchronous performance is in the process of waiting for steamed buns, Ah Mui provides the news of “steamed buns coming out of the oven” by telephone. You only need to wait for Ah Mui’s telephone.

If you carefully read every word in the above case, you can slowly realize that the reason why asynchrony and nonblocking, synchronization and blocking are easily confused, is only because the expressions of the two are slightly similar.
Blocking and non-blocking concerns: what are you doing while waiting for steamed bread?
Synchronous and asynchronous concerns are: do you wait for the result of “steamed bread coming out” or do you run away immediately and wait for the “steamed bread coming out” ah mui told you. the key point is how do you know about “steamed bread coming out”.
Therefore, in the real world, the silliest person will use asynchronous blocking IO method to write programs. In the other three methods, more people will choose synchronous blocking or asynchronous non-blocking. The biggest problem with synchronous non-blocking is that you need to keep busy in various tasks, causing your brain to be confused and very tired.

[Original Address:https://blog.ti-node.com/blog …]