Sub topic 15
GATE Problem-Solving Patterns
This chapter turns the earlier concepts into repeatable procedures for numerical and tracing questions. It assumes you know the earlier chapters of this tutorial: paging, multilevel tables, the TLB, demand paging, page replacement and thrashing. You should also be comfortable with powers of two and reading short Python.
A Working Routine
Most memory-management questions follow the same four steps.
- Write down every given quantity with its unit. Convert sizes to powers of two (1 KB = 2^10 bytes, 1 MB = 2^20 bytes) and times to one unit.
- Decide what the question asks for: bit counts, a size, a time, or a count of faults.
- Pick the pattern below that matches.
- Check the result against a sanity rule. Page offset bits equal log2(page size). A hit ratio never exceeds 1. EAT never falls below the memory access time.
Pattern 1 and 2: Bit Split and Page Table Size
For a virtual address of v bits and pages of 2^d bytes, the offset is d bits and the page number is v - d bits. A single-level page table has 2^(v-d) entries, so its size is entries × PTE size.
For a multilevel table, each page-table page must fit in one page. It therefore holds page size / PTE size entries, which gives log2 of that many index bits per level. The number of levels is the page-number bits divided by the index bits, rounded up.
def levels(va_bits, page_bytes, pte_bytes):
offset_bits = page_bytes.bit_length() - 1
index_bits = (page_bytes // pte_bytes).bit_length() - 1
page_no_bits = va_bits - offset_bits
n_levels = -(-page_no_bits // index_bits) # ceiling division
return offset_bits, page_no_bits, index_bits, n_levels
print(levels(32, 4096, 4)) # (12, 20, 10, 2)
print(levels(40, 8192, 8)) # (13, 27, 10, 3)For the first case, the single-level table has 2^20 entries × 4 bytes = 4 MB. Two levels of 10 bits each split it into 1024-entry tables of exactly one page. For the second case, 27 bits do not divide evenly by 10. One level then uses only 7 bits, and by convention the outermost level takes the smaller share.
Pattern 3: Effective Access Time with a TLB
Let h be the TLB hit ratio, t the TLB lookup time and m the memory access time. The standard form for an n-level table is:
EAT = h × (t + m) + (1 − h) × (t + n×m + m)
A miss costs n memory accesses to walk the table plus one for the data.
def eat(h, t_tlb, t_mem, levels=1):
hit = t_tlb + t_mem
miss = t_tlb + levels * t_mem + t_mem
return h * hit + (1 - h) * miss
print(eat(0.9, 10, 100)) # 120.0 (up to rounding)
print(eat(0.9, 10, 100, 2)) # 130.0 (up to rounding)Check the second value by hand: 0.9 × 110 + 0.1 × 310 = 99 + 31 = 130 ns.
Some questions state that the TLB lookup overlaps with the memory access, or that it is "negligible". In that case set t to 0. If the question gives a cache as well, apply the same weighted-average idea one level at a time.
Pattern 4: Effective Access Time with Page Faults
Let p be the page-fault rate, m the memory access time and S the fault service time. Then:
EAT = (1 − p) × m + p × S
Questions usually ask for the largest p that keeps the slowdown under a limit. Take m = 200 ns and S = 8 ms = 8,000,000 ns, with EAT allowed to be at most 10% above m, which is 220 ns:
200 + p × (8,000,000 − 200) ≤ 220, so p ≤ 20 / 7,999,800 ≈ 2.5 × 10^-6.
That is about one fault per 400,000 accesses. This is the reason fault rates must be tiny.
A common variant adds dirty pages. If a fraction d of victim pages is modified, the service time becomes S = S_clean + d × S_writeback, because a dirty victim needs an extra disk write. Substitute that S into the same formula.
Pattern 5: Tracing a Reference String
Keep a table with one column per reference and mark each fault. Use these rules:
- FIFO evicts the page that has been resident longest. A hit does not change the order.
- LRU evicts the page whose last use is furthest in the past. A hit moves the page to the most-recent position.
- Optimal evicts the page whose next use is furthest in the future, or never. Ties may be broken arbitrarily without changing the fault count.
Always count the initial loads of empty frames as faults, unless the question says the frames start filled.
def fifo_faults(refs, frames):
memory, faults = [], 0
for p in refs:
if p not in memory:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(p)
return faults
def lru_faults(refs, frames):
memory, faults = [], 0
for p in refs:
if p in memory:
memory.remove(p)
else:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(p)
return faults
def opt_faults(refs, frames):
memory, faults = [], 0
for i, p in enumerate(refs):
if p in memory:
continue
faults += 1
if len(memory) == frames:
future = refs[i + 1:]
def next_use(q):
return future.index(q) if q in future else len(future) + 1
memory.remove(max(memory, key=next_use))
memory.append(p)
return faults
refs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
for f in (fifo_faults, lru_faults, opt_faults):
print(f.__name__, f(refs, 3), f(refs, 4))The output matches this table.
| Algorithm | 3 frames | 4 frames |
|---|---|---|
| FIFO | 9 | 10 |
| LRU | 10 | 8 |
| Optimal | 7 | 6 |
FIFO gets worse when frames increase from 3 to 4. This is Belady's anomaly. LRU and Optimal are stack algorithms: the set of pages kept with k frames is always a subset of the set kept with k+1 frames. They can never show the anomaly. The table also shows that LRU is not always better than FIFO at one fixed size. Here FIFO beats LRU with 3 frames.
Pattern 6: Segment Translation
A logical address is a pair (segment, offset). Look up the segment's base and limit. If offset ≥ limit, the hardware raises a trap. Otherwise the physical address is base + offset.
| Segment | Base | Limit |
|---|---|---|
| 0 | 1000 | 400 |
| 1 | 6300 | 400 |
| 2 | 4300 | 1100 |
- (2, 500): 500 < 1100, so the physical address is 4300 + 500 = 4800.
- (1, 450): 450 ≥ 400, so this is a protection violation and traps.
- (0, 400): 400 is not less than 400, so this also traps. The limit is a count of bytes, so valid offsets run from 0 to limit − 1.
For segmented paging, the offset inside the segment is itself split into page number and page offset. The segment table then points to a page table for that segment.
Pattern 7: Placement in Contiguous Memory
Take free holes of 100, 500, 200, 300 and 600 KB in address order, and requests of 212, 417, 112 and 426 KB in that order.
| Strategy | 212 | 417 | 112 | 426 |
|---|---|---|---|---|
| First fit | 500 hole | 600 hole | 288 left from the 500 hole | no hole fits |
| Best fit | 300 hole | 500 hole | 200 hole | 600 hole |
| Worst fit | 600 hole | 500 hole | 388 left from the 600 hole | no hole fits |
Best fit places all four requests here, while the other two strategies fail on the last one. This is not a general rule. Recompute the hole list after every placement. The unused remainder of a hole is external fragmentation. Waste inside a fixed-size partition or frame is internal fragmentation.
Pattern 8: Thrashing and the Working Set
The working set WS(t, Δ) is the set of pages referenced in the last Δ references. If the sum of all processes' working-set sizes exceeds the number of frames, the system thrashes. Recognize the symptom in the wording: CPU utilization is low, the paging disk is busy, and the OS has just raised the degree of multiprogramming.
The correct reaction is to suspend one or more processes, which lowers the demand and frees frames. Adding more processes makes it worse. Installing a faster CPU does not help either. A bigger Δ gives a larger working set and a more conservative estimate.
Common Mistakes
- Using bits where bytes are needed. PTE size is in bytes, address widths are in bits.
- Forgetting the extra memory access for the data after a page-table walk.
- Treating the optimal and LRU stack property as true for FIFO.
- Counting a hit in FIFO as a refresh of the page's position.
- Ignoring the "valid bit" or protection bits in PTE size when the question says each entry holds extra flags. Add them to the frame number bits before rounding up to bytes.
Exercise
A system has a 32-bit virtual address, 8 KB pages and 4-byte page table entries, with a single-level page table. How large is the page table?
Show answer
The offset is 13 bits, so the page number is 32 − 13 = 19 bits. The table has 2^19 entries. At 4 bytes each, the size is 2^21 bytes = 2 MB.
Exercise
A two-level page table is used with a TLB hit ratio of 0.8, a TLB lookup time of 20 ns and a memory access time of 100 ns. What is the effective access time?
Show answer
A hit costs 20 + 100 = 120 ns. A miss costs 20 + 2×100 + 100 = 320 ns. EAT = 0.8 × 120 + 0.2 × 320 = 96 + 64 = 160 ns.
Exercise
A segment has base 2000 and limit 600. What happens for the logical addresses (offset values) 599 and 600 in this segment?
Show answer
Offset 599 is below the limit, so it maps to physical address 2599. Offset 600 is not below the limit, so the hardware traps with a protection violation.