Four common IO models of Netty

Four common IO models :

Synchronous blocking BIO : The simplest IO model, user threads are blocked when reading and writing.

Data copy means that the requested data is first stored in the kernel space, and then copied from the kernel space to the program's buffer.

img

The user thread is blocked in the IO process, cannot do anything, and the resource utilization of the CPU is not high.

Synchronous non-blocking NIO : The user thread continuously initiates IO requests. The system returns a status value when the data does not arrive; the data is actually read only after the data arrives.

img

The user thread can return immediately every time it requests IO, but in order to get the data, it needs to poll continuously, which consumes a lot of money.

The amount of CPU. Generally, this model is rarely used directly, but the feature of non-blocking IO is used in other IO models

IO multiplexing : IO multiplexing is built on the blocking function select provided by the kernel. The user first adds the sockets that need to perform IO operations to select, and then waits for the blocking function select to return. When the data arrives, the socket is activated, select returns, and the user thread can then initiate a read request.

img

In fact, we can register multiple sockets for select, and then continuously call select to read the activated sockets to achieve the effect of processing multiple IO requests at the same time in the same thread. Further, we extract the select polling and put it in one In the thread, the user thread registers the relevant socket or IO request with it, and notifies the user thread when the data arrives, which can improve the CPU utilization of the user thread. In this way, the asynchronous way of reading and writing user data is realized.

1

IO multiplexing is the most commonly used IO model, because the thread polling select will be blocked, and the degree of asynchrony is not "complete
" enough, so it is often called asynchronous blocking IO

Asynchronous AIO : True asynchronous IO requires stronger support from the operating system. In the IO multiplexing model, the user thread is notified after the data reaches the kernel, and the user thread is responsible for copying the data from the kernel space; while in the asynchronous IO model, when the user thread receives the notification, the data has been copied by the operating system from the kernel to the user. In the specified buffer, the user thread can use it directly.

img

Compared with IO multiplexing, asynchronous IO is not commonly used, because the current operating system's support for asynchronous IO is not perfect, and IO multiplexing is basically sufficient. There are many ways to simulate the IO multiplexing model. Asynchronous IO (when an IO event is triggered, the user thread is not directly notified, but the data is read and written into the buffer specified by the user).

Guess you like

Origin blog.csdn.net/a1774381324/article/details/120796494