Several models of IO

1. Introduction to I/O model terms

When it comes to the I/O model, the words synchronous, asynchronous, blocking, and non-blocking will be involved. The concepts of these words are explained below.

Blocking and non-blocking

Blocking and non-blocking refer to waiting or doing other things.

Blocking (waiting for the water to boil) (blocking): Before the call result is returned, the caller is suspended (the current thread enters a non-executable state, in this state, the CPU will not allocate time slices, and the thread suspends running);

Non-blocking (watching TV, checking whether the water is boiling from time to time) (nonblocking): The caller will not be suspended before the call result is returned (the current thread is still executable, the CPU will allocate a time slice, and the thread can still run)

Synchronous and asynchronous

Synchronization/asynchronous focus on message communication mechanism

Synchronous (a kettle that won't ring) (synchronous): After the callee completes the task, it will not actively return the call result to the caller.

Asynchronous (a kettle that will ring) (asynchronous): After the callee completes the task, it will actively return the call result to the caller.

                  

       Blocking, non-blocking, and multi-channel IO multiplexing are all synchronous IO, and asynchronous must be non-blocking, so there is no such thing as asynchronous blocking and asynchronous non-blocking. True asynchronous IO requires deep involvement of the CPU. In other words, only when the user thread does not consider the execution of IO at all when operating IO and leaves all IO execution to the CPU, and only waits for a completion signal, is true asynchronous IO. Therefore, pulling a child thread to poll, go to an infinite loop, or use select, poll, and epool are not asynchronous.

2. I/O model type

IO models are divided into the following five categories

  1. Blocking I/O: All processes are blocked
  2. Non-blocking I/O: If there is no data buffer, return EWOULDBLOCK immediately
  3. I/O multiplexing type (select and poll): block in the wait and copy phases respectively
  4. Signal-driven I/O (SIGIO): not blocked in the wait phase, but blocked in the copy phase (signal-driven I/O), that is, notification
  5. Asynchronous I/O (AIO): Completely non-blocking, providing a signal when I/O is complete

1. Blocking I/O

Note: The application calls an IO recvfrom function, which will cause the application to block. After entering the blocking state, it will not return until the I/O operation is completed; if the system kernel data is not ready, it will keep waiting for the data to be prepared because it is called The recvfrom function causes the application to block, so it keeps waiting and cannot do anything. After the kernel data is prepared, the data is copied from the kernel to the user space. After the copy is completed, the I/O function returns a success indication. Note: It blocks during the I/O operation phase.

2. Non-blocking I/O

Description: Return immediately when the user thread initiates an IO request. However, if no data is read, the return field is "EWOULDBLOCK". The user thread needs to continuously initiate IO requests until the data arrives before the data is actually read and execution continues. That is the "polling" mechanism. During the entire IO request process, although the user thread can return immediately after each IO request is initiated, in order to wait for the data. It still requires constant polling, repeated requests, and consumes a lot of CPU resources; it is a wasteful way of CPU. This model is generally rarely used, but the non-blocking IO feature is used in other models.

3.I/O multiplexing (select and poll)

Note: The I/O multiplexing model will use the select or poll function. In the I/O multiplexing model, it is not blocked in the I/O operation process, but blocked in the select or poll function; take select as an example : The process blocks at select, waiting for one of several descriptors to become operable. If it does not wait, it will continue to block in the first stage. If it waits until a descriptor becomes operable, the recvfrom function will be called to copy the data. to the application buffer.

4. Signal driven I/O (SIGIO)

Description: First, we allow the socket to perform signal-driven I/O and install a signal processing function SIGIO. If the data is not ready, the result will be returned immediately and the process will continue to work without blocking. When the data is ready, the system kernel will actively send a SIGIO signal to the application. After the application receives the signal, it can call the I/O operation function recvfrom in the signal processing function for data processing. The advantage of the signal-driven I/O model is that when the datagram arrives, it does not block, and the main loop can continue executing, just waiting for notification from the handler, or the data is ready to be processed, or the datagram is ready to be read.

5.Alternative I/O (AIO)

Description: When an asynchronous procedure call is issued, the caller cannot get the result immediately. After the component that actually handles this call is completed, it informs the caller of the input and output operations through status notifications and callback notifications. Users can directly perform read and write operations on I/O. These operations tell the kernel the location of the user's read and write buffer, and the way the kernel notifies the application after the I/O operation is completed, which is through status notification or callback notification mentioned above. caller. The read and write operations of asynchronous I/O always return immediately, but there is no return result indicating whether it is blocked, because the real read and write operations of the asynchronous I/O operation have been taken over by the kernel. After the kernel completes the data processing, it generates a signal and then notifies The event that the user just handed over to himself has been processed.

Summary and comparison of five I/O models

The Chinese icon is as follows:

The English icon is as follows:

From the two pictures, we can see that the further back, the less blocking, and theoretically the efficiency is optimal. Among the five I/O models, the first three are synchronous I/O, and the latter two are asynchronous I/O.

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/132352927