天天看點

23、深入了解計算機系統筆記,虛拟存儲器,存儲器映射

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.

23、深入了解計算機系統筆記,虛拟存儲器,存儲器映射

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.隻要有一個程序試圖寫私有區域内的某個頁面,那麼這個寫操作就會觸發一個保護政策。它就會在實體存儲器中建立這個頁面的一個新拷貝,更新頁面條目指向這個新的拷貝,然後恢複這個頁面的可寫權限。

23、深入了解計算機系統筆記,虛拟存儲器,存儲器映射

process 2 writes to a page in the private area

4、unix程序可以使用mmap函數來建立新的虛拟存儲器區域,并将對象映射到這些區域中。

繼續閱讀