当 CPU 第一次接触匿名文件 (CSAPP) 时,数据是否实际在磁盘和内存之间传输

Is data actually transferred between disk and memory when CPU first touches a anonymous file ( CSAPP)

CSAPP 第 2 章第 9 章第 8 节(第 807 页)

Anonymous file: An area can also be mapped to an anonymous file, created by the kernel, that contains all binary zeros. The first time the CPU touches a virtual page in such an area, the kernel finds an appropriate victim page in physical memory, swaps out the victim page if it is dirty, overwrites the victim page with binary zeros, and updates the page table to mark the page as resident. Notice that no data is actually transferred between disk and memory. For this reason, pages in areas that are mapped to anonymous files are sometimes called demand-zero pages.

当受害页面 dirty.I 认为应该写回 disk.Why “请注意,磁盘和内存之间实际上没有传输数据。”?

不幸的是,这是 Unix 方面的糟糕术语。部分问题是历史上缺少硬文件系统(在某些 Unix 变体中得到纠正)。在理想化的分页模型中,用户创建的文件可以用作页面文件。静态数据(包括代码)可以直接从可执行文件中分页。 read/write 数据从页面文件分页。从这个意义上说,映射是匿名的,因为实际上不是文件而是页面文件的一部分。

在大多数 Unix 变体中,没有页面文件,而是交换分区。这是因为已经存在了几十年的原始 Unix 文件系统设计不佳。传统的 Unix 文件系统没有连续文件的概念。这使得无法对页面文件执行逻辑 I/O。因此,传统的Unix使用swap分区代替。

即使您映射到命名文件,在许多 Unix 变体上映射也仅用于第一次读取。在匿名映射的情况下,第一次读取创建一个需求零页。在这两种情况下,将其写回磁盘都会转到交换分区。从 Unix 的角度来看,将其称为 "anonymous" 映射是有道理的,但从概念的角度来看(人们期望内存到文件的映射是双向的)它根本没有意义。