1. Conceptual analysis
Believe that a lot of Linux background development work has been in contact with the concept of synchronous & asynchronous, blocking & non-blocking, but also believe that there have been misunderstandings, such as the idea that synchronization is blocking, asynchronous is non-blocking, the following we first analyze what the meaning of these concepts respectively.
Synchronous:
The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. That is, one thing must be done . To do the next thing when the previous one is done.
For example, normal B/s mode (synchronous): Submit request, wait for server processing, processing completed return this period the client browser can not do anything
Asynchronous:
Asynchronous concepts and synchronization are relative. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion.
For example, an AJAX request (asynchronous): The request isprocessed by the event-triggered server (which is what the browser can still do) .
Blocking:
A blocking call means that the current thread is suspended until the call results are returned (the thread goes into a non-executable state where the CPU does not allocate a time slice to the thread, that is, the thread pauses). Functions are returned only after the result is obtained.
Someone might equate blocking calls with synchronous calls, and in fact he is different. For synchronous calls, many times the current thread is still active, but logically the current function does not return, it will preempt the CPU to perform other logic, and will proactively detect if the IO is ready.
Non-blocking
The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.
The simple point of understanding is:
1. Sync, that is, I call a function, the function does not end before I death the result.
2. Asynchronous, that is, I call a function, do not need to know the result of the function, the function has a result notify me (callback notification)
3. Blocking is called me (function), I (function) did not receive the data or did not get the results, I will not return.
4. Non-blocking, just call me (function), I (function) immediately return, via select Notify caller
The difference between synchronous IO and asynchronous IO is that the process is blocked when the data is copied
The difference between blocking IO and non-blocking IO is that the application's call returns immediately
In conclusion, synchronous and asynchronous, blocking and non-blocking, some mixing, in fact, they are not the same thing, and they modify the object is not the same.
2. Five Kinds of IO models
After understanding the concepts of synchronous and asynchronous, blocking, and non-blocking, let's talk about the five IO models of Linux:
1) Blocking I/O (blocking I/O)
2) non-blocking I/O (nonblocking I/O)
3) I/O multiplexing (SELECT and poll) (I/O multiplexing)
4) Signal-driven I/O (signal driven I/O (SIGIO))
5) asynchronous I/O (asynchronous I/O (the POSIX aio_functions))
The first 4 are synchronous and the last is asynchronous.
2.1. Blocking I/O
The application calls an IO function that causes the application to block and wait for the data to be ready. If the data is not ready, wait .... The data is ready to be copied from the kernel to the user space, and the IO function returns a success indicator.
blocking I/O model diagrams: when calling the Recv ()/recvfrom () function, the process of waiting for data and replicating data in the kernel occurs.
When the recv () function is called, the system first checks to see if there is ready data. If the data is not ready, then the system is in a wait state. When the data is ready, the data is copied from the system buffer to the user space, and then the function returns. In a socket application, when the recv () function is called, the data is not necessarily present in the user space, then the recv () function is in a wait state.
2.2. Non-blocking I/O
Non-blocking IO calls the IO function repeatedly through the process (multiple system calls and returns immediately); During the data copy process, the process is blocked
We set a socket interface to non-blocking to tell the kernel that when the requested I/O operation cannot be completed, do not sleep the process, but return an error. This way our I/O operations function will constantly test whether the data is ready, and if not, continue testing until the data is ready. In this continuous testing process, the CPU will be a lot of time.
2.3. IO multiplexing
mainly select and Epoll; for an IO port, two calls, two returns, has no advantage over blocking IO; The key is to enable monitoring of multiple IO ports simultaneously, and the I/O multiplexing model uses the Select, poll, Epoll functions, which also block the process , but unlike blocking I/O, these two functions can block multiple I/O operations at the same time. I/O functions can be detected at the same time for multiple read operations, multiple write operations, and I/O operation functions are not actually invoked until there is data readable or writable.
2.4. Signal-driven IO
First we allow the socket interface to drive the signal-driven I/O and install a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data.
2.4. Asynchronous IO
When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller of the input-output operation by state, notification, and callback after completion
Finally, we summarize and compare the following five kinds of IO models:
Reference Link: http://www.360doc.com/content/16/0425/12/9075092_553624380.shtml Here are a variety of models and advantages and disadvantages
Brief introduction of Linux synchronous and asynchronous, blocking and non-blocking concepts and five IO models