JAVA NIO 中文1

来源:互联网 发布:电视棒软件下载 编辑:程序博客网 时间:2024/05/16 15:53
 

If the requested address is not a valid virtual memory address (it doesn't belong to any of
the memory segments of the executing process), the page cannot be validated, and
a segmentation fault is generated. This vectors control to another part of the kernel and
usually results in the process being killed.

如果被请求的地址不是一个有效虚拟内存地址(它不属于任何当前进程的内存段中),则页不能被确认,一个段错误会被生成。控制会被转移到内核的别的部分,常常导致进程被杀死

Once the faulted page has been made valid, the MMU is updated to establish the new virtualto-
physical mapping (and if necessary, break the mapping of the stolen page), and the user
process is allowed to resume. The process causing the page fault will not be aware of any of
this; it all happens transparently.

一旦页错误被确认有效,MMU就会更新,把新的虚拟-物理映射见了起来(如果必须,把被偷的页面的映射捣毁),然后用户进程就被允许继续了。导致了页错误的进程对此一无所知,对它而言这些是透明的。

This dynamic shuffling of memory pages based on usage is known as demand paging. Some
sophisticated algorithms exist in the kernel to optimize this process and to prevent thrashing, a
pathological condition in which paging demands become so great that nothing else can get
done.

这个动态基于使用的内存步法被称作按需分页。内核中一些老到的算法会优化这个过程防止殴打(太悲惨了,系统程序员日子暗无天日啊),指一个无法控制的情况下,页请求变得太大以至于卡死了。

1.4.4 File I/O
File I/O occurs within the context of a filesystem. A filesystem is a very different thing from a
disk. Disks store data in sectors, which are usually 512 bytes each. They are hardware devices
that know nothing about the semantics of files. They simply provide a number of slots where
data can be stored. In this respect, the sectors of a disk are similar to memory pages; all are of
uniform size and are addressable as a large array.

1.4.4 文件 I/O
文件I/O发生在文件系统上下文中。一个文件系统和磁盘是非常不一样的东东。磁盘把数据存在扇区,通常每扇区512字节。它们是物理设备对文件语义一无所知。它们只是提供一些口子然后数据可以放进去。从这个角度看,扇区和内存页很类似,都是统一尺寸的可寻址的大数组。

A filesystem is a higher level of abstraction. Filesystems are a particular method of arranging
and interpreting data stored on a disk (or some other random-access, block-oriented device).
The code you write almost always interacts with a filesystem, not with the disks directly. It is
the filesystem that defines the abstractions of filenames, paths, files, file attributes, etc.

一个文件系统是更高层的抽象。文件系统是安排和解释存储在磁盘上的特别办法(其余的比如随机访问,按块访问等)。您的代码总是和文件系统打交道,而不是和 磁盘直接打交道。文件系统定义了一些抽象:文件名,路径,文件,属性等东东。

The previous section mentioned that all I/O is done via demand paging. You'll recall that
paging is very low level and always happens as direct transfers of disk sectors into and out of
memory pages. So how does this low-level paging translate to file I/O, which can be
performed in arbitrary sizes and alignments?

上一节提到所有I/O都是通过页面调度完成的。回忆一下,页面调度是非常底层的,要么直接从磁盘扇区进来,要么当内存不够时换出。那么这么底层的页面交换如何变成文件I/O,那可是需要随心所欲的大小和对齐的。

A filesystem organizes a sequence of uniformly sized data blocks. Some blocks store meta
information such as maps of free blocks, directories, indexes, etc. Other blocks contain file
data. The meta information about individual files describes which blocks contain the file data,
where the data ends, when it was last updated, etc.

一个文件系统组织了一系列均匀大小的数据块。一些块存放元数据,比如自由块地图,目录,索引等。另外块保存文件数据。涉及单个文件的元数据描述了哪些块包含文件数据,哪里数据结束,最后更新的时间等。

When a request is made by a user process to read file data, the filesystem implementation
determines exactly where on disk that data lives. It then takes action to bring those disk
sectors into memory. In older operating systems, this usually meant issuing a command
directly to the disk driver to read the needed disk sectors. But in modern, paged operating
systems, the filesystem takes advantage of demand paging to bring data into memory.

当一个用户进程发出读文件的请求,文件系统会确定数据到底在磁盘什么地方。然后把这些扇区放到内存。在一些老的操作系统上,这常常意味着直接给磁盘驱动器发一个命令读入所需扇区。但是在现代,页调度的操作系统,文件系统利用了页面调度技术把数据放入内存。

Filesystems also have a notion of pages, which may be the same size as a basic memory page
or a multiple of it. Typical filesystem page sizes range from 2,048 to 8,192 bytes and will
always be a multiple of the basic memory page size.
How a paged filesystem performs I/O boils down to the following:
• Determine which filesystem page(s) (group of disk sectors) the request spans. The file
content and/or metadata on disk may be spread across multiple filesystem pages, and
those pages may be noncontiguous.
• Allocate enough memory pages in kernel space to hold the identified filesystem pages.
• Establish mappings between those memory pages and the filesystem pages on disk.
• Generate page faults for each of those memory pages.
• The virtual memory system traps the page faults and schedules pageins to validate
those pages by reading their contents from disk.
• Once the pageins have completed, the filesystem breaks down the raw data to extract
the requested file content or attribute information.

文件系统还拥有有一个关于页面的小东东,大小可能是一个内存页面大小或者若干个页面大小。典型的文件系统的页大小从2048到8192字节,总是基础内存页大小的整数倍。一个文件分页系统如何执行I/O归结为以下几条:

