Memory and virtual addresses
Why every process sees its own private address space, and how the OS makes that illusion cheap.
If processes used physical addresses directly, any bug could overwrite another program's data, and every program would have to know where it happened to be loaded. Neither is acceptable.
Virtual memory splits each address space into fixed-size pages and maps them to physical frames through a page table. The CPU's MMU does the translation on every access, so the program never sees real addresses.
Pages that have not been touched recently can be written to disk and unmapped. When they are needed again, a page fault brings them back. This lets a program use more memory than the machine physically has — at a cost.
/* Виртуальный адрес — это НЕ адрес в микросхеме памяти.
* MMU переводит его в физический на каждом обращении. */
#define PAGE_SHIFT 12 /* страница = 4 КиБ */
int vpn = vaddr >> PAGE_SHIFT; /* номер виртуальной страницы */
int off = vaddr & 0xFFF; /* смещение внутри страницы */
int pfn = page_table[vpn].frame; /* номер физического фрейма */
int paddr = (pfn << PAGE_SHIFT) | off;Practice
Why can two processes both use address 0x1000 without interfering?
Hints
- The address the program sees is not the address the RAM chip sees.
- Each process gets its own translation table.
12 XP
Put the steps of handling a page fault in order.
Hints
- The hardware cannot fix this alone — it must ask the kernel.
- The faulting instruction is not lost; it runs again afterwards.
14 XP
A machine has 8 GB of RAM and a program needs 20 GB. Explain what happens and why it may still work.
Hints
- Not all of the program is needed at the same moment.
- The machine has somewhere slower to put the rest.
14 XP