Top Banner Ad

BCS401 — Unit 4: Memory Management

Table of Contents (20 sections)

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 log⁡2(page size)\log_2(\text{page size}) 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 = 2k2^k bytes, the low kk 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:

  1. CPU generates a logical address; MMU splits it into page number pp and offset dd.
  2. The Translation Lookaside Buffer (TLB), a small fully-associative cache of recent (p,f)(p,f) pairs, is searched in parallel.
  3. TLB hit: frame number is obtained immediately; physical address = f×page size+df \times \text{page size} + d. No memory reference to the page table.
  4. TLB miss: page table is consulted (page-table base register + p×PTE sizep \times \text{PTE size}). The translation is loaded into the TLB, evicting an old entry; then the address is formed.
  5. 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.
  6. 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 pp.

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 =2048=211= 2048 = 2^{11} bytes → offset = 11 bits.
  • Page number bits =32−11=21= 32 - 11 = 21 bits.
  • Number of logical pages =221=2,097,152= 2^{21} = \mathbf{2,097,152} entries.
  • Each entry must hold a physical frame number: physical address 20 bits − 11 offset bits = 9 frame bits, i.e. up to 29=5122^9 = 512 frames (plus valid/protection/dirty bits).
  • Physical memory =220= 2^{20} bytes = 1 MB; logical address space =232= 2^{32} 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:

  1. On a page fault, the OS checks the PTE's valid bit.
  2. If the reference is invalid → trap (protection violation); if valid but not resident → choose a free frame (or victim).
  3. Read the page from disk into the frame; update page table and TLB; restart the faulting instruction.
  4. Effective access time (EAT): EAT=(1−p)⋅ma+p⋅page-fault service timeEAT = (1-p)\cdot ma + p\cdot\text{page-fault service time}, where pp is page-fault rate and mama is memory access time. If p=0.001p=0.001, ma=100ma=100 ns and fault service = 8 ms: EAT ≈0.999(100)+0.001(8,000,000)≈8100\approx 0.999(100) + 0.001(8{,}000{,}000) \approx 8100 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 pp 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, 2212^{21} 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)

  1. 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.
  2. Practice: A 36-bit logical address with 8 KB pages — how many page-table entries? (Answer: 236−13=2232^{36-13}=2^{23}.)
  3. Practice: Explain why paging suffers internal fragmentation while segmentation suffers external fragmentation.
  4. Practice: With EAT formula, compute the maximum page-fault rate if EAT must stay below 200 ns, ma=100ma=100 ns and fault service = 5 ms.
  5. Practice: Distinguish a TLB miss from a page fault with a concrete example.

6. Last-Night Survival Checklist (Unit 4 only)

  1. Bit arithmetic: page size 2k2^k → kk offset bits; entries =2logical bits−k=2^{\text{logical bits}-k}. For 32-bit/2 KB → 221=2,097,1522^{21}=2{,}097{,}152.
  2. Two fault counts to recall: LRU = 10, Optimal = 8 for the 20-reference PYQ; lookahead example = 6.
  3. Partition placement: first-fit takes the first hole, best-fit the tightest, worst-fit the largest; show leftover holes after every step.
  4. TLB diagram: draw logical address → TLB → (miss) page table → physical address, with the page-fault branch to disk.
  5. 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.