Operating System
Comprehensive exam revision notes for BCA-205N Unit 3 (Memory Management & Virtual Memory Architecture). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Operating System
Memory Management & Virtual Memory Architecture
Core Definitions & Key Concepts
Logical Address and Physical Address
A Logical Address (virtual address) is generated by the CPU while executing a program instruction. It exists purely in the logical address space abstraction. A Physical Address is the actual hardware memory address loaded onto the memory bus to access a specific RAM chip byte. The hardware MMU (Memory Management Unit) translates logical addresses to physical addresses at runtime.
Internal Fragmentation and External Fragmentation
Internal Fragmentation occurs when allocated memory blocks are slightly larger than the requested payload; the unused space inside the allocated partition is wasted (characteristic of fixed partitioning and paging). External Fragmentation occurs when total free memory is sufficient to satisfy an allocation request, but it is broken up into tiny non-contiguous blocks scattered across RAM (characteristic of variable partitioning and segmentation).
What is a Translation Lookaside Buffer (TLB)
A TLB is an ultra-fast hardware associative associative cache inside the MMU that stores recently used page-to-frame translations. It allows single-cycle physical address translation, avoiding the need for an extra memory access to read the Page Table from RAM.
What is Demand Paging and what is a Page Fault
Demand Paging is a virtual memory scheme where pages of a program are loaded into physical RAM only when demanded during execution (lazy swapping).
A Page Fault is a hardware trap raised by the MMU when a running process attempts to access a virtual page marked invalid (valid-invalid bit = 0), indicating the page currently resides on disk and must be fetched by the OS.
What is Belady's Anomaly and which page replacement algorithm exhibits it
Belady's Anomaly is the counter-intuitive phenomenon where increasing the number of physical page frames allocated to a process results in an increase (rather than decrease) in the total number of page faults. It occurs in non-stack algorithms like FIFO (First-In, First-Out).
Thrashing in operating systems
Thrashing is a severe performance collapse that occurs when a system spends more time servicing page faults and swapping pages in/out of disk than executing useful user instructions. It happens when the sum of active working sets exceeds available physical memory.
Conceptual & Architectural Analysis
The step-by-step procedure of Page Fault Handling by the Operating System with a diagram
Page Fault Handling Mechanism
+---------------+ 1. Memory Reference
| CPU |-------------------------------------> [ Page Table ]
+---------------+ |
^ | 2. Invalid Bit Trap
| v
| [ Operating System ]
| |
| | 3. Locate Page on Backing Store
| v
| 6. Restart [ Disk Storage ]
| Instruction |
| | 4. Read Page into Free Frame
| v
[ Physical RAM ] <------------------------------------+-------+
Frame Allocated | 5. Reset Valid Bit in Page TableStep-by-Step Sequence:
- Memory Access: CPU accesses logical address. MMU checks Page Table and finds
valid bit = 0. - Trap to Kernel: MMU signals a Page Fault Trap to the OS kernel.
- Context Save: Kernel saves current process CPU registers and state.
- Locate Backing Store: OS determines the target page's exact sector location on swap disk.
- Disk I/O: OS finds a free physical frame in RAM (or executes page replacement if RAM is full) and initiates disk I/O to copy the page. The faulted process is placed in the Waiting queue.
- Update Page Table: On I/O completion, OS records the new physical frame number in the Page Table and flips the valid bit to
1. - Instruction Restart: Process moves to Ready Queue and resumes execution, restarting the exact instruction that triggered the fault.
Compare Paging and Segmentation memory management architectures
Paging vs Segmentation
| Feature | Paging | Segmentation |
|---|---|---|
| Concept | Memory partitioned into fixed-size physical blocks (Frames) and equal logical blocks (Pages). | Program divided into variable-sized logical segments reflecting programmer modules (Code, Stack, Heap, Symbol Table). |
| Address Structure | One-dimensional address decomposed into (Page Number, Page Offset). | Two-dimensional address consisting of (Segment Number, Segment Offset). |
| Visibility | Completely invisible to programmer/compiler (handled by MMU/OS). | Visible to programmer/compiler as distinct functional units. |
| Fragmentation | Suffers from internal fragmentation (on the last page); zero external fragmentation. | Suffers from external fragmentation as segments dynamically grow/shrink; zero internal fragmentation. |
| Protection & Sharing | Uniform protection bits per page (read/write/exec). Sharing arbitrary pages is awkward. | Natural unit of protection and code sharing (e.g., entire shared library segment can be marked read-only). |
Calculate the Effective Memory Access Time (EMAT) given TLB hit ratio and access latencies
Effective Memory Access Time (EMAT) Formula & Derivation
Let:
- \alpha = TLB Hit Ratio (fraction of translations found in TLB)
- t_{tlb} = TLB lookup time
- t_{m} = Main memory access time
1. Case: TLB Hit (Probability \alpha):
Physical frame address is resolved directly inside TLB. Only one main memory access is needed to fetch the actual operand: $Time_{hit} = t_{tlb} + t_m$
2. Case: TLB Miss (Probability 1 - \alpha):
MMU searches TLB, misses, accesses the Page Table in main memory, updates TLB, and then accesses main memory for the operand: $Time_{miss} = t_{tlb} + 2 \cdot t_m$
Effective Memory Access Time Formula:
$EMAT = \alpha \cdot (t_{tlb} + t_m) + (1 - \alpha) \cdot (t_{tlb} + 2 t_m) = t_{tlb} + (2 - \alpha) \cdot t_m$
Example Calculation:
If TLB access time is 20 ns, memory access time is 100 ns, and TLB hit ratio is 90\% (\\alpha = 0.90): $EMAT = 20 + (2 - 0.90) × 100 = 20 + 1.10 × 100 = \mathbf{130 ns} *(Compared to 200\text{ ns}$ without TLB, EMAT is reduced by 35%).*
In-Depth Analysis & Implementation
Given the page reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 for a memory system with 3 physical frames, calculate the total number of page faults using: (a) FIFO, (b) Optimal (OPT), and (c) LRU Page Replacement Algorithms. Demonstrate Belady's Anomaly
Numerical Evaluation of Page Replacement Algorithms
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Number of Frames: 3
(a) First-In, First-Out (FIFO)
| Page | Frame 1 | Frame 2 | Frame 3 | Status |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault (1) |
| 0 | 7 | 0 | - | Fault (2) |
| 1 | 7 | 0 | 1 | Fault (3) |
| 2 | 2 (replaces 7) | 0 | 1 | Fault (4) |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 3 (replaces 0) | 1 | Fault (5) |
| 0 | 2 | 3 | 0 (replaces 1) | Fault (6) |
| 4 | 4 (replaces 2) | 3 | 0 | Fault (7) |
| 2 | 4 | 2 (replaces 3) | 0 | Fault (8) |
| 3 | 4 | 2 | 3 (replaces 0) | Fault (9) |
| 0 | 0 (replaces 4) | 2 | 3 | Fault (10) |
| 3 | 0 | 2 | 3 | Hit |
| 2 | 0 | 2 | 3 | Hit |
| 1 | 0 | 1 (replaces 2) | 3 | Fault (11) |
| 2 | 0 | 1 | 2 (replaces 3) | Fault (12) |
| 0 | 0 | 1 | 2 | Hit |
| 1 | 0 | 1 | 2 | Hit |
| 7 | 7 (replaces 0) | 1 | 2 | Fault (13) |
| 0 | 7 | 0 (replaces 1) | 2 | Fault (14) |
| 1 | 7 | 0 | 1 (replaces 2) | Fault (15) |
- Total Page Faults (FIFO, 3 frames) = 15
(b) Optimal Page Replacement (OPT)
Replaces the page that will not be used for the longest period in the future.
| Page | Frame 1 | Frame 2 | Frame 3 | Status |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault (1) |
| 0 | 7 | 0 | - | Fault (2) |
| 1 | 7 | 0 | 1 | Fault (3) |
| 2 | 2 (replaces 7) | 0 | 1 | Fault (4) |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 (replaces 1) | Fault (5) |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 4 (replaces 2) | 0 | 3 | Fault (6) |
| 2 | 2 (replaces 4) | 0 | 3 | Fault (7) |
| 3 | 2 | 0 | 3 | Hit |
| 0 | 2 | 0 | 3 | Hit |
| 3 | 2 | 0 | 3 | Hit |
| 2 | 2 | 0 | 3 | Hit |
| 1 | 2 | 0 | 1 (replaces 3) | Fault (8) |
| 2 | 2 | 0 | 1 | Hit |
| 0 | 2 | 0 | 1 | Hit |
| 1 | 2 | 0 | 1 | Hit |
| 7 | 7 (replaces 2) | 0 | 1 | Fault (9) |
| 0 | 7 | 0 | 1 | Hit |
| 1 | 7 | 0 | 1 | Hit |
- Total Page Faults (OPT, 3 frames) = 9 (Optimal lower bound)
(c) Least Recently Used (LRU)
Replaces the page that has not been referenced for the longest duration in past references.
| Page | Frame 1 | Frame 2 | Frame 3 | Status |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault (1) |
| 0 | 7 | 0 | - | Fault (2) |
| 1 | 7 | 0 | 1 | Fault (3) |
| 2 | 2 (replaces 7) | 0 | 1 | Fault (4) |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 (replaces 1) | Fault (5) |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 4 (replaces 2) | 0 | 3 | Fault (6) |
| 2 | 4 | 0 | 2 (replaces 3) | Fault (7) |
| 3 | 4 | 3 (replaces 0) | 2 | Fault (8) |
| 0 | 0 (replaces 4) | 3 | 2 | Fault (9) |
| 3 | 0 | 3 | 2 | Hit |
| 2 | 0 | 3 | 2 | Hit |
| 1 | 1 (replaces 0) | 3 | 2 | Fault (10) |
| 2 | 1 | 3 | 2 | Hit |
| 0 | 1 | 0 (replaces 3) | 2 | Fault (11) |
| 1 | 1 | 0 | 2 | Hit |
| 7 | 1 | 0 | 7 (replaces 2) | Fault (12) |
| 0 | 1 | 0 | 7 | Hit |
| 1 | 1 | 0 | 7 | Hit |
- Total Page Faults (LRU, 3 frames) = 12
Comparative Summary Table:
| Algorithm | Page Faults | Performance Ranking |
|---|---|---|
| Optimal (OPT) | 9 | 1 (Theoretical Benchmark) |
| LRU | 12 | 2 (Realistic High Performance) |
| FIFO | 15 | 3 (Poorest Performance) |
Thrashing in detail. What causes it? How does the Working Set Model proposed by Peter Denning eliminate thrashing
Thrashing and the Working-Set Model
1. What is Thrashing?
Thrashing is a catastrophic system state where the operating system spends virtually all its time moving pages back and forth between physical RAM and disk swap space, with CPU utilization collapsing toward near zero.
CPU
Utilization ^
| Optimal
| Operating
| Point
| / \
| / \
| / \ <--- Thrashing Zone:
| / \ CPU utilization drops steeply!
| / \
+-----+------------+-------------> Degree of Multiprogramming2. Root Cause of Thrashing:
- As the OS increases the Degree of Multiprogramming to boost CPU utilization, physical RAM frames are divided among more processes.
- Eventually, each process is allocated fewer frames than the minimum number required to support its active locality of execution.
- Process P_i faults and waits for disk I/O. The short-term scheduler notices CPU idle time and mistakenly increases multiprogramming by admitting another process.
- Total demand for page frames explodes; page queues back up; CPU utilization plummets to near zero.
3. The Working-Set Model (Peter Denning):
The Working Set Model uses the principle of locality of reference to track the exact set of pages an active process is referencing over a sliding time window.
- Working-Set Window ($\Delta$): A fixed parameter defining the past number of page references examined.
- Working-Set $WSS_i(t,\Delta)$: The set of distinct virtual pages referenced by process P_i during the most recent \Delta references.
Reference Stream: ... 2 6 1 5 7 7 7 7 5 1 [ t ]
|<------- Delta = 10 ------->|
Working Set WSS(t, 10) = { 1, 2, 5, 6, 7 } --> Size = 5 pagesPrevention Algorithm:
- The OS calculates total memory frame demand:
$D = \sum_{i} |WSS_i|$
- Let M be total available physical frames in RAM.
- If $D \le M$: All processes have their entire working set resident in memory. The system runs at peak efficiency with minimal page faults.
- If $D > M$: Total demand exceeds memory capacity! Thrashing is imminent. The OS suspends (swaps out) an entire process, frees its allocated frames to other processes, and lowers D until D ≤ M.
Important Exam Points & Quick Revision
Examiner Insights & University Exam Strategy:
- Guaranteed 10/15-Mark Problem: Numerical comparison of FIFO, LRU, and OPT page replacement algorithms is a staple on MJPRU semester examinations. Construct full tracking tables showing the state of every frame at every step and count faults accurately.
- 5-Mark Derivation: Be ready to write the EMAT formula (t_{tlb} + (2-\alpha)t_m) with step-by-step logic.
- Conceptual Clarity: Distinguish Internal vs External Fragmentation, and explain how Paging eliminates external fragmentation.
100% left on this page — finishing it closes the gap for Memory Management & Virtual Memory Architecture.
Memory Management & Virtual Memory Architecture still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.