Five IO models of UNIX

Time:2020-5-26

preface

I plan to summarize Java’sBIO(IO),AIO,NIO, the last step is to find out that the IO model of UNIX (Linux) needs to be mastered in advance, so summarize the IO model of UNIX first.

summary

Relationship between Java IO and UNIX IO (non strict correspondence)

Five IO models are introduced in unix network programming: blocking IO, non blocking IO, IO multiplexing, signal driven IO and asynchronous io.
The correspondence between Java IO model and UNIX IO model is as follows (this correspondence is not strict):

Java IO model UNIX IO model
BIO Blocking IO
NIO IO multiplexing
AIO Asynchronous IO

Kernel state and user state of data

Five IO models of UNIX
Before I explain the IO model, I will talk about two states of data: kernel state and user state.
We load the file from disk into memory. What does the operating system do?
Step 1: because all our programs interact with the kernel of the operating system, the file is first loaded from the disk to the kernel, and then the file is in kernel state.
Step 2: the file is reloaded from the kernel to memory, and then the application program can read and write in memory. At this time, the file is in kernel mode.
So the difference of the five IO models of UNIX points out that the processing flow of these two steps is different.

Five IO models of UNIX are introduced respectively

Blocking IO

Five IO models of UNIX
This mode is very simple. The system callsrecvfromAfter the function, the thread waits until:
The first step is to load the file into kernel mode, and the second step is to load the file into user mode.

non-blocking io

Five IO models of UNIX
The system keeps passingrecvfromPoll until the first step is completed, and then block the data from kernel state to user state in the second step.
The non blocking IO mode here mainly refers to the first step, loading data to the kernel state. This process is non blocking, and polling is used to determine whether the data is ready in the kernel.

IO multiplexing

Five IO models of UNIX
The system first checks whether the kernel data is ready through select.
When the kernel data is loaded, the system callsrecvfromTo load kernel state data into user state.
It looks like the first and second steps are blocking operations, but select can handle multiple file handles (including sockets) at the same time at a very low cost.

Signal driven IO

Five IO models of UNIX
The first step is to register a callback function and notify me when the kernel data is ready.
At this time, the system can do other things without blocking waiting for kernel data.
Step 2, blocking callrecvfrom, load the kernel’s data into the user state.

Asynchronous IO

Five IO models of UNIX
In theory, this model is a real asynchronous model, because in the above four models, in the second step: data loading from kernel state to user state is synchronous operation.
In this model, when the system loads the file, it only needs to pass AIO_ Read registers a callback to notify the current system when the file is loaded in kernel state or user state.
In this process, the system can perform other operation tasks without waiting.

Summary and comparison

Five IO models of UNIX
This figure is a summary and comparison of the above five IO models. Generally speaking, the later the model is, the more efficient in theory.
The first four IO models [blocking IO, non blocking IO, IO multiplexing, signal driven IO] are all synchronous IO, only the last one is truly asynchronous [asynchronous IO]

Introduction to system call

1. The old version of NiO in java used select mode, but later changed to epoll. Why?

Because select is a polling mode, it constantly checks the status of the file handle.
Epoll is a callback mode. When the file handle is ready, it can directly callback, which is more efficient.

2. Does java have a real AIO mode?

Under Windows system, it is realized by IOCP.
In Linux system, no, because the underlying AIO layer of Linux system is still epoll.
(I guess that’s why netty uses NiO instead of AIO)

outside the box

In the form of dialogue, it is easy to understand
Ramble: how to explain to girlfriend what are the five IO models of Linux?

The kernel state and user state of data are also mentioned in this article. At the same time, efficient data transmission methods are introduced:
zero-copy