BCS401 · Operating system · Unit 4
Memory Management
Unit Scope & Exam Weightage: This unit covers bare machine and resident monitor, fixed and variable multiprogramming partitions, protection, paging and TLB address translation, segmentation and paged segmentation, virtual memory and demand paging, page replacement algorithms (LRU/Optimal), thrashing, cache memory organization and locality of reference. It is highly numerical: AKTU repeatedly asks 7-mark page-fault and partition-allocation problems.
Importance-Wise Roadmap (BCS401 Unit 4)
| Priority | Exact Syllabus Topic | PYQ Evidence | Marks Pattern | Tag |
|---|---|---|---|---|
| 1 | Page replacement algorithms (LRU, Optimal, lookahead variant) | 2024-25, 2025-26 | 7m × 3 | Numerical core |
| 2 | Paging + TLB + page-table address translation | 2024-25, 2025-26 | 7m × 2 | Diagram |
| 3 | Variable/fixed partition allocation (first/best/worst fit) | 2023-24, 2024-25, 2025-26 | 7m × 3 | Must solve |
| 4 | Virtual memory & demand paging concepts | 2023-24 | 7m | Theory |
| 5 | Paging vs. segmentation, paged segmentation | 2023-24 | 7m | Comparison |
| 6 | Locality of reference, resident monitor, thrashing, cache | 2023-24, 2025-26 | 2m | Quick marks |
Memory-specific examiner guidance: In page-fault tables, show the frame contents after each reference and mark hits explicitly; the evaluator gives step marks for the frame column. For address split questions, first compute and write the bit fields before touching the numbers.
1. From Bare Machine to Multiprogramming
1.1 Bare Machine and Resident Monitor
A bare machine has no software support: the programmer loads a job directly into memory, sets switches, and controls the device. A resident monitor is a small program permanently kept in a protected part of memory; it reads the next job from input, loads it, transfers control, and regains control when the job ends. It automates job-to-job transition and is the ancestor of the batch operating system.
PYQ (2023-24, 2 marks): "Define a resident monitor?" A monitor program that remains resident in main memory and controls the automatic sequencing of jobs — loading, starting and unloading each job without operator intervention.
1.2 Fixed vs. Variable Partitions
| Feature | Fixed partitioning | Variable partitioning |
|---|---|---|
| Partition sizes | Fixed at system generation, may be equal or unequal | Created dynamically to fit each job |
| Internal fragmentation | Yes: unused space inside a partition | No internal fragmentation |
| External fragmentation | No (partitions cannot be merged) | Yes: scattered small holes |
| Placement | Job assigned to one partition | First-fit / best-fit / worst-fit search |
| Compaction | Not needed | Needed to coalesce holes |
| Simplicity | Very simple | More bookkeeping (free/allocated lists) |
PYQ (2023-24, 7 marks): "Compare and contrast multiprogramming with fixed partitions and variable partitions?" Use the table; add that fixed partitions suit predictable batch workloads, while variable partitions improve utilization for jobs of widely differing sizes at the cost of fragmentation and compaction overhead.
PYQ (2024-25, 7 marks): "Discuss multiprogramming with variable partitions and how it improves memory utilization." Explain the free-space list, dynamic allocation exactly matching job size, no internal fragmentation, and the need to merge adjacent free holes; mention compaction to counter external fragmentation.
1.3 Solved: First-Fit, Best-Fit, Worst-Fit
PYQ (2025-26, 7 marks): "Given partitions 100 KB, 500 KB, 200 KB, 300 KB, 600 KB (in order), place processes 212 KB, 417 KB, 112 KB, 426 KB (in order) using first-fit, best-fit and worst-fit. Which is most efficient?"
First-fit (first hole large enough):
| Process | Chosen hole | Leftover holes after placement |
|---|---|---|
| 212 | 500 → 288 KB left | 100, 288, 200, 300, 600 |
| 417 | 600 → 183 KB left | 100, 288, 200, 300, 183 |
| 112 | 288 → 176 KB left | 100, 176, 200, 300, 183 |
| 426 | cannot fit (largest hole 300) | — |
Best-fit (smallest hole that fits):
| Process | Chosen hole | Leftover holes |
|---|---|---|
| 212 | 300 → 88 KB left | 100, 500, 200, 88, 600 |
| 417 | 500 → 83 KB left | 100, 83, 200, 88, 600 |
| 112 | 200 → 88 KB left | 100, 83, 88, 88, 600 |
| 426 | 600 → 174 KB left | 100, 83, 88, 88, 174 |
Worst-fit (largest hole):
| Process | Chosen hole | Leftover holes |
|---|---|---|
| 212 | 600 → 388 KB left | 100, 500, 200, 300, 388 |
| 417 | 500 → 83 KB left | 100, 83, 200, 300, 388 |
| 112 | 388 → 276 KB left | 100, 83, 200, 300, 276 |
| 426 | cannot fit (largest hole 300) | — |
Conclusion: best-fit is most efficient here — it places all four processes; first-fit and worst-fit each leave the 426 KB process waiting. Best-fit's tiny leftover holes (83, 88 KB) are a drawback in general, but here the large 600 KB hole is preserved for the last process.
Protection schemes: base and limit registers (each logical address checked against bounds), keyed pages/segments, and user/supervisor mode bits prevent a process from touching another process's memory or kernel memory.
2. Paging, Segmentation and Address Translation
2.1 Paging Fundamentals
Paging: a memory-management scheme that divides logical memory into fixed-size pages and physical memory into equally sized frames. A page table maps each logical page number to a physical frame number; the page offset is unchanged during translation. Paging eliminates external fragmentation but may cause internal fragmentation in the last page.
Logical address = page number (p) + page offset (d)
Physical address = frame number (f) + same offset (d)
If page size = bytes, the low bits are the offset and the remaining bits form the page number.
2.2 TLB + Page Table Translation (PYQ 2025-26, 7 marks)
Logical address (32 bits)
+------------------+----------------+
| page number p | offset d (11b) |
+------------------+----------------+
|
v
+-----------+ hit +----------------------+
| TLB |-------->| frame f + offset d |----> physical address
| (p -> f) | +----------------------+
+-----------+
| miss
v
+---------------------------+
| page table (in memory) | f = PTE[p]
| PTE[p] -> frame or fault |
+---------------------------+
|
+--> update TLB, retry; if page not resident -> page fault -> disk
Step-by-step working:
- CPU generates a logical address; MMU splits it into page number and offset .
- The Translation Lookaside Buffer (TLB), a small fully-associative cache of recent pairs, is searched in parallel.
- TLB hit: frame number is obtained immediately; physical address = . No memory reference to the page table.
- TLB miss: page table is consulted (page-table base register + ). The translation is loaded into the TLB, evicting an old entry; then the address is formed.
- If the page-table entry's valid/present bit is 0 → page fault: OS brings the page from disk, updates the PTE and TLB, and restarts the instruction.
- Context switches must flush or tag TLB entries (ASIDs) so translations of another process are not reused.
TLB vs. page table: TLB is fast, small (32–1024 entries), expensive and associative; page table is large, stored in main memory and indexed directly by .
2.3 Solved: Maximum Page-Table Entries (PYQ 2024-25, 7 marks)
"32-bit logical address, 20-bit physical address, page size 2048 bytes, byte-addressable. Maximum number of entries in the page table?"
- Page size bytes → offset = 11 bits.
- Page number bits bits.
- Number of logical pages entries.
- Each entry must hold a physical frame number: physical address 20 bits − 11 offset bits = 9 frame bits, i.e. up to frames (plus valid/protection/dirty bits).
- Physical memory bytes = 1 MB; logical address space bytes = 4 GB.
Common trap: do not use the 20-bit physical address to count page-table entries; entries equal the number of logical pages.
2.4 Paging vs. Segmentation vs. Paged Segmentation
PYQ (2023-24, 7 marks): "Explain the principles of paging and segmentation as memory management techniques, comparing their suitability for different system architectures?"
| Basis | Paging | Segmentation |
|---|---|---|
| Division unit | Fixed-size pages (e.g., 4 KB) | Variable-size logical segments (code, data, stack) |
| Programmer visibility | Invisible to programmer | Segments can be named by the programmer |
| Address | page number + offset | segment number + offset |
| Fragmentation | Internal only | External only |
| Table | Page table | Segment table (base + limit per segment) |
| Protection | Per page | Per segment (natural for code/data separation) |
| Sharing | Per page | Whole segment (e.g., shared library) |
| Suitable for | General-purpose OS, virtual memory | Modular programming, protection-oriented systems |
Paged segmentation: each segment is divided into pages; address = segment number + page number + offset. It combines segmentation's logical protection/sharing with paging's allocation simplicity. Example: MULTICS and IA-32 segmented paging. Segment table entry points to a page table for that segment.
3. Virtual Memory and Demand Paging
Virtual memory: a technique that lets a process execute even when only part of its address space is in main memory. The OS maps virtual (logical) addresses to physical addresses and fetches pages from secondary storage on demand, giving the illusion of a large contiguous private memory.
Demand paging mechanics:
- On a page fault, the OS checks the PTE's valid bit.
- If the reference is invalid → trap (protection violation); if valid but not resident → choose a free frame (or victim).
- Read the page from disk into the frame; update page table and TLB; restart the faulting instruction.
- Effective access time (EAT): , where is page-fault rate and is memory access time. If , ns and fault service = 8 ms: EAT ns — showing why low fault rates matter.
PYQ (2023-24, 7 marks): "Discuss virtual memory concepts, including address translation, page tables, and demand paging mechanisms?" Cover the definition, logical→physical mapping with the page table, valid/dirty/protection bits, the six demand-paging steps, EAT formula, and benefits (more processes in memory, no need for full loading, sharing of code).
PYQ (2025-26, 2 marks): "What is locality of reference in demand paging?" Programs tend to reference the same pages repeatedly in a short interval (temporal locality) and pages near recently referenced ones (spatial locality). Demand paging works because of locality: after a page is faulted in, the next references are likely to hit it, keeping small.
Thrashing: if the OS over-commits memory, a process spends more time paging than executing; page-fault rate rises, CPU utilization drops, and the OS may wrongly increase the degree of multiprogramming. Fix: working-set model — keep each process's active page set resident; suspend a process if its working set cannot be maintained.
Cache memory organization: cache is small, fast memory holding recently used main-memory blocks. Directly mapped (one line per block, cheap but conflicts), fully associative (any line, expensive hardware), set-associative (compromise). Replacement uses LRU/random; write policies are write-through and write-back. Locality makes caches effective — the same principle that makes demand paging viable.
4. Page Replacement Algorithms — Fully Worked PYQs
4.1 Purpose and Policies
PYQ (2024-25, 2 marks): "What is the purpose of a page replacement algorithm?" When a page fault occurs and no free frame exists, it selects the victim page to evict so that the number of future page faults is minimised. Common policies: FIFO (oldest page, Belady's anomaly possible), LRU (least recently used), Optimal/MIN (never used longest into the future — theoretical minimum, unrealisable without future knowledge).
4.2 Solved: LRU and Optimal on 20 References
PYQ (2025-26, 7 marks): "Reference string 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6; 4 frames initially empty. Find faults for LRU and Optimal."
| Ref | LRU frames (left = oldest) | Victim | Fault? | OPT frames | Victim | Fault? |
|---|---|---|---|---|---|---|
| 1 | 1 | – | F | 1 | – | F |
| 2 | 12 | – | F | 12 | – | F |
| 3 | 123 | – | F | 123 | – | F |
| 4 | 1234 | – | F | 1234 | – | F |
| 2 | 1342 | – | hit | 1234 | – | hit |
| 1 | 3421 | – | hit | 1234 | – | hit |
| 5 | 4215 | 3 | F | 1235 | 4 | F |
| 6 | 2156 | 4 | F | 1236 | 5 | F |
| 2 | 1562 | – | hit | 1236 | – | hit |
| 1 | 5621 | – | hit | 1236 | – | hit |
| 2 | 5612 | – | hit | 1236 | – | hit |
| 3 | 6123 | 5 | F | 1236 | – | hit |
| 7 | 1237 | 6 | F | 2367 | 1 | F |
| 6 | 2376 | 1 | F | 2367 | – | hit |
| 3 | 2763 | – | hit | 2367 | – | hit |
| 2 | 7632 | – | hit | 2367 | – | hit |
| 1 | 6321 | 7 | F | 2361 | 7 | F |
| 2 | 6312 | – | hit | 2361 | – | hit |
| 3 | 6123 | – | hit | 2361 | – | hit |
| 6 | 1236 | – | hit | 2361 | – | hit |
- LRU faults = 10 (first four compulsory + 6 replacements).
- Optimal faults = 8 (first four + replacements of 4, 5, 1 and 7).
- Optimal ≤ LRU, as expected — LRU uses past behaviour, Optimal knows the future.
4.3 Solved: Modified Optimal with 4-Reference Lookahead
PYQ (2024-25, 7 marks): "OS correctly predicts only the next 4 references (including current). Reference string 1, 3, 2, 4, 2, 3, 1, 2, 4, 3, 1, 4; 3 frames initially empty. Find page faults."
At each miss, evict a page not appearing in the known 4-reference window; if all appear, evict the one used farthest ahead.
| Ref | Window (current + next 3) | Frames after | Decision | Fault? |
|---|---|---|---|---|
| 1 | 1,3,2,4 | 1 | load | F |
| 3 | 3,2,4,2 | 1 3 | load | F |
| 2 | 2,4,2,3 | 1 3 2 | load | F |
| 4 | 4,2,3,1 | 3 2 4 | evict 1 (not in window) | F |
| 2 | 2,3,1,2 | 3 2 4 | 2 already resident | hit |
| 3 | 3,1,2,4 | 3 2 4 | hit | hit |
| 1 | 1,2,4,3 | 2 4 1 | evict 3 (farthest, index 9) | F |
| 2 | 2,4,3,1 | 2 4 1 | hit | hit |
| 4 | 4,3,1,4 | 2 4 1 | hit | hit |
| 3 | 3,1,4 | 4 1 3 | evict 2 (not in window) | F |
| 1 | 1,4 | 4 1 3 | hit | hit |
| 4 | 4 | 4 1 3 | hit | hit |
Total page faults = 6. (Pure Optimal and standard LRU also give 6 for this short string; the point of the question is applying the finite-window rule, not merely the count.)
4.4 Replacement Policy Summary
| Policy | Victim choice | Faults for §4.2 (4 frames) | Note |
|---|---|---|---|
| FIFO | oldest loaded page | not required here | Belady's anomaly possible |
| LRU | least recently used | 10 | approximates optimal well with locality |
| Optimal | next used farthest in future | 8 | lower bound; not implementable online |
| 4-lookahead | not in next 4 references, else farthest | 6 on §4.3 data | practical approximation of Optimal |
5. Exam-Ready Questions and Practice Set
Model 7-mark answer skeletons
| Question | Required content |
|---|---|
| TLB + page table translation | Split address, TLB hit/miss flow, page fault path, diagram |
| 32-bit/20-bit page table size | offset bits, page-number bits, entries, 9 frame bits |
| Paging vs. segmentation | six-row comparison + paged segmentation note |
| Fixed vs. variable partitions | fragmentation types, placement, compaction |
| First/best/worst fit | three placement tables + efficiency conclusion |
| Virtual memory and demand paging | mapping, page fault steps, EAT, benefits |
| LRU/Optimal faults | full frame table + fault totals |
| Lookahead Optimal | window-based victim table + total faults |
Practice questions (label: practice, not real PYQs)
- Practice: For reference string 7,0,1,2,0,3,0,4,2,3 with 3 frames, simulate FIFO and LRU; demonstrate Belady's anomaly using 4 frames.
- Practice: A 36-bit logical address with 8 KB pages — how many page-table entries? (Answer: .)
- Practice: Explain why paging suffers internal fragmentation while segmentation suffers external fragmentation.
- Practice: With EAT formula, compute the maximum page-fault rate if EAT must stay below 200 ns, ns and fault service = 5 ms.
- Practice: Distinguish a TLB miss from a page fault with a concrete example.
6. Last-Night Survival Checklist (Unit 4 only)
- Bit arithmetic: page size → offset bits; entries . For 32-bit/2 KB → .
- Two fault counts to recall: LRU = 10, Optimal = 8 for the 20-reference PYQ; lookahead example = 6.
- Partition placement: first-fit takes the first hole, best-fit the tightest, worst-fit the largest; show leftover holes after every step.
- TLB diagram: draw logical address → TLB → (miss) page table → physical address, with the page-fault branch to disk.
- Definitions: internal vs. external fragmentation, demand paging, locality, thrashing, working set — one line each.
Final memory-management reminder: For every numerical, write the initial state, the decision rule, and the final table. In the partition question, the winning algorithm is the one that places all processes — best-fit here — not the one with the smallest single leftover hole.