Module 9 · 17 min read · Free chapter
Memory management and virtual memory
Paging splits an address into page number and offset and maps pages to frames. With a TLB, effective access time is h(t + m) + (1 − h)(t + 2m). Page faults are costly, so the fault rate must be tiny. OPT gives the fewest faults, LRU approximates it, and FIFO can show Belady's anomaly.
Start from zero: a small desk and a big shelf
Think of a library reading room. Your desk can hold only four books, but for your project you need forty, which stay on the tall shelves behind you. You keep the books you are using now on the desk, and when you need another, you put one back and fetch the new one.
Main memory is the desk and the disk is the shelf. A program may be far bigger than memory, yet it runs smoothly, because at any moment it uses only a few parts of itself. The operating system decides which parts sit in memory and quietly swaps the rest.
To do this well, the OS needs answers to three questions. How does a program's address turn into a real place in memory? How is memory shared between many programs without gaps going to waste? And when memory is full, which part should go back to the shelf?
This chapter answers all three, with the numericals that GATE asks.
Why it matters. Without virtual memory, every program would have to fit entirely in RAM and know where it was loaded. With it, your laptop runs dozens of programs whose total size is many times the RAM.
Address binding and contiguous allocation
A program refers to memory by addresses, and these must be bound to real locations at some point. Binding at compile time fixes the location in the code, binding at load time fixes it when the program is loaded, and binding at execution time lets the location change while the program runs. Only execution-time binding allows a process to be moved or swapped back to a different place.
The CPU produces a logical (virtual) address, and the memory-management unit (MMU) turns it into a physical address. In the simplest scheme the MMU holds two registers for the running process. It checks that the logical address is less than the limit register, traps if it is not, and adds the relocation (base) register to get the physical address.
In contiguous allocation each process gets one unbroken block. As processes come and go, memory becomes a pattern of used blocks and free holes, and a new request must be placed in a hole.
| Strategy | Rule | Request of 280 KB, holes 150, 400, 250, 600, 300 KB |
|---|---|---|
| First fit | first hole that is big enough | 400 KB, leaving 120 KB |
| Best fit | smallest hole that is big enough | 300 KB, leaving 20 KB |
| Worst fit | largest hole | 600 KB, leaving 320 KB |
First fit is fastest and usually as good as best fit in memory use. Best fit leaves tiny useless slivers, and worst fit leaves large holes that may still be useful.
Two kinds of waste appear. External fragmentation means the total free memory is enough, but it is split into holes too small to use; compaction shuffles processes together to merge the holes, which needs execution-time binding. Internal fragmentation means space is wasted inside a block, because the block given is bigger than the request.
Why it matters. Fragmentation is the main reason contiguous allocation was replaced by paging. Exam questions ask which fit a request goes to and which kind of fragmentation a scheme suffers from.
Paging and the page table
Paging cuts logical memory into fixed-size pages and physical memory into frames of the same size. Any page can go into any free frame, so a process no longer needs one unbroken block, and external fragmentation disappears. A page table for each process records which frame holds each page.
Page sizes are powers of two, which makes splitting an address trivial. If the logical address has bits and the page size is bytes, the high bits are the page number and the low bits are the offset . The MMU replaces by the frame number from the page table and keeps unchanged.
Worked example 1. Pages are 1 KB, so the offset has 10 bits. For logical address 2500, the page number is and the offset is . If the page table maps page 2 to frame 5, the physical address is
Worked example 2. A system has 32-bit logical addresses, 4 KB pages and 4-byte page-table entries. The offset needs 12 bits, so the page number has 20 bits and there are pages. A flat page table therefore needs bytes = 4 MB for every process, even a tiny one.
Worked example 3. The same machine has 1 GB of physical memory. It holds frames, so a frame number needs 18 bits.
Besides the frame number, each page-table entry carries control bits. The valid bit says whether the page belongs to the process and is in memory, protection bits allow read, write or execute, the dirty bit says the page was changed, and the reference bit says it was used recently.
Paging still has internal fragmentation, because the last page of a process is usually only partly full. On average the waste is half a page per process, which is why pages are not made very large.
Why it matters. Paging is how every modern operating system manages memory. Splitting an address into page number and offset is the first step of nearly every memory numerical in GATE.
The TLB, multilevel and inverted page tables
The page table lives in main memory, so a naive scheme needs two memory accesses for every data access: one to read the page-table entry and one to read the data. That would halve the speed of every program. The fix is a translation look-aside buffer (TLB), a small, very fast associative cache that holds recently used page-to-frame entries.
On a TLB hit the frame number comes from the TLB, and only one memory access is needed. On a miss the page table is read from memory and the entry is copied into the TLB. With TLB lookup time , memory access time and hit ratio , for a one-level table
Worked example 4. Let ns, ns and . Then ns, a slowdown of 40% over plain memory. Raising the hit ratio to 0.98 gives ns.
Multilevel page tables. A 4 MB flat table per process is wasteful when most of the address space is unused. The answer is to page the page table: a 32-bit address with 4 KB pages is split into 10 bits for the outer table, 10 bits for an inner table and 12 bits of offset. The outer table has entries of 4 bytes, which is exactly one 4 KB page, and inner tables are created only for regions the process actually uses.
The price is time. With two levels, a TLB miss needs three memory accesses, two for the tables and one for the data, so the formula becomes . The TLB is what makes this cost bearable.
An inverted page table turns the idea around. It has one entry for each physical frame in the whole system, holding the process ID and page number stored there, so its size depends on physical memory and not on the number of processes. Searching it is slow, so it is used with a hash table.
Why it matters. EAT with a TLB is among the most common GATE numericals in this module. Knowing how levels add memory accesses lets you adapt the formula to any question.
Segmentation
Programmers do not think of a program as a row of equal pages. They think of a code part, a global data part, a stack and a heap. Segmentation matches this view: memory is divided into variable-size segments, each a logical unit with its own name and length.
A logical address is a pair (s, d): segment number and offset within it. The segment table gives each segment a base, where it starts in physical memory, and a limit, its length. The hardware checks that , traps if not, and otherwise forms the physical address .
Worked example 5. Segment 1 has base 2000 and limit 600. The address (1, 450) is legal because , and it maps to . The address (1, 700) fails the limit check, so the hardware traps to the OS with an addressing error.
| Feature | Paging | Segmentation |
|---|---|---|
| Unit size | fixed pages | variable segments |
| Seen by programmer | no | yes |
| Fragmentation | internal only | external |
| Protection and sharing | per page | per logical unit, more natural |
Because segments vary in size, segmentation brings back external fragmentation. Segmentation with paging solves this: each segment is itself paged, so the programmer keeps logical segments while memory is still handed out in frames.
Why it matters. Segmentation explains why a program can protect its code as read-only while its stack stays writable. The limit check is also a favourite one-step numerical.
Demand paging and page faults
Virtual memory lets a process run even when only part of it is in memory. Under demand paging, a page is loaded only when it is first used, and pages not in memory are marked invalid in the page table.
When the CPU touches an invalid page, the hardware raises a page fault, and the OS handles it in six steps. It checks that the reference is legal, finds a free frame, schedules a disk read of the page into that frame, waits while another process runs, updates the page table to valid, and restarts the instruction that faulted. If there is no free frame, a page must first be chosen for replacement, and if that page is dirty it must be written back to disk.
A page fault is very expensive, because a disk is about a hundred thousand times slower than memory. With page-fault rate , memory access time and fault service time ,
Worked example 6. Let ns and ms ns. With , ns, about 8.2 μs. One fault in a thousand accesses makes memory about 40 times slower.
How small must be to keep the slowdown under 10%? We need , so . That is fewer than one fault in about 400,000 accesses.
Demand paging works because programs show locality of reference: in any short period they use a small set of pages, such as one loop and the data it touches. Pure demand paging starts a process with no pages at all and lets the first faults bring them in.
Why it matters. This numerical shows why the page-fault rate must be tiny and why good replacement matters so much. It is also why adding RAM speeds up a slow computer more than almost anything else.
Page replacement: FIFO, optimal and LRU
When memory is full and a fault occurs, the OS must pick a victim page. The goal is the fewest page faults, and methods are compared on a reference string, the sequence of pages a process uses. We use 1, 3, 0, 3, 5, 6, 3, 1, 0, 3 with 3 frames, counting the first loads as faults.
- FIFO replaces the page that has been in memory longest, like a queue.
- Optimal (OPT) replaces the page that will not be used for the longest time in future, which gives the fewest faults but needs to know the future.
- LRU (least recently used) replaces the page not used for the longest time in the past, using the past as a guide to the future.
| Reference | 1 | 3 | 0 | 3 | 5 | 6 | 3 | 1 | 0 | 3 |
|---|---|---|---|---|---|---|---|---|---|---|
| FIFO | F | F | F | H | F | F | F | F | F | H |
| LRU | F | F | F | H | F | F | H | F | F | H |
| OPT | F | F | F | H | F | F | H | H | F | H |
Worked example 7. FIFO has 8 faults: at 5 it evicts 1, at 6 it evicts 3, and then 3 must come back, evicting 0. LRU has 7 faults: at 6 it evicts 0, the least recently used, so 3 stays and hits. OPT has 6 faults: at 5 it evicts 0, which is needed latest, and at 6 it evicts 5, which is never needed again, so both 3 and 1 hit.
Belady's anomaly. You would expect more frames to mean fewer faults, but FIFO can break this rule. On the string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, FIFO gives 9 faults with 3 frames and 10 faults with 4 frames.
LRU and OPT never show the anomaly because they are stack algorithms: the pages held with frames are always a subset of those held with frames. On the same string, LRU gives 10 faults with 3 frames and 8 with 4.
Exact LRU needs a timestamp or a stack updated on every memory access, which is costly. Real systems approximate it with the reference bit, as in the second-chance or clock algorithm.
Why it matters. Counting faults on a reference string is asked in almost every GATE paper. Belady's anomaly is a favourite one-line theory question.
Thrashing and the working set
Raising the number of processes in memory usually raises CPU use, because while one process waits for I/O another can run. But beyond a point each process has too few frames for the pages it is actively using. It faults, takes a frame from another process, which then faults too, and soon every process is waiting for the disk.
This collapse is thrashing: the system spends more time paging than doing useful work. A naive scheduler then sees low CPU use and admits even more processes, which makes it worse.
The cure is to give each process as many frames as its current locality needs. The working set of a process is the set of distinct pages it referenced in the last references. For example, if the last five references were 2, 6, 1, 5, 2 and , the working set is {1, 2, 5, 6}, of size 4.
If is the working-set size of process , the total demand is . If exceeds the number of available frames, the OS suspends one process and frees its frames, and when demand falls it lets a new process in. Choosing matters: too small misses the locality, and too large spans several localities.
A simpler control is the page-fault frequency method. The OS sets an upper and a lower bound on the fault rate, gives a process more frames when its rate is above the upper bound, and takes frames away when it is below the lower bound.
Why it matters. Thrashing explains why a computer with too little RAM becomes almost unusable rather than just a bit slower. The working-set idea is how the OS keeps the degree of multiprogramming in a safe range.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about memory in four ways:
- Explain address translation in paging or segmentation with a labelled diagram.
- Compute page-table sizes, bit widths, effective access times with a TLB or with page faults.
- Trace page-replacement methods on a reference string and comment on the results.
- Discuss a problem and its cure, such as fragmentation and compaction, or thrashing and the working set.
Practice questions (our own, not previous-year questions).
- Explain paging with a diagram of address translation, and show how a TLB reduces the effective access time with a numerical example. (15 marks)
- For the reference string 7, 0, 1, 2, 0, 3, 0, 4 with 3 frames, find the number of faults under FIFO, LRU and optimal replacement. What is Belady's anomaly? (10 marks)
- What is thrashing? Explain how the working-set model and page-fault frequency control prevent it. (10 marks)
Answer plan for question 3.
- Define thrashing as the state in which processes spend more time servicing page faults than executing.
- Explain the cause, which is too many processes in memory so that each has fewer frames than its current locality needs.
- Draw the graph of CPU utilisation against the degree of multiprogramming, showing the sharp fall after the peak.
- Define the working set as the distinct pages referenced in the last Δ references and give a short example.
- Explain that the OS compares the total working-set demand with available frames and suspends a process when demand is higher.
- Describe page-fault frequency control with its upper and lower bounds, and conclude that both methods keep each process's locality in memory.
Quick revision
- The MMU checks a logical address against the limit register and adds the base register.
- First fit takes the first big-enough hole, best fit the smallest and worst fit the largest.
- Compaction cures external fragmentation, and paging removes it but keeps internal fragmentation.
- With an -bit address and page size , the page number has bits and the offset bits.
- A 32-bit address with 4 KB pages and 4-byte entries needs a 4 MB flat page table.
- With a TLB, for one level, and each extra level adds one on a miss.
- In segmentation the offset must be less than the limit, and the physical address is base plus offset.
- With page faults, , so must be below about one fault in 400,000 accesses.
- OPT is best, LRU is the practical approximation, and FIFO can show Belady's anomaly.
- Thrashing is cured by giving each process its working set and reducing multiprogramming.