Virtual memory, paging and segmentation
| English | Chinese | Pinyin |
|---|---|---|
| pages | 页 | yè |
| paging | 分页 | fēn yè |
| segmentation | 分段 | fēn duàn |
| virtual address space | 虚拟地址空间 | xū nǐ dì zhǐ kōng jiān |
| frames | 页框 | yè kuāng |
| page table | 页表 | yè biǎo |
| page fault | 缺页 | quē yè |
| swap file | 交换文件 | jiāo huàn wén jiàn |
| thrashing | 抖动 | dǒu dòng |
The disk light that means the machine has given up
- Open too many applications on a machine short of memory and something distinctive happens. Everything slows almost to a stop, and the disk light stops flickering and stays on.
- The processor is not busy. It is waiting, because the memory each program needs is on the disk, and fetching one page evicts another that is needed a moment later.
- The machine is doing nothing but moving pages. It has enough work and enough memory to do neither.
- This lesson is virtual memory, how paging 分页 and segmentation 分段 provide it, and how the same mechanism that makes memory look bigger can make a machine useless.
The virtual address space
- Each process gets its own virtual address space 虚拟地址空间: a clean, contiguous range of addresses starting at zero, which the OS maps to wherever the data really is.
- Three things follow. Each process sees a simple private space and need not know what else is running. Processes are protected from each other, since one cannot name an address in another's space. And the total memory in use can exceed the physical RAM, because part of it lives on disk.
- That isolation is a security boundary as much as a convenience.
A benefit of virtual memory is that it:
Virtual memory gives each process a private space and lets the working set exceed installed RAM by using disk.
What does giving each process a virtual address space achieve? Select all that apply.
Virtual memory buys simplicity, isolation and capacity. It costs speed, since part of the memory is really on disk.
Paging
- The virtual space is divided into fixed-size pages 页; physical memory into frames 页框 of the same size. A page table 页表 records which frame currently holds each page.
- When a process uses an address, the hardware looks up the page in the table and finds the frame. If the page is not in RAM, that is a page fault 缺页.
- On a page fault the OS reads the page from the swap file 交换文件 on disk into a free frame. If no frame is free, it must first evict a page, writing it back to disk if it has been changed.

Same-sized pages and frames, joined by a table
In paging, memory is divided into:
Paging uses fixed-size pages and frames, linked by a page table. (Variable sizes are segmentation.)
A page fault occurs when:
The accessed page isn't in a frame, so the OS fetches it from the swap file (evicting another page if needed).
Worked example: what happens on a page fault
- A process accesses an address whose page is not in RAM. Describe what the OS does. [4]
- The hardware detects that the page is not present and raises a page fault interrupt, and the process is blocked.
- The OS finds a free frame; if there is none, it selects a page to replace, and writes it back to the swap file if it has been modified.
- It reads the required page from the swap file into that frame and updates the page table.
- The process is returned to the ready state and the instruction is retried. Four steps, four marks, and note the process was blocked throughout.
What happens on a page fault
Step through a page fault. When the program touches a page that isn't in RAM, the OS quietly fetches it from disk and updates the page table — so the program sees more memory than physically exists.
Put the OS's response to a page fault in order.
Detect and block, make room, load, record, retry. A page fault is the normal mechanism, not an error.
Thrashing
- Choosing which page to replace matters, because the wrong choice means it will be needed again immediately.
- When there is too little RAM for the working set of the running processes, almost every access causes a page fault, and every fault evicts a page that is wanted again at once.
- The system then spends nearly all its time swapping pages instead of executing instructions. That is thrashing 抖动, or disk thrashing, and it is the state described at the top of this lesson.
- The cure is more RAM, or fewer processes; a faster processor does not help at all, because the processor is not the thing that is busy.
A machine slows almost to a stop and the disk runs constantly. What will fix it?
This is thrashing: nearly every access faults and the processor waits. The processor is not the busy component, so speeding it up changes nothing.
Segmentation
- Segmentation divides memory into variable-sized logical segments that match the program's own structure: the code, the stack, the heap, a large data array.
- Because a segment is a logical unit, it can carry its own permissions: the code segment can be read-only and executable, the stack read-write and not executable.
- The contrast the exam wants: pages are fixed-size and have no meaning to the program; segments are variable-sized and correspond to logical parts of it. Many real systems combine the two, paging within segments.

Segments are the program's own parts, not equal slices
Match each memory-management term to its meaning.
Paging = fixed pages; segmentation = logical variable units; a page fault triggers a swap; too many faults = thrashing.
Segmentation divides memory into variable-sized logical units (code, stack, heap), each with its own permissions, whereas paging uses fixed-size pages.
Segments match the program's logical structure; pages are uniform fixed-size blocks — some systems combine both.
Unlike pages, which are all the same size, segments are of ____ size and match the program's logical parts.
A segment is the code, the stack or the heap, so it can carry its own permissions. A page is a physical division with no meaning to the program.
Worked example: paging against segmentation
- State two differences between paging and segmentation. [2]
- Pages are all of a fixed size, decided by the system; segments are of variable size, decided by the logical parts of the program.
- A page is a purely physical division with no meaning to the program; a segment corresponds to a logical unit such as the code or the stack, and can be given its own access permissions.
- A third if needed: paging can leave unused space inside the last page of an allocation, while segmentation leaves gaps of unusable space between segments.
Marks that slip away
- Virtual memory does not create memory. It lets the total in use exceed physical RAM by keeping part of it on disk, at the cost of disk accesses.
- A page fault is not an error. It is the normal mechanism by which a page is brought in.
- Thrashing is spending more time swapping than executing, and the fix is more RAM or fewer processes, never a faster processor.
- Fixed size versus variable size, logically meaningful is the difference between paging and segmentation. Say both halves.
You've got it
- a virtual address space gives each process a private contiguous range, protects processes from each other, and lets memory in use exceed physical RAM
- paging: fixed-size pages mapped to frames by a page table; a page fault blocks the process while the OS evicts a page if necessary, loads from the swap file, updates the table and retries
- thrashing is more time spent swapping than executing; cure it with more RAM or fewer processes
- segmentation uses variable-sized segments matching the program's logical parts, each with its own permissions; many systems page within segments