---
tags: cs:app, csapp
---
# [CS:APP](https://hackmd.io/@sysprog/CSAPP) 第 9 章重點提示和練習
## 導讀
* [Linux 核心設計: 不僅是個執行單元的 Process](https://hackmd.io/s/r1ojuBGgE)
* [Linux 核心設計: 記憶體管理](https://hackmd.io/@sysprog/rJBXOchtE)
* [Introduction to Memory Management in Linux](https://elinux.org/images/b/b0/Introduction_to_Memory_Management_in_Linux.pdf) / [video](https://youtu.be/7aONIVSXiJ8)
* [mmap](http://man7.org/linux/man-pages/man2/mmap.2.html) 系統呼叫
![](https://i.imgur.com/zUJRT0s.png)
![](https://i.imgur.com/WokCQmO.png)
![](https://i.imgur.com/FLF2JMP.png)
> 出處: [Memory mapping](https://courses.engr.illinois.edu/cs241/sp2014/lecture/11-MemoryMapping_annotated_sol.pdf)
## Virtual Memory
- [ ] [Virtual Memory: Concepts](https://www.cs.cmu.edu/~213/lectures/16-vm-concepts.pdf) / [錄影](https://www.youtube.com/watch?v=Fy9cnP9TXUc&list=PL22J-I2Pi-Gf0s1CGDVtt4vuvlyjLxfem&index=17)
![](https://i.imgur.com/AIKZ8Y3.png)
Why Virtual Memory (VM)?
* Uses main memory efficiently
* Use DRAM as a cache for parts of a virtual address space
* Simplifies memory management
* Each process gets the same uniform linear address space
* Isolates address spaces
* One process can’t interfere with another’s memory
* User program cannot access privileged kernel information and code
![](https://i.imgur.com/rzJVAYO.png)
![](https://i.imgur.com/C33PAiY.png)
==Page 561 ~ 563==
MMU 的 page size 選擇是有限的,而在 x86 32-bit 保護模式有以下兩種 page 設定:
* normal ones, 4 KiB
* huge ones, 4 MiB
不是每種 x86 處理器都支援 large page,需要具備 Page Size Extension (PSE)
4 KiB 不僅在 x86 架構出現,其他架構也常見,計算方式:
So assume that both page directory and page table contain 2^x^ entries, and the page size is 2^y^ bytes. To make full use of the 2^32^ address, we have:
2^x^ * 2 ^x^ * 2^y^ = 2^32^ $\rightarrow 2x + y = 32$
Each entry in page directory/table consumes 4 bytes (32-bit), therefore:
2^y^ / 4 = 2^x^ $\rightarrow y - 2 = x$
Thus y = 12, and page size in bytes will be 2^y^ = 2^12^ = 4 KiB.
注意: 4M hugepages are only for 32-bit mode x86. 64-bit x86 uses 2M or 1G hugepages, because the 4-level page-table format uses 9 bits per level.
[Page size trade-off](https://en.wikipedia.org/wiki/Page_%28computer_memory%29#Page_size_trade-off)
```clike
#include <stdio.h>
#include <unistd.h> /* sysconf(3) */
int main(void) {
printf("The page size for this system is %ld bytes.\n",
sysconf(_SC_PAGESIZE)); /* _SC_PAGE_SIZE is OK too. */
return 0;
}
```
參考輸出: (x86_64/Linux)
```
The page size for this system is 4096 bytes.
```
![](https://i.imgur.com/931pLTJ.png)
![](https://i.imgur.com/hIiWuwT.png)
![](https://i.imgur.com/bkMFP48.png)
==往後到 p.19==
==Page 565==
* Virtual memory seems terribly inefficient, but it works
because of locality.
* At any point in time, programs tend to access a set of active
virtual pages called the working set
* Programs with better temporal locality will have smaller working sets
* If (working set size < main memory size)
* Good performance for one process (after cold misses)
* If (working set size > main memory size )
* Thrashing: Performance meltdown where pages are swapped (copied)
in and out continuously
* If multiple processes run at the same time, thrashing occurs if
their total working set size > main memory size
![](https://i.imgur.com/kKmqHZS.png)
==Page 565 ~ 567==
![](https://i.imgur.com/YJ4CWb1.png)
![](https://i.imgur.com/LiOJpK4.png)
* [反璞歸真系列之探究 UNIX v6 作業系統設計](https://www.slideshare.net/jserv/unix-v6study)
* 在 1970 年 1 月份發佈的 16-bit [PDP-11](https://en.wikipedia.org/wiki/PDP-11) 即具備 [KL-11 MMU](https://minnie.tuhs.org//pipermail/tuhs/2016-December/007500.html)
VM Address Translation
==p.29 到 p.42==
==Page 568 ~ 575==
- [ ] [Virtual Memory: Systems](https://www.cs.cmu.edu/~213/lectures/17-vm-details.pdf) / [錄影](https://www.youtube.com/watch?v=uVjwj4kcqEU)
![](https://i.imgur.com/FTgjDy6.png)
![](https://i.imgur.com/2FcINb0.png)
==Page 579==
Copy-on-write (COW): p.31 ~ p.42
==Page 582 ~ 586==
搭配參照:
* [The Linux Virtual Memory System](https://people.redhat.com/pladd/NUMA_Linux_VM_NYRHUG.pdf)
* [20 years of Linux Virtual Memory: from simple server workloads to cloud virtualization](https://archive.fosdem.org/2017/schedule/event/iaas_20yealin/attachments/slides/1498/export/events/attachments/iaas_20yealin/slides/1498/VM.pdf)