•决定哪些文件系统页被请求跨越了。文件内容和磁盘元数据也许会涉及多个页,而且这些页也许不连续。
•在内核分配足够的内存页来容纳这些业。
•在文件页和内存页之间建立映射。
• 为每个内存页生成一个页面错误。
• 虚拟内存系统捕捉这些页错误导致的陷阱,做分页计划,并读入这些页面确定之。
• 一旦分页完成了,文件系统再把它们解析成需要的文件或属性信息。

Note that this filesystem data will be cached like other memory pages. On subsequent I/O
requests, some or all of the file data may still be present in physical memory and can be
reused without rereading from disk.

注意,文件系统数据像别的内存页一样会被缓存。在I/O请求之后,部分或全部数据将继续存在物理内存中,而且可以重用,不用再读磁盘。

Most filesystems also prefetch extra filesystem pages on the assumption that the process will
be reading the rest of the file. If there is not a lot of contention for memory, these filesystem
pages could remain valid for quite some time. In which case, it may not be necessary to go to
disk at all when the file is opened again later by the same, or a different, process. You may
have noticed this effect when repeating a similar operation, such as a grep of several files. It
seems to run much faster the second time around.

大部分文件系统会预取页,它们假定进程会读取文件的剩余部分。如果不是占用太多内存,文件系统页会在内存中待好长一会儿。如此,如果文件再被打开,或者同一进程,或者别的进程,也许没有必要再去读磁盘。您也许已经注意到,当重复同一个操作,例如grep若干个文件。貌似第二次会快很多。

Similar steps are taken for writing file data, whereby changes to files (via write( )) result in
dirty filesystem pages that are subsequently paged out to synchronize the file content on disk.
Files are created by establishing mappings to empty filesystem pages that are flushed to disk
following the write operation.

写数据是类似步骤,通过write(),在脏文件页中的文件变化,随之被调出(paged out),和磁盘的文件内容同步。如果是新建的文件,通过映射到空的页建的文件,在写操作之后会被倒入到磁盘。

1.4.4.1 Memory-mapped files
For conventional file I/O, in which user processes issue read( ) and write( ) system calls to
transfer data, there is almost always one or more copy operations to move the data between
these filesystem pages in kernel space and a memory area in user space. This is because there
is not usually a one-to-one alignment between filesystem pages and user buffers. There is,
however, a special type of I/O operation supported by most operating systems that allows user
processes to take maximum advantage of the page-oriented nature of system I/O and
completely avoid buffer copies. This is memory-mapped I/O, which is illustrated in
Figure 1-6.

1.4.4.1 Memory-mapped files
按照管理,文件I/O,既用户进程通过调用read()和write()系统方法来出传输数据,总是同时存在一个或多个这种在内核文件页和用户内存区域之间拷贝数据的操作。这是因为文件页和内存缓存之间不是一一对应的。但是,一种特别的I/O而且大部分操作系统支持,允许用户进程最大限度地利用系统I/O的分页特点并完全避免缓存拷贝。这叫内存映像I/O,如Figure 1-6分析.

Memory-mapped I/O uses the filesystem to establish a virtual memory mapping from user
space directly to the applicable filesystem pages. This has several advantages:
• The user process sees the file data as memory, so there is no need to issue read( ) or
write( ) system calls.
• As the user process touches the mapped memory space, page faults will be generated
automatically to bring in the file data from disk. If the user modifies the mapped
memory space, the affected page is automatically marked as dirty and will be
subsequently flushed to disk to update the file.
• The virtual memory subsystem of the operating system will perform intelligent
caching of the pages, automatically managing memory according to system load.
• The data is always page-aligned, and no buffer copying is ever needed.
• Very large files can be mapped without consuming large amounts of memory to copy
the data.

内存映射I/O使用文件系统来建立一个虚拟内存映射,从用户空间直接到合适的文件系统页。这有几个好处:
• 用户进程视文件数据为内存,所以不需要发出read(),write()系统调用。
• 如果用户进程遇到了内存映射空间,页错会自动生成,把文件数据从磁盘带入。如果用户修改了内存映射空间,受影响的页会自动标记为脏,并随之被flush到磁盘更新文件。
• 操作系统的虚拟内存子系统会智能缓存页面,根据系统负荷自动管理内存
• 数据始终是页对齐的,不再需要缓存拷贝。
• 非常大的文件都可映射,用不着消耗大量内存。

Virtual memory and disk I/O are intimately linked and, in many respects, are simply two
aspects of the same thing. Keep this in mind when handling large amounts of data. Most
operating systems are far more effecient when handling data buffers that are page-aligned and
are multiples of the native page size.

虚拟内存和磁盘I/O亲密链接了,从很多方面来说,这是一物两面。记住当处理大量数据时,大部分操作系统在处理页对齐的或者是自然页整数倍的时候,有远高于普通情况的效率。

1.4.4.2 File locking
File locking is a scheme by which one process can prevent others from accessing a file or
restrict how other processes access that file. Locking is usually employed to control how
updates are made to shared information or as part of transaction isolation. File locking is
essential to controlling concurrent access to common resources by multiple entities.
Sophisticated applications, such as databases, rely heavily on file locking.

文件锁是一个机制,通过它一个进程可以阻止另外进程访问一个文件,或者限制另外进程访问的方式。锁常常用来 Locking is usually employed to control how
updates are made to shared information or as part of transaction isolation. File locking is
essential to controlling concurrent access to common resources by multiple entities.
Sophisticated applications, such as databases, rely heavily on file locking.

原创粉丝点击