How every program gets its own memory
Virtual memory gives every process its own private range of addresses. The operating system maps those addresses onto real memory in small blocks called pages. This stops one program reading another program's data, and it lets a program use more memory than the machine has installed.
Behind every address your code uses, a table does the translation.
By the end of this lesson
Note
I can turn a virtual address into a physical address with a page table.
Key words
| Word | What it means | Example |
|---|---|---|
| Virtual address | an address a process uses, private to that process | |
| Physical address | the real place in the memory chips | |
| Page | a fixed-size block of virtual addresses | 4096 bytes |
| Frame | a block of real memory the same size as a page | |
| Page table | the list that says which frame holds each page | |
| Offset | how far into a page an address is |
The cells students fill in are left blank in this preview.
Why a process should not see real addresses
Suppose every program used real addresses. One bug could overwrite another program, or the system itself. Every program would also need to know where free memory is.
Instead, each process sees addresses that start from 0, as if it owned the whole machine. The processor and the system translate each address as it is used.
See how much real memory the lab has
grep -E '^(MemTotal|MemFree)' /proc/meminfoShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Compare virtual size with the part in memory
grep -E '^(VmSize|VmRSS)' /proc/self/statusShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Memory is cut into pages and frames
The system cuts virtual addresses into equal blocks called pages, and real memory into blocks of the same size called frames. Any page can sit in any frame.
To translate an address, divide it by the page size. The answer is the page number. The remainder is the offset, the distance into the page. The offset never changes in translation.
Read the real page size in bytes
getconf PAGESIZEShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Split address 8200 into page and offset
python3 -c "print(8200 // 4096, 8200 % 4096)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A page table says where each page is
The page table is a list with one entry for each page. The entry holds the frame number, or a mark that the page is not in memory.
The physical address is the frame number times the page size, plus the offset. The tables below use pages of 16 bytes so the sums stay small.
Worked example: a page table of four pages
| Page | Frame | Meaning |
|---|---|---|
| 0 | 5 | In memory, in frame 5 |
| 1 | 2 | In memory, in frame 2 |
| 2 | none | Not in memory |
| 3 | 7 | In memory, in frame 7 |
The cells students fill in are left blank in this preview.
Worked example: translating four addresses
| Virtual address | Page and offset | Frame | Physical address |
|---|---|---|---|
| 21 | 1 and 5 | 2 | 2 x 16 + 5 = 37 |
| 3 | 0 and 3 | 5 | 5 x 16 + 3 = 83 |
| 50 | 3 and 2 | 7 | 7 x 16 + 2 = 114 |
| 40 | 2 and 8 | none | Not in memory: a page fault |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a virtual address is the real position of the data in the memory chips. It is only a name that the page table turns into a real position. Two processes can use address 21 at the same time, and the table sends each to a different frame.