1、Linux通过将一个虚拟存储器区域与一个磁盘上的对象(object)关联起来,以初始化这个虚拟存储器区域的内容,这个过程称为存储器映射(memory mapping)。虚拟存储器区域可以映射到两种类型的对象:
1)unix文件系统中的普通文件:一个区域可以映射到一个普通磁盘文件的连续部分。
2)匿名文件:一个区域也可以映射到一个匿名文件,匿名文件是由内核创建的,包含的全是二进制零。
一旦一个虚拟页面被初始化了,它就在一个由内核维护的专门的交换文件(swap file)间换来换去。
2、共享对象
一个对象可以被映射到虚拟存储器的一个区域,要么作为共享对象,或作为私有对象。If a process maps a shared object into an area of its virtual address space, then any writes that the process makes to that area are visible to any other processes that have also mapped the shared object into their virtual memory. Further, the changes are also reflected in the original object on disk.
Changes made to an area mapped to a private object, on the other hand, are not visible to other processes, and any writes that the process makes to the area are not reflected back to the object on disk. A virtual memory area that a shared object is mapped into is often called a shared area. Similarly for a private area.
A shared object. (a) After process 1 maps the shared object. (b) After process 2 maps the same shared object. (Note that the physical pages are not necessarily contiguous.)
3、私有对象,写时拷贝(copy-on-write)
私有对象是使用一种叫做写时拷贝的技术被映射到虚拟存储器中的。如图中所示:two processes have mapped a private object into different areas of their virtual memories, but share the same physical copy of the object. For each process that maps the private object, the page table entries for the corresponding private area are flagged as read-only, and the area struct (vm_area_struct) is flagged as private copy-on-write.只要有一个进程试图写私有区域内的某个页面,那么这个写操作就会触发一个保护策略。它就会在物理存储器中创建这个页面的一个新拷贝,更新页面条目指向这个新的拷贝,然后恢复这个页面的可写权限。
process 2 writes to a page in the private area
4、unix进程可以使用mmap函数来创建新的虚拟存储器区域,并将对象映射到这些区域中。