Virtual memory
Introducido
6:44
Sistemas operativos
An operating system turns one processor into many machines. The first three units build the illusions of the processor: processes, taking turns, and safe…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| virtual address/ˈvɜːtʃuːəl əˈdres/ | Dirección virtual |
| page table/peɪdʒ ˈteɪbl/ | tabla de páginas |
| physical frame/ˈfɪzɪkl freɪm/ | Marco físico |
| TLB/ˌtiː el ˈbiː/ | TLB (caché de traducción de direcciones) |
| page fault/peɪdʒ fɒlt/ | falta de página |
| 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. |
La mayor ilusión
- La dirección virtual se divide en número de página y desplazamiento; la tabla de páginas mapea páginas virtuales a marcos físicos, una entrada por página; la TLB guarda en caché las traducciones.
- Máquina de curso: 16-bits de dirección virtual, páginas de 256 bytes $\Rightarrow$ 256 páginas virtuales; marco físico de 12 bits $\Rightarrow$ 16 marcos — cada proceso puede abordar 64 KiB mientras solo existen 4 KiB.


16-bit virtual addresses with 256-byte pages give — virtual pages
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
8-bit VPN after the 8-bit offset: 2 to the 8th is 256 pages.
El recorrido, luego el acierto
- Dirección $0x0204$: desplazamiento $0x04$, VPN $0x02$. Primer acceso: falta en TLB, recorrido de tabla — VPN 2 a marco 5, físico $5 \times 256 + 4 = 0x0504$; la TLB guarda en caché VPN 2.
- Dirección $0x0210$: misma VPN — acierto en TLB, físico $0x0510$, sin recorrido. Un bucle de 8 páginas con 8000 accesos y una TLB LRU de 10 entradas falla 8 veces (compulsorio): una tasa de aciertos de $99.9\%$.
With VPN 2 mapped to frame 5, address 0x0204 translates to —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
frame x 256 + offset: 5 x 256 + 4 = 0x0504.
Esta tarjeta se corrigió, por eso aparece en inglés hasta que se actualice su traducción.
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 —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
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.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
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.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Belady's anomaly: on this string FIFO makes 9 faults with 3 frames but 10 with 4.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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)?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$0.99 \times 11 + 0.01 \times 21 = 11.1$.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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)?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$2^{20} \times 4$ bytes $= 4$ MiB.
Two-level table: 4 KiB outer plus 2 inner tables of 4 KiB: total (KiB)?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$4 + 2 \times 4 = 12$ KiB.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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$.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
¿Qué salió mal? Solo el equipo del sitio ve esta información.