This article is excerpted from “Java interview advancement means north build personal technical competitiveness”
A popular question to ask in an interview because it gives the interviewer a sense of your operating system.
IO model this piece is really difficult to understand, need too much computer knowledge. It has taken me a long time to write this article, and I wish I could tell you what I know. Hope friends can have the goods! In order to write this article, I also read the book UNIX Network Programming. Heartache ~
Personal ability is limited. If there is anything that needs to be added/improved/modified, please feel free to point it out in the comments section and make progress together!
preface
I/O has always been a difficult knowledge for many friends to understand. In this article, I will explain my understanding of I/O to you, hoping that it can be helpful to you.
I/O
What is the I/O?
I/O (Input/Outpu) is the Input and output.
Let’s take a look at I/O in terms of computer architecture.
According to the von Neumann structure, the computer structure is divided into five parts: arithmetic unit, controller, memory, input device and output device.
Input devices (such as keyboards) and output devices (such as displays) are external devices. Network cards and hard disks can be input devices or output devices.
The input device inputs data to the computer, and the output device receives data output by the computer.
From the perspective of computer architecture, I/O describes the process of communication between a computer system and external devices.
Let’s take another look at I/O from an application perspective.
In order to ensure the stability and security of the operating system, the address space of a process is divided into User space and Kernel space.
Applications like ours run in user space, and only kernel space can perform system-level resource-related operations such as file management, process communication, memory management, and so on. In other words, if we want to do IO operations, we must depend on the capacity of the kernel space.
Also, programs in user space cannot access kernel space directly.
When you want to perform I/O operations, you can only invoke the system to request the operating system for help because you do not have the permission to perform these operations.
Therefore, if a user process wants to perform IO operations, it must access the kernel space indirectly through a system call
Disk IO (reading and writing files) and network IO (network requests and responses) are the ones we encounter most during normal development.
From an application’s perspective, our application makes IO calls (system calls) to the operating system’s kernel, and the operating system’s responsible kernel performs specific IO operations. In other words, our application actually only invokes IO operations, which are performed by the operating system kernel.
When an application makes an I/O call, it goes through two steps:
- The kernel waits for the I/O device to prepare data
- The kernel copies data from kernel space to user space.
What are the common IO models?
On UNIX, there are five I/O models: synchronous blocking I/O, synchronous non-blocking I/O, I/O multiplexing, signal-driven I/O, and asynchronous I/O.
These are the five IO models we often refer to.
There are three common IO models in Java
BIO (Blocking I/O)
BIO belongs to the synchronous blocking IO model.
In the synchronous blocking IO model, after an application makes a read call, it blocks until data is copied to user space in the kernel.
This is fine if the number of client connections is not high. However, when faced with hundreds of thousands or even millions of connections, the traditional BIO model is powerless. Therefore, we need a more efficient I/O processing model to handle the 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 channels, selectors, and buffers. N in NIO can be interpreted as non-blocking, not just New. It supports buffer-oriented, channel-based approaches to I/O operations. For high-load, high-concurrency (network) applications, use NIO.
NIO in Java can be thought of as the I/O multiplexing model. NIO in Java is also widely considered a synchronous non-blocking IO model.
Follow my train of thought to look down, believe you will get the answer!
Let’s first look at the synchronous non-blocking IO model.
In the synchronous non-blocking IO model, applications make read calls and wait for data to be copied from kernel space to user space, while threads remain blocked until data is copied from kernel space to user space.
The synchronous non-blocking IO model is an improvement over the synchronous blocking IO model. Constant blocking is avoided through polling operations.
However, this IO model is also problematic: the application’s constant I/O system calls polling to see if the data is ready can be CPU intensive.
This is where the I/O multiplexing model comes in.
In the IO multiplexing model, the thread first makes a SELECT call to ask the kernel if the data is ready, and when the data is ready, the user thread makes a read call. The procedure for reading calls (data from kernel space -> user space) is still blocked.
Currently support IO multiplexing system call, select, epoll and so on. The SELECT system call is currently supported on almost all operating systems
- Select call: System call provided by the kernel to query the available status of multiple system calls at once. Almost all operating systems support it.
- Epoll call: The Linux 2.6 kernel is an enhanced version of the SELECT call, which optimizes IO execution efficiency.
IO multiplexing model reduces CPU resource consumption by reducing invalid system calls.
NIO in Java has a very important concept of a Selector, which can also be called a multiplexer. It allows you to manage multiple client connections with only one thread. When the client data arrives, it will be served.
AIO (Asynchronous I/O)
AIO is NIO 2. NIO 2, an improved version of NIO introduced in Java 7, is the asynchronous IO model.
Asynchronous IO is implemented based on events and callbacks, meaning that an action is applied and returned, not blocked, and when the background processing is complete, the operating system notifies the appropriate thread to proceed with the subsequent action.
At present, AIO is not widely used. Netty tried AIO before and abandoned it. This is because Netty’s performance on Linux has not improved much since AIO was adopted.
Finally, a diagram briefly summarizes BIO, NIO, and AIO in Java.
reference
- Tomcat & Jetty In Depth
- How to complete an IO: llC687. Top/Post/How to complete an IO…
- Programmers should understand IO:www.jianshu.com/p/fa7bdc4f3…
- Ten minutes read Java NIO underlying principle: www.cnblogs.com/crazymakerc…
- Know how much IO model | theory article: www.cnblogs.com/sheng-jie/p…
- UNIX Network Programming Volume 1; Socket Networking API section 6.2 IO model
recommended
JavaGuide Interview Shock Edition PDF version. Illustrated Computer basics PDF version.