Virtual memory
แนะนำใหม่
6:44
ระบบปฏิบัติการ
An operating system turns one processor into many machines. The first three units build the illusions of the processor: processes, taking turns, and safe…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| virtual address/ˈvɜːtʃuːəl əˈdres/ | ที่อยู่เสมือน |
| page table/peɪdʒ ˈteɪbl/ | ตารางหน้า |
| physical frame/ˈfɪzɪkl freɪm/ | เฟรมฟิสิกส์ |
| TLB/ˌtiː el ˈbiː/ | |
| page fault/peɪdʒ fɒlt/ | ข้อผิดพลาดหน้า |
| Belady's anomaly/bɪˈleɪdiz əˈnɒməli/ | with FIFO page replacement, giving a process more frames can cause more page faults |
| thrashes/ˈθræʃɪz/ | spends most of its time swapping pages in and out instead of doing useful work |
| two-level page table/tuː ˈlevl peɪdʒ ˈteɪbl/ | A page table split into an outer table and inner tables, with inner tables created only for regions of memory in use. |
The biggest illusion
- A virtual address 虚拟地址 splits into a page number and an offset. The page table 页表 maps each virtual page to a physical frame 物理页框, one entry per page. The TLB 快表 caches recent translations.
- The course machine: 16-bit virtual addresses with 256-byte pages give 256 virtual pages; 12-bit physical addresses give 16 frames. Each process can address 64 KiB, while only 4 KiB really exists.

16-bit virtual addresses with 256-byte pages give — virtual pages
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
8-bit VPN after the 8-bit offset: 2 to the 8th is 256 pages.
The walk, then the hit
- Address $0x0204$: offset $0x04$, VPN (virtual page number) $0x02$. First access: TLB miss, so the hardware walks the table and finds VPN 2 in frame 5. The physical address is $5 \times 256 + 4 = 0x0504$, and the TLB stores VPN 2.
- Address $0x0210$ has the same VPN: TLB hit, physical $0x0510$, no walk. A loop over 8 pages with 8000 accesses and a 10-entry LRU TLB misses only 8 times (the first touches): a $99.9\%$ hit rate.
With VPN 2 mapped to frame 5, address 0x0204 translates to —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
frame x 256 + offset: 5 x 256 + 4 = 0x0504.
The fault and its price
- Address $0x0400$: VPN 4, and its entry is invalid, so there is a page fault 缺页. The process blocks. The OS chooses a page to remove (by LRU; if it was changed, it is written back first), reads the new page in, updates the table and the TLB, and runs the instruction again.
- The cost model: TLB hit 1 ns, table walk 10 ns, fault 5 ms. One fault per million accesses still adds 5 ns per access on average. The hierarchy works only if faults are rare.
A page fault can mean that a valid page is absent or that the access is invalid. The OS checks the address mapping to distinguish them.
A page fault differs from an invalid access because the fault —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A fault on a valid but nonresident page triggers a fetch; an invalid access is an error. The OS checks the address mapping to tell them apart.
Choosing the page to remove
- Take the reference string $1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5$ with $3$ frames. Each policy removes a different page when a frame is needed:
| Policy | Removes | Faults |
|---|---|---|
| FIFO | the oldest page | 9 |
| LRU | the least recently used page | 10 |
| OPT | the page used furthest in the future | 7 |
- OPT is an ideal no real system can reach, because it needs to know the future.
- Belady's anomaly 贝拉迪异常: FIFO with $4$ frames makes $10$ faults, more than with $3$. LRU never does that. When the working sets of all processes are bigger than memory, the system thrashes 抖动: it spends its time on faults, not on work.
How many page faults does FIFO make on 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with 4 frames?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The four first loads, then 5, 1, 2, 3, 4, 5 all fault: 4 + 6 = 10, one more than with 3 frames.
Under FIFO, adding frames always reduces page faults.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Belady's anomaly: on this string FIFO makes 9 faults with 3 frames but 10 with 4.
Effective access time
- A TLB hit costs 1 ns plus the 10 ns memory access; a miss adds one more 10 ns access for the table walk. At a 99% hit rate: $0.99 \times 11 + 0.01 \times 21 = \mathbf{11.1}$ ns.
- The TLB hides almost all of the translation cost. At 90% hits the same machine averages 12 ns, so programs that jump across many pages pay for it.
TLB 1 ns, memory 10 ns, one-level walk, 99% TLB hits: effective access time (one decimal, ns)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$0.99 \times 11 + 0.01 \times 21 = 11.1$.
Worked example: how big is a page table?
- A 32-bit address with 4 KiB pages has $2^{20}$ pages. With 4-byte entries, a flat table is $2^{20} \times 4 = \mathbf{4}$ MiB for every process, even a tiny one.
- A two-level page table 二级页表 keeps a 4 KiB outer table and creates inner tables only where memory is used. A process that uses 8 MiB in one area needs 2 inner tables: 12 KiB in total.
32-bit addresses, 4 KiB pages, 4-byte entries: flat page table size (MiB)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$2^{20} \times 4$ ไบต์ $= 4$ MiB.
Two-level table: 4 KiB outer plus 2 inner tables of 4 KiB: total (KiB)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$4 + 2 \times 4 = 12$ KiB.
Marks that slip away
- Translating the offset. Only the page number is mapped; the offset is copied unchanged: $0x0204$ in frame 5 is $0x0504$.
- Treating a page fault as an error. A fault on a valid page is normal: the OS loads the page and runs the instruction again.
- Assuming LRU always beats FIFO. On the course reference string FIFO makes $9$ faults and LRU $10$.
Recap
- Virtual address = page number | offset; the page table maps pages to frames; the TLB caches the mapping.
- $0x0204 \to$ VPN 2, offset 4 $\to$ frame 5 $\to 0x0504$.
- A fault costs milliseconds, so the hierarchy works only if faults are rare.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้