ReferenceRichard Stevens's"UNIX Network Programming Volume 1, third edition: The sockets networking".
Steven s proposed a total of five Io models:
- Blocking Io
- Nonblocking Io
- Io multiplexing (select and poll)
- Signal driven io (sigio)
- Asynchronous io (the POSIX aio_functions)
First, let's talk about the objects and steps involved when I/O occurs.
An input operation usually involves the following two phases:
1. Wait for the data to be ready ). Input operations on a set of interfaces usually involve waiting for data to arrive from the network and then being copied to a buffer in the kernel.
2. Copy data from the kernel cache to the process buffer (copying the data from the kernel to the process)
Remember that these two phases are important, because the differences between the five Io models discussed below are different in the two phases.
Blocking I/O model (blocking I/O)
By default, all interfaces are blocking.
The process calls recvfrom, and the system calls until the datagram arrives (the first stage) and is copied to the buffer of the application process (the second stage) or an error occurs (the most common error is that the system call is interrupted by a signal. A process is blocked from calling recvfrom to returning it. After successful return of recvfrom, the application process starts to process the datagram.
Nonblocking I/O model (non-blocking I/O)
The data is not ready for the first three times of calling recvfrom. This is an ewouldblock error immediately returned by the kernel. The data is ready when recvfrom is called for the fourth time. It is copied to the application process buffer. recvfrom is returned successfully, and then the application process starts to process the datagram.
The most important operation here isPolling). The application process continuously polls the kernel to check whether the data is ready. This usually takes a lot of CPU time. This model is usually available in systems that provide specific functions.
I/O multiplexing model (I/O multiplexing Model)
When a user process calls the SELECT statement, the entire process will be blocked. At the same time, the kernel will "Monitor" All sockets under the SELECT statement. When the data in any socket is ready, select will return the socket-readable condition. We call recvfrom to copy the read datagram to the application.ProgramProcess buffer.
Compared with the blocking Io diagram, I/O Reuse does not show any advantages. In fact, it may be slightly inferior. Because two system calls (select and recvfrom) are required, and blocking Io calls only one system call. However,The advantage of using select is that it can process multiple connections at the same time.
Signal-driven I/O model (signal-driven I/O model)
We first enable the signal-driven I/O function of the set of interfaces, and install a signal processing function through the sigaction system call. The system call will return immediately. Our process is not blocked, but continues to be executed. When the datagram is ready for reading, the kernel generates a sigio signal for the process. Then we can call recvfrom in the signal processing function to read the datagram, notify the main loop that the data is ready for processing, or immediately notify the main loop to read the datagram. Regardless of how the sigio signal is processed, the advantage of this model is that the process can continue to run while waiting for the datagram to arrive (the first stage) without being blocked.
Asynchronous I/O model (asynchronous I/O model)
After the process initiates the read operation, it can immediately start to do other things. On the other hand, from the perspective of kernel, when it receives an asynchronous read, it will first return immediately, so it will not generate any block to the user process. Then, the kernel will wait for the data preparation to complete and then copy the data to the user memory. After all this is done, the kernel will send a signal to the user process to tell it that the read operation is complete.
The working mechanism of this model is to inform the kernel to start an operation and notify us after the kernel completes the entire operation (including the second stage, that is, copying data from the kernel to the process buffer.
This model differs from the previous model in that the signal-driven I/O is used by the kernel to notify us when an I/O operation can be started, the asynchronous I/O model informs us when I/O operations are completed by the kernel.
The introduction of the five I/O models is complete.What is the difference between blocking and non-blocking? What is the difference between synchronous Io and asynchronous io.
Blocking I/ovs non-blocking I/O: Calling blocking Io will block the corresponding process until the operation is completed, while non-blocking Io will return immediately when the kernel still prepares data.
Synchronous I/O vs asynchronous I/O:
Let's take a look at these two definitions:
A synchronous I/O operation causes the requesting process to be blocked until thatI/O operationCompletes;
An asynchronous I/O operation does not cause the requesting process to be blocked;
The difference between the two is that synchronous Io blocks process when performing "Io operation. According to this definition, the first four models, blocking I/O, non-blocking I/O, Io multiplexing, and signal driven Io, all belong to synchronous Io. Some may say that non-blocking Io is not blocked. Here is a very "Tricky" place. The "Io operation" in the definition refers to the real Io operation, that is, the recvfrom system call in the example. When non-blocking Io executes the recvfrom system call, if the kernel data is not ready, the process will not be blocked. However, when the data in the kernel is ready, recvfrom will copy the data from the kernel to the user memory. At this time, the (Phase 2) process is blocked. During this time, the process is blocked. Asynchronous Io is different. When a process initiates an I/O operation, it directly returns the result and ignores it again until the kernel sends a signal telling the process that I/O is complete. In this process, the process is not blocked at all.
Comparison of Io models:
The main difference between the first four models is the first stage, because they are the same in the second stage: Process blocking and recvfrom system calls during data copying from the kernel buffer to the process buffer.