# Ch18 Paging 這一章是《Operating Systems: Three Easy Pieces》的第 18 章,主題是 Paging: Introduction(分頁:簡介),目的是介紹一種在現代作業系統中非常常見的 記憶體虛擬化方法。 ## Intro - 早期使用 segmentation 把記憶體切成**不同大小**的 segment,容易產生 **external fragmentation** - **Paging**:把記憶體切成**固定大小的區塊** - Virtual memory -> **pages** - Physical memory -> **frames** - 好處: - 防止 internal/external fragmentation - 讓 process 可以更靈活運用記憶體空間 ![image](https://hackmd.io/_uploads/SJppIBlAkg.png) ## Address translation - 把 virtual address 拆成 - **VPN**:Virtual page 的編號 - **Offset**:在 page 中的第幾個 byte ![image](https://hackmd.io/_uploads/ByjCISgCyg.png) - 利用 **Page table** 找出與 VPN 對應的 **PFN** - 用 **PFN + offset** 找出 physical address ![image](https://hackmd.io/_uploads/SJa5uSxA1x.png) ### 找到 PTE 在哪 - **PTE(Page table entry)**: - Page table 中用來記錄 VPN -> PFN 關係的表格 - **the number of PTE = the number of virtual page** - **PTBR(Page Table Base Register)**: - Page table 的 base - 找出 PTE 的公式 - $\mathrm{target\ PTE = PTBR + VPN \times sizeof(PTE)}$ - 整體流程: ![image](https://hackmd.io/_uploads/HJDiOHgAJe.png) ## 問題 有些過程硬體的讀取速度較慢: ![image](https://hackmd.io/_uploads/rkifFSgRkl.png) - **解決方案**:使用 **TLB**(下一章內容)