I. Understanding synchronous, asynchronous, blocking, non-blocking
Appearance characters: Lao Zhang, Kettle Two (ordinary kettle, referred to as a water bottle, the kettle will ring, referred to as the kettle).
1 Lao Zhang put the kettle on the fire and conforming the water. (Synchronous blocking)
Lao Zhang thinks he is a bit silly.
2 Lao Zhang put the kettle on the fire, go to the living room to watch TV, and occasionally go to the kitchen to see if the water is open. (Synchronous non-blocking)
Lao Zhang still feel a little silly, then become high-end, bought a will ring flute of that kind of kettle. After the water is open, you can make a loud noise.
3 Old Zhang put the kettle on the fire and conforming the water. (Asynchronous blocking)
Lao Zhang thinks such silly meaning is not big.
4 Old Zhang put the kettle on the fire, went to the living room to watch TV, the kettle rang no longer to see it, rang again to get the pot. (Asynchronous non-blocking)
Lao Zhang thought he was smart.
The so-called synchronous asynchronous, just for the kettle.
Ordinary kettle, synchronous; kettle, asynchronous.
Although all can work, but the kettle can be completed after their own, the old Zhang Water opened. This is not an ordinary kettle.
Synchronization can only allow callers to poll themselves (in case 2), resulting in inefficient old Zhang.
The so-called blocking non-blocking, only for the old Zhang.
The old Zhang of conforming, blocking; Old Zhang watching TV, non-blocking.
Situation 1 and situation 3 old Zhang is blocked, the daughter-in-law shouted he did not know. Although the 3-ring kettle is asynchronous, it does not make much sense to conforming's old Zhang. So asynchronous is generally used in conjunction with non-blocking, in order to play an asynchronous effect.
Find another example of buying a book:
Asynchronous/Synchronous:
Async: You go to the bookstore, ask "How to conquer the beautiful girl" arrived no, the boss said, no arrival, arrived to call you. You're gone, and you don't have to go to the bookstore before you get a call from your boss.
Synchronization: You go to the bookstore, ask "How to conquer the United States Maiden" arrived no, the boss said, did not arrive, you go, after a period of time, you went to the bookstore, asked "How to conquer the United States Maiden" arrived no, the boss said no, .... In this way, you constantly come to the bookstore to ask yourself. Until your shoes are broken ...
Note: When you leave the bookstore (either asynchronously or synchronously), you can do your own thing, such as doing a big health care.
Blocking/non-blocking
Blocking: You go to the bookstore, ask "How to conquer the United States Maiden" arrived no, the boss said, did not arrive, so you in the bookstore bitter wait, and so on the book arrives, besides sitting, nothing can do, until the book comes.
Non-blocking: You go to the bookstore, ask "How to conquer the United States Maiden" arrived no, the boss said, no arrival, you left. (As for not coming to the bookstore, it depends on your mood.) But in the computer, do not finish the thing, is to deal with. )
Asynchronous/synchronous is the way you get to the message, and the concern ismessage communication mechanism (synchronous communication/asynchronous communication)。
Blocking/non-blocking is how you handle things, and the concern isState of the program while waiting for the call result (message, return value)。
The two groups of concepts are not at one level.
In the example above, you are the equivalent of a process in a computer, and your two legs are the equivalent of a CPU. (analogy is far-fetched also)
Second, the analysis
In the high-performance I/O design, there are two well-known modes reactor and Proactor modes, where reactor mode is used for synchronous I/O, and Proactor is used for asynchronous I/O operations.
Before comparing these two patterns, we first understand a few concepts, what is blocking and non-blocking, what is synchronous and asynchronous.
synchronous and asynchronous are for application and kernel interaction, synchronous refers to the user process to trigger IO operations and wait or poll to see if the IO operation is ready, and asynchronous refers to the user process triggered IO operation began to do their own things, The IO is notified when the IO operation is complete (the feature of asynchronous is notification ).
while blocking and non-blocking is for the process at the time of access to data, according to the readiness of the IO operation to take a different way, white is a read or write operation function Implementation mode, blocking mode read or write function will wait, not blocking mode, The read or write function returns a status value immediately .
The first IO operation is actually divided into two steps: initiating the IO request and the actual IO operation.
The difference between synchronous IO and asynchronous IO is whether the second step is blocked, if the actual IO read/write blocking request process, then is synchronous io, so blocking IO, non-blocking IO, IO take, signal-driven IO is synchronous io, if not blocked, but the operating system to help you do the IO operation and return the results to you, So that's asynchronous IO.
The difference between blocking IO and non-blocking IO is that the first step is whether the initiating IO request will be blocked, and if blocking until done is the traditional blocking IO, and if it does not block, then it is non-blocking IO.
When it comes to blocking, I'm going to say I/O waits first. I/O wait is unavoidable, so since there is waiting, there will be blocking, but notice that we say the blocking refers to the current process of initiating I/O operation is blocked
Synchronous blocking I/O means that when a process invokes certain system calls or library functions that involve I/O operations, such as accept (which is also counted as I/O operation), send (), recv (), and so on, the process pauses and waits for the I/O operation to complete before continuing. This is a simple and effective I/O model that can be combined with multiple processes to efficiently utilize CPU resources, but at the cost of a large amount of memory overhead for multiple processes.
Synchronous blocking process sitting in water, you can't burn porridge
Synchronous non-blocking is similar to using a process to sit on water and burn porridge. while (true) {if ...} The benefit is that a process handles multiple I/O requests. The disadvantage is the need to keep polling.
The difference is not waiting for data to be ready. Because the data accounted for 80% of the waiting time. The advantage of synchronous non-blocking is that multiple I/O operations are handled simultaneously in one process.
In synchronous blocking I/O, the time that the process actually waits may consist of two parts, one waiting for the data to be ready, and the other waiting for the data to replicate, which may take longer for network I/O. In contrast, a call to synchronize non-blocking I/O does not wait for the data to be ready, and if the data is unreadable or not writable, it returns to the tell process immediately.
For example, when we use non-blocking recv () to receive network data, if there is no data to be received in the NIC buffer, the function returns in time, telling the process that no data is readable. This non-blocking I/O, combined with repeated polling, is attempted compared to blocking I/O
Data is ready to prevent the process from being blocked, the biggest benefit is that multiple I/O operations can be handled concurrently in one process. But it takes a lot of CPU time to make the process busy waiting because it requires the process to perform multiple polling to see if the data is ready.
Non-blocking I/O is generally only valid for network I/O, so we use o_nonblock in the socket's option setting so that the Send () or recv () of the socket is non-blocking.
If the server wants to receive data from multiple TCP connections at the same time, it must rotate the method that receives the data for each socket call, such as recv (). Regardless of whether these sockets have data to be received, ask again, if most sockets do not have the data to receive, then the process will waste a lot of CPU time to check these sockets, which is obviously not what we want to see.
Synchronous and asynchronous, blocking and non-blocking, some mixing, in fact, they are completely not the same, and they modify the object is not the same.
Blocking and non-blocking refers to whether the process needs to wait when the data accessed by the process is not ready, simply saying that this is equivalent to the implementation difference within the function, that is, when it is not ready to return directly or wait for the ready;
While synchronous and asynchronous refers to the mechanism of accessing data, synchronization generally refers to the active request and wait for the completion of the I/O operation, when the data is ready to read and write must block (difference between ready and read and write two stages, synchronous read and write must block), asynchronous refers to the active request data can continue processing other tasks, and then wait for I/ Notification that the operation is complete, which allows the process to read and write data without blocking. (Wait for "notification")
In most cases, the Web server uses queue-based free competition for these requests, consumes CPU and I/O resources through multi-execution streams (multi-process or multi-threaded), and reduces any innocent wait times, including a number of implementation-specific concurrency strategies, in real-world applications, especially Web servers, It is essential to handle a large number of file descriptors at the same time. The advent of multiple I/O readiness Notifications provides a high-performance solution for a large number of file descriptor readiness checks, which allows processes (such as electronic screens, to smell the taste of various restaurants) to monitor all file descriptors at the same time, You can quickly get all the ready file descriptors and then access the data only for those file descriptors.
Back to the story of buying noodles, if you buy not only a portion of noodles, but also in a few other snack bars to buy dumplings, porridge, pies, etc., because the friends who go shopping together see your noodles are also hungry. These things all need time to wait for production. In the synchronous non-blocking I/O model, you have to take turns to go to the snack bar to ask about progress, painful. Now after the introduction of multi-Channel I/O ready notification, the snack city administration to the hall installed an electronic screen, after all the food in the snack bar will be displayed on the screen, this is a good news, you just need to see the interval of the big screen can be, perhaps you can also visit the nearby shops, You can also see the big screen not far away.
Multi-ready: 1. Emphasis on multi-channel. 2. Only the requested data is ready. Not read/write for I/O
Epoll is targeting such a scenario.
Select, Epoll only requires that the process (i) passively receive data ready (noodles) notifications. Conforms to the definition of async. No need to stay in a restaurant (synchronous blocking). Or polling (synchronous non-blocking).
Linux-synchronous asynchronous non-blocking blocking parsing