Detailed IO model (easy to understand)

insert image description here

What is I/O?

I/O (Input/Output) means input/output.

Let's first interpret I/O from the perspective of computer structure .

According to the von Neumann structure, the computer structure is divided into five parts: arithmetic unit, controller, memory, input device, and output device.
insert image description here
Input devices (such as keyboards) and output devices (such as monitors) are external devices. Network cards and hard disks can belong to both input devices and output devices.

Input devices input data to the computer, and output devices receive data output by the computer.

From a computer architecture perspective, I/O describes the process of communicating between a computer system and external devices.

Let's first interpret I/O from the application point of view.

According to the operating system-related knowledge learned in the university: In order to ensure the stability and security of the operating system, the address space of a process is divided into user space (User space) and kernel space (Kernel space).

The applications we usually run run in the user space, and only the kernel space can perform system-level resource-related operations, such as file management, process communication, memory management, and so on. That is to say, if we want to perform IO operations, we must rely on the capabilities of the kernel space.

Also, programs in user space cannot directly access kernel space.

When you want to perform IO operations, because there is no permission to perform these operations, you can only initiate a system call to request the operating system to help.

Therefore, if the user process wants to perform IO operations, it must indirectly access the kernel space through system calls

We are most exposed to disk IO (read and write files) and network IO (network requests and responses) in the usual development process.

From the application point of view, our application initiates IO calls (system calls) to the kernel of the operating system, and the kernel responsible for the operating system performs specific IO operations. That is to say, our application actually just initiates the call of the IO operation, and the execution of the specific IO is completed by the kernel of the operating system.

When an application makes an I/O call, it goes through two steps:

  1. The kernel waits for the I/O device to be ready for data
  2. The kernel copies data from kernel space to user space.

What are the common IO models?

Under the UNIX system, there are five IO models: synchronous blocking I/O, synchronous non-blocking I/O, I/O multiplexing, signal-driven I/O and asynchronous I/O.

This is also the 5 IO models we often mention.

3 common IO models in Java

BIO (Blocking I/O)

BIO belongs to the synchronous blocking IO model.

In the synchronous blocking IO model, after the application initiates a read call, it will block until the kernel copies the data to the user space.
insert image description here
In the case where the number of client connections is not high, there is no problem. However, when faced with 100,000 or even millions of connections, the traditional BIO model is powerless. Therefore, we need a more efficient I/O processing model to cope with higher concurrency.

NIO (Non-blocking/New I/O)

NIO in Java was introduced in Java 1.4, corresponding to the java.nio package, which provides abstractions such as Channel, Selector, and Buffer. N in NIO can be understood as Non-blocking, not simply New. It supports buffer-oriented, channel-based I/O operations. For high-load, high-concurrency (network) applications, NIO should be used.

NIO in Java can be seen as an I/O multiplexing model. There are also many people who believe that NIO in Java belongs to the synchronous non-blocking IO model.

Follow my ideas and look down, I believe you will get the answer!

Let's first look at the synchronous non-blocking IO model.
insert image description here

In the synchronous non-blocking IO model, the application will always initiate read calls and wait for the data to be copied from the kernel space to the user space. The thread is still blocked until the data is copied to the user space in the kernel.

Compared with the synchronous blocking IO model, the synchronous non-blocking IO model is indeed a great improvement. By polling the operation, it avoids blocking all the time.

However, this IO model also has problems: the process of the application constantly polling whether the data is ready by the I/O system call is very CPU-intensive.

At this point, the I/O multiplexing model comes into play.
insert image description here

In the IO multiplexing model, the thread first initiates a select call to ask the kernel whether the data is ready, and when the kernel prepares the data, the user thread initiates a read call. The process of the read call (data from kernel space -> user space) is still blocked.

目前支持 IO 多路复用的系统调用,有 select,epoll 等等。select 系统
调用,目前几乎在所有的操作系统上都有支持。
 select 调用 :内核提供的系统调用,它支持一次查询多个系统调用的可
 用状态。几乎所有的操作系统都支持。
  epoll 调用 :linux 2.6 内核,属于 select 调用的增强版本,优化了
   IO 的执行效率。

The IO multiplexing model reduces the consumption of CPU resources by reducing invalid system calls.

NIO in Java has a very important concept of selector (Selector), which can also be called multiplexer. With it, only one thread is needed to manage multiple client connections. When the client data arrives, it will be served.
insert image description here

AIO (Asynchronous I / O)

AIO is also NIO 2. An improved version of NIO, NIO 2, was introduced in Java 7, which is the asynchronous IO model.

Asynchronous IO is implemented based on the event and callback mechanism, that is, the application will return directly after the operation, and will not be blocked there. When the background processing is completed, the operating system will notify the corresponding thread to perform subsequent operations.
insert image description here
At present, the application of AIO is not very extensive. Netty tried AIO before, but gave up. This is because after Netty uses AIO, the performance on Linux systems does not improve much.

Finally, let's take a picture to briefly summarize BIO, NIO, and AIO in Java.
insert image description here

Welcome to add

Guess you like

Origin blog.csdn.net/JIAYOUYAA/article/details/124456897