Article directory:
1. What is IO
2. What is Java NIO
3.I/O Common Concepts
4. Why Use NiO
5.IO VS NIO
First, what is IO
I/O or input/output refers to the interface between the computer and the outside world or between a program and the rest of the computer. It is critical for any computer system, so the body of all I/O is actually built into the operating system. Separate programs generally allow the system to do most of the work for them. In Java programming, I/O has been completed until recently using a stream . All I/O is treated as a single byte movement, moving one byte at a time through an object called Stream. Flow I/O is used to contact the outside world. It is also used internally to convert an object to a byte and then back to the object. We're accustomed to talking about interfaces that refer to I/O interfaces.
Ii. What is Java NIO
NIO is the abbreviation for new I/O, and belongs to the jdk1.4 provided by the current API. Now that the JDK version is 1.8, the new IO is a bit inappropriate, and NIO has a more appropriate term-non-blocking (non-blocking) Io.
Iii. Common concepts of I/O
3.1 DMA
DMA (Direct memory access) is an important feature of all modern computers, and the CPU is idle when transferring data between the DMA controller and memory.
In the process of transmitting data in DMA, the source physical address and destination physical address are required to be contiguous. However, in some computer systems, such as IA, the continuous memory address is not necessarily continuous, then the DMA transfer is divided into several times to complete. If a single interrupt is initiated after a piece of physical continuous data is transmitted, and the host makes the next physical continuous transfer, this is the Block DMA mode. The Scatter/gather method is different, it uses a list to describe the physical discontinuous memory, and then the first address of the list to the DMA master. After the DMA master transmits a piece of physical continuous data, it does not have to interrupt again, but instead transmits the next piece of physical continuous data according to the linked list, and finally initiates an interrupt.
It is clear that the Scatter/gather method is more efficient than the Block DMA mode.
3.2 Kernel space and user space
User space is the region where the regular process is located. The JVM is a regular process that is stationed in user space. User space is a non-privileged zone: for example, code executed in the zone cannot directly access the hardware device. Kernel space is the region in which the operating system resides. The kernel code has special powers: it communicates with the device controller, controls the running state of the user zone process, and so on. Most importantly, all I/O is directly or indirectly through the nuclear space.
When a process requests an I/O operation, it executes a system call that transfers control to the kernel. When the kernel is called in this way, it takes any necessary steps to find the data required by the process and transmits the data to the specified buffer within the user space. The kernel tries to cache or pre-read the data, so the data required for the process may already be in the kernel space. If so, the data simply needs to be copied. If the data is not in kernel space, the process is suspended and the kernel begins to read the data into memory.
Why not just let the disk controller send the data to the buffer in the user space? There are a few problems with this. First, the hardware typically does not have direct access to the user space (the hardware device typically does not use the virtual memory address directly). Second, a block-based hardware device such as a disk operates on a fixed-size block of data, and a user process may request an arbitrary-sized or unaligned block of data. In the process of data transactions between user space and storage devices, the kernel is responsible for the decomposition and re-grouping of data, thus acting as an intermediary.
3.3 Virtual Memory
Virtual memory is used by all modern operating systems. Virtual memory means using a false (or virtual) address instead of physical (hardware
RAM) memory address. This is a lot of good, summed up can be divided into two categories:
- More than one virtual address can point to the same physical memory address.
- The virtual memory space can be larger than the actual available hardware memory.
The device controller cannot be directly stored in the user space via DMA, but the same effect can be achieved by taking advantage of the first item mentioned above. Maps the kernel-space address to the virtual address of the user space to the same physical address, so that the DMA hardware (which only accesses the physical memory address) can populate the buffer that is visible to both the kernel and the user-space process. However, the prerequisite is that the kernel and user buffers must be aligned using the same page, and the buffer size must also be a multiple of the disk controller block size (typically 512-byte disk sector). The operating system divides the memory address space into pages, which are fixed-size byte groups. The size of the memory page is always a multiple of the disk block size, typically 2 powers (this simplifies addressing operations). Typical memory pages are 1,024, 2,048, and 4,096 bytes.
Paging technology of modern operating system
3.4 Paging Technology
The modern CPU contains a subsystem called the Memory Management Unit (MMU), which is logically located between the CPU and the physical memory. The device contains the mapping information required to convert the virtual address to the physical memory address. When the CPU refers to a memory address, the MMU is responsible for determining the page on which the address is located (often by shifting the address value or masking bit operations) and converting the virtual page number to a physical page number (this step is done by hardware, extremely fast). The MMU submits a page fault to the CPU if there is currently no physical memory page that forms a valid mapping to the virtual page. Page faults result in a system call that transfers control to the kernel, with the virtual address information that causes the error, and then the kernel takes steps to verify the validity of the page. The kernel arranges the paging operation and reads the missing page contents back into physical memory. This often leads to other pages being moved out of physical memory, so that the new page gets to the place. In this case, if the page to be moved has been touched (since the creation or last page call, the content has changed), you must first perform page recall and copy the page contents to the paging area on disk.
If the requested address is not a valid virtual memory address (any one of the memory segments that are not part of the executing process), the page cannot be validated and the segment error is generated. As a result, control is handed over to another part of the kernel, often resulting in the process being forced to close. Once the error page passes validation, the MMU is updated to establish a new virtual-to-physical mapping (if necessary, break the mapping of the page being moved out), and the user process continues. User processes that cause page faults will not be aware of this, and everything will be done unconsciously.
3.5 I/O and stream I/O for block (file)
File I/O belongs to the file system category, and the file system is very different from disk. The disk has data on the sector, typically a sector of 512 bytes. A disk is a hardware device that knows nothing about a file, it simply provides a series of data access windows. At this point, disk sectors are quite similar to memory pages: they are all uniform sizes and can be accessed as large arrays.
A file system is a higher level of abstraction and is a unique way of arranging and interpreting data for disks (or other random access block devices). The code you write almost invariably deals with the file system, rather than dealing directly with the disk. Is that the file system defines the abstract concepts of file names, paths, files, file attributes, and so on. The file system organizes a series of data blocks that are consistent in size. Some blocks store meta information, such as mappings for free blocks, directories, indexes, and some contain file data. The meta information for a single file describes which blocks contain the file data, where the data ends, when the last update was made, and so on.
When a user process requests to read the file data, the file system needs to determine where the data is located on the disk, and then begins to read the relevant disk sectors into memory. Older operating systems often publish commands directly to disk drives, requiring them to read the required disk sectors. The modern operating system using paging technology uses the request page scheduling to obtain the required data.
The entire process of performing I/O using paging technology can be summarized in the following steps:
? Determines which pages of the file system the requested data is distributed on (disk sector groups). The file contents and metadata on the disk may span multiple file system pages, and these pages may not be contiguous.
? Allocate a sufficient number of memory pages in the kernel space to accommodate the file system pages that are determined.
? Establishes a mapping between a memory page and a file system page on disk.
? A page fault is generated for each memory page.
? The virtual memory system captures page faults, arranges page paging, reads page contents from disk, and makes the page valid.
? Once the paging operation is complete, the file system parses the original data to get the required file content or attributes
Information.
Most operating systems assume that the process continues to read the remainder of the file, thus pre-reading the additional file system pages. If memory contention is not severe, these file system pages may continue to be valid for a considerable period of time. This way, when the file is later opened again by the same or different process, it may not be necessary to access the disk at all. You may also have encountered this situation: when performing similar operations repeatedly, such as string retrieval in several files, the second time seems to run much faster.
Similar steps are used when writing file data. At this point, changes in the contents of the file (via write ()) will cause the file system page to become dirty and then be paged out to synchronize with the file contents on the disk. The file is created by mapping the file to a free file system page, and then flushing the file system page to disk in the subsequent write operation.
Not all I/O are block-oriented, there is flow I/O, and the principle mimics the channel. I/O byte streams must be sequentially accessed, and common examples are TTY (console) devices, printer ports, and network connections.
The transmission of a stream is generally (and not necessarily) slower than a block device and is often used for intermittent input. Most operating systems allow the flow to be placed in nonblocking mode, so that the process can see if there is input on the stream, even if it did not, and it does not affect anything else. Such a capability allows the process to be processed when there is input, and other functions are performed when the input stream is idle. Further than non-blocking mode is the readiness option. The readiness selection is similar to nonblocking mode (often built on non-blocking mode), but the task of viewing the flow readiness is given to the operating system. The operating system is instructed to view a series of streams and remind the process which streams are ready. In this way, the process can use the same code and a single thread to realize the multi-channel transport of a multi-activity stream simply by virtue of the readiness information returned by the operating system. This technology is widely used in the field of Web servers to handle a large number of network connections. Readiness selection is essential in large capacity scaling.
3.6 Memory-Mapped files
Memory-mapped I/O uses a file system to establish a virtual memory map from user space until the available file system pages. This is done with a few
A benefit:
? The user process treats the file data as memory, so there is no need to publish a read () or write () system call.
? When the user process touches the mapped memory space, the page fault is automatically generated, which reads the file data from the disk into memory. If the user modifies the mapped memory space, the related page is automatically marked as dirty, then flushed to disk and the file is updated.
? The virtual memory subsystem of the operating system intelligently caches pages and automatically manages memory based on system load.
? The data is always page-aligned, eliminating the need to perform a buffer copy.
? Large files use maps that copy data without consuming large amounts of memory.
3.7 File Lock
The file locking mechanism allows a process to block other processes from accessing a file or restricting its access. The usual use is to control how the shared information is updated, or for transaction isolation. File locking is essential in controlling the concurrent access of multiple entities to common resources. Complex applications such as databases rely heavily on file locking.
"File lock" literally has the meaning of locking the entire file (which is usually the case), but the lock can often occur on a more subtle level, and the locked area can often be detailed to a single byte. A lock is associated with a specific file, starting at a specific byte address of a file, containing a specific number of contiguous bytes. This is critical for coordinating multiple processes to access different areas of the file independently of each other.
There are two ways to lock a file: Shared and exclusive. Multiple shared locks can work on the same file area at the same time, while exclusive locks are different, requiring that the relevant area cannot have other locks in effect.
Iv. why the use of NIO
The ultimate goal of I/O is efficiency, and efficiency cannot be achieved without the support of the underlying operating system and file system features. These features include: File locking, nonblocking I/O, readiness selection, and memory mapping. Most of today's operating systems support these features, while the Java legacy I/O mechanism does not emulate these common I/O services. NIO was created to enable Java programmers to implement high-speed I/O without having to write custom native code. NIO transfers the most time-consuming I/O operations (that is, the fill and fetch buffers) back to the operating system, which can greatly improve speed. Typically, a buffer operation occurs when a process requires an I/O operation, it performs a read or write system call, makes a request to the underlying operating system, and the operating system fills or dries out the data buffers as required.
For file I/O, integrated I/O provides support for the NIO feature. The part of enterprise application software that involves I/O is mostly a functional requirement for read and write files, and there are few concurrent requirements, so the Javaio package is already competent.
For network I/O, traditional blocking I/O, one thread corresponds to one connection, and the thread pool pattern is simple and efficient in most scenarios. NiO is undoubtedly a better choice when the number of connections is very high and data movement is very frequent.
Description: Content is self based on some
V. IO VS NIO
5.1 Comparison of file streams and file blocks
The most important difference between the original I/O library (in java.io.*) and NIO is the way data is packaged and transmitted. The original I/O processes the data in a stream, often in order to deal with individual bytes or characters, it is necessary to execute several method calls to the object layer. and NIO processes the data in chunks. The stream-oriented I/O system processes data one byte at a time. An input stream produces one byte of data, and one output stream consumes one byte of data. Creating filters for streaming data can be very easy. It is relatively straightforward to link several filters so that each filter is only responsible for a single part of the complex processing mechanism. On the downside, flow-oriented I/O is usually quite slow. A block-oriented I/O system processes data as a block. Each operation generates or consumes a block of data in one step. Processing data by block is much faster than processing data by (streaming) bytes. But block-oriented I/O lacks the elegance and simplicity of some stream-oriented I/O.
5.2 Integrated File I/O
The original I/O package and NIO have been well integrated in JDK 1.4. Java.io.* has been re-implemented on the basis of NIO, so now it can take advantage of some of the features of NiO. For example, some classes in the Java.io.* package contain methods for reading and writing data as blocks, which makes processing faster even in a more stream-oriented system. You can also use the NIO library to achieve standard I/O functionality. For example, it is easy to use block I/O to move data one byte at a time. NIO also provides many of the benefits that are not available in the original I/O package.
Reference Link: (some links need Fq), thanks to the original author's knowledge sharing
1.http://blog.csdn.net/tsyj810883979/article/details/6876594#comments
2.http://www.iteye.com/magazines/132-java-nio
3.http://ifeve.com/overview/
4.http://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html
5.http://www.myexception.cn/internet/1218142.html
6.http://www.jb51.net/article/50621.htm
7.http://www.yangyong.me/java-nio%e5%85%a5%e9%97%a8%e4%b8%8e%e8%af%a6%e8%a7%a3/
Java NiO Learning Notes (i)