Java NIO1:I/O Model Overview

Source: Internet
Author: User

I/O model

Before starting the study of NIO, there is an understanding of the I/O model, which is absolutely beneficial to the study of NIO. I draw a diagram that simply represents the process of moving data from an external disk to the memory area of a running process:

This picture clearly ignores a lot of details, only involves the basic operation, the following analysis of this picture.

User space and kernel space

A computer usually has a certain size of memory space, such as a computer has a 4GB address space, but the program does not fully use these address space, because these address space is divided into user space and kernel space. The program can only use the memory of the user space , the use of this refers to the program can request the memory space, not really access to the address space. Let's look at what the user space and kernel space are:

1. User space

The user space is the area where the regular process is located, what is a regular process, and what the task manager sees is the general process:

The JVM is a regular process, stationed in the user space, the user space is a non-privileged area, such as the code executed in the region does not directly access the hardware device.

2. Kernel space

Kernel space mainly refers to the program logic used by the operating system for program scheduling, virtual memory usage, or connection of hardware resources. The kernel code has special rights, such as its ability to communicate with the device controller and control the entire running state for the zone process. And I/O is related to the fact that all I/O is directly or indirectly through kernel space .

So, why divide user space and kernel space? This is also to ensure the stability and security of the operating system. The user program can not directly access the hardware resources, if the user program needs to access hardware resources, must invoke the interface provided by the operating system, the process of calling the interface is called the system. Each system call will have two memory space between the mutual switch, the usual network transmission is also a system call, the data transmitted over the network first received from the kernel space to the remote host data, and then from the kernel space copied to the user space for use by the user program. This data replication from the kernel space to the user control is time consuming, while preserving the security and stability of the program, sacrificing some of its efficiency.

Finally, how to allocate the ratio of user space and kernel space is also a problem, is to allocate more to the user space for the user program to use, or first to keep the kernel has enough space to run, or to balance. In the current Windows 32-bit operating system, the default user space: The ratio of kernel space is 1:1, while the default ratio in 32-bit Linux systems is 3:1 (3GB user space, 1GB kernel space).

Steps for the process to perform I/O operations

buffers, and how buffers work, are the basis for all I/O. The so-called "input/output" is all about moving data into or out of the buffer.

The process performs an I/O operation, which boils down to making a request to the operating system to either clean up the data in the buffer (write) or fill the buffer with data (read). Process uses this mechanism to handle all data in and out operations, the operating system internal processing of this task mechanism, its complexity may be beyond imagination, but in terms of concept, but very straightforward, from the above diagram, you can summarize the process to perform I/O operations of several steps:

1. The process uses the underlying function read () to establish and execute an appropriate system call, requiring its buffer to be filled, when control is handed over to the kernel

2. The kernel then issues a command to the disk control hardware requiring it to read data from disk

3. The disk controller and data are written directly to the kernel memory buffer, this step is done through DMA, without the need for primary CPU assistance. Here more, about DMA, can Baidu, it is an important feature of modern computers, it allows different speed of the hardware devices to communicate, without the need to rely on the CPU of a large number of interrupt load, greatly improve the efficiency of the entire system

4. A disk controller fills the buffer, and the kernel copies the data from the temporary buffer in the kernel space to the buffer specified by the process when the read () call is executed

5. Process gets data from buffer in user space

Of course, if the data is already in the kernel space, the data needs to be simply copied. Why not just let the disk controller send the data to the buffer of user space? One of the simplest reasons is that hardware typically does not have direct access to user space.

Synchronous and asynchronous, blocking, and non-blocking

With the above understanding of I/O, we can understand the difference between synchronous and asynchronous.

Synchronous and asynchronous concerns are how the user thread interacts with the kernel . Synchronization refers to the need to wait after a user thread initiates an I/O request or to poll for kernel I/O operations to complete before proceeding; asynchronous refers to a user thread that continues execution after initiating an I/O request and notifies the user thread when the kernel I/O operation completes, or invokes a callback function registered by the user thread.

Blocking and non-blocking concerns are the way that user threads invoke kernel operations . Blocking means that the I/O operation needs to be completely completed before it returns to the user space, and non-blocking means that the I/O operation is returned immediately to the user with a status value, without waiting for the I/O operation to complete completely.

Synchronous blocking I/O

The synchronous blocking I/O model, the simplest I/O model, is blocked by the user thread during I/O operations of the kernel.

The synchronous blocking I/O operation is where the user thread initiates an I/O read operation through the system calling the read () function, which is transferred from the user space to the kernel space. The kernel waits until the packet arrives, and then copies the received data to the user space to complete the read () operation. The user thread needs to wait for the read () function to read the data to the user space buffer in order to continue processing the received data throughout the process. This causes the user thread to initiate an I/O request and cannot do anything with insufficient resource utilization for the CPU .

Synchronous non-blocking I/O

The synchronous nonblocking I/O model is based on the synchronous blocking I/O model, which can be returned immediately after the user thread initiates an I/O request.

Synchronous non-blocking I/O operations, as stated above, the user thread immediately after initiating an I/O request returns, but no data is read at this time, the user thread needs to constantly initiate I/O requests, until the data arrives, not really read the data, continue to execute. Throughout the process, the user needs to constantly call read () to try to read successfully before continuing processing the received data. In this way, although the user thread can return immediately after each initiating an I/O request, this does not make sense, and in order to wait for the data to continue polling, repeating requests and consuming a lot of CPU resources , this model is rarely used.

I/O multiplexing

The I/O multiplexing model is based on the multi-path separation function Select provided by the kernel , using the Select function to avoid polling waits in the synchronous nonblocking I/O model.

The I/O multiplexing operation bit, the user thread first adds the I/O operations that need to be made to the Select, and then blocks the wait for the select system call to return. When the data arrives, the I/O operation is activated, the Select function returns, the user thread formally initiates a read () request, reads the data, and continues execution.

From a process point of view, I/O requests and synchronous blocking models using the Select function are not much different, or even more, adding monitoring I/O and additional operations that call the SELECT function are less efficient. However, the biggest advantage of using the Select function is that you can handle multiple I/O requests simultaneously in one line range. The user can register multiple I/O, and then constantly call Select to read the activated I/O, to achieve the same line range simultaneous processing of multiple I/O requests . In the synchronous blocking model, the user must be multi-threaded to reach this destination.

However, the advantages of using the Select function are not limited to this. Although the above approach allows multiple I/O requests to be processed within a single thread, the process of each I/O request is still blocked (blocking on the Select function), and the average time is even longer than the synchronous blocking I/O model. If the user thread registers only the I/O requests that it is interested in, and then does its own thing and waits until the data arrives, it can increase the CPU utilization.

The I/O multiplexing model is the most commonly used I/O model, but it is not asynchronous enough because it uses a select system call that blocks threads. Therefore, the I/O multiplexing model can only be called asynchronous blocking I/O, not true asynchronous I/O.

Java NIO1:I/O Model Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.