Java NIO1:I/O Model Overview

Source: Internet
Author: User
Tags epoll signal handler

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 interpretation of I/O, let's look at the differences between synchronous and asynchronous, blocking, and non-blocking concepts, the main two of which are different in the focus point.

1. Synchronous and asynchronous

The concept of synchronous and asynchronous is wide, not only in I/O, but also with synchronous call/asynchronous invocation, synchronous request/asynchronous request. Synchronous and asynchronous, the focus is on the message communication mechanism .

The so-called synchronization is that when a "call request" is issued, the "call request" does not return until the result is obtained , but the return value is obtained once the call returns. In other words, it is the result of "caller" actively waiting for "call". As we write, method A calls the Math.random () method, method B calls the String.substring () method all synchronous calls because the caller actively waits for the return of these methods.

The so-called asynchronous, is the opposite,"call" after the issue, the call returned directly, all did not return the results . In other words, when an asynchronous call request is made, the caller does not get the result immediately, so the asynchronous call is appropriate for scenarios where data consistency requirements are not high , such as that module a updates a value in the cache. Module B shares something with Sina Weibo, which focuses more on " doing this " than " doing it right away ", in a distributed way, is to sacrifice the strong consistency of the system to improve the availability of the whole system and partition fault tolerance. If in this scenario, we want to get the result of an asynchronous call, the callee can notify the caller via status, notification, or call through a callback function that corresponds to Future/futuretask, wait/notify in Java.

2, blocking and non-blocking

Blocking and non-blocking concerns the state of the program as it waits for the result to be called .

A blocking call means that the current thread is suspended until the result of the call is returned, and the calling thread returns only after the result has been obtained.

A non-blocking call refers to a call that does not block the current thread until the result is immediately available.

Linux network I/O model

Since the vast majority of Java applications are deployed on Linux systems, here's a talk about the Linux network I/O model.

The Linux kernel sees all external devices as a single file, and reading and writing to a file invokes the system commands provided by the kernel, returning a file descriptor (FD, filename descriptor). and read and write to a socket will also have a corresponding descriptor, called SOCKETFD (Socket descriptor), the descriptor is a number, it points to a struct in the kernel (struct, C + + data type, similar to Java class, store a variety of different types of data, This stores some properties such as file path, data area, and so on.

Based on UNIX network programming for the classification of I/O models, UNIX provides 5 I/O models, respectively:

1. Blocking I/O model

The blocking I/O model is the most commonly used I/O model, and by default all file operations are blocked, using the socket to explain the model: call recvfrom in user space, Its system call is not returned until the packet arrives and is copied to the buffer of the application process or an error occurs, and during this time it waits until the process is blocked from the start of the call to Recvfrom and the entire period it returns, so it is called blocking I/O.

2. Non-blocking I/O model

recvfrom from the user space to the kernel space, if the buffer does not have data, it will return a ewouldbock error , generally the non-blocking I/O model polling Check this state, see if the kernel space has data to come, When data arrives, the data is copied from the kernel space to the user space.

3. I/O multiplexing model

Linux provides Select/poll, a process that blocks a select operation by passing one or more FD to a Select or poll system call, so that Select/poll can help us detect if multiple FD is in the ready state. Select/poll is a sequential scan of FD readiness, and the number of supported FD is limited, so its use is subject to some constraints. Linux also provides a epoll system call, and Epoll uses an event-driven alternative to sequential scanning, so performance is higher. When FD is ready, the function rollback is immediately dropped.

4. Signal-driven I/O model

First, the socket signal driver I/O function is turned on, and a signal handler function is executed via the system call Sigaction (this system call returns immediately, the process continues to work, it is non-blocking). When the data is ready, a sigio signal is generated for the process, which notifies the application to call Recvfrom to read the data and notifies the main loop function to process the data.

5. Asynchronous I/O

Tells the kernel to initiate an action and let the kernel notify the developer when the entire operation is complete, including copying data from the kernel to the user's own buffer area.

Java NIO1:I/O Model Overview

Related Article

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.