Topic 8.3 · Paging, Segmentation & Virtual Memory

Virtual Memory, Demand Paging & Page Fault Handling

4 min read1 question to test yourself

1Definition

Virtual Memory is a memory management technique that enables the execution of processes that are not completely loaded into physical RAM by treating a section of secondary disk storage (Swap-Space) as an extension of main memory. Demand Paging is the primary implementation of virtual memory where individual pages are swapped into RAM only when demanded during execution. A Page Fault is a hardware interrupt raised when a program accesses a valid logical page that is not currently loaded into physical RAM.

2Simple Explanation

Historically, if a computer had 4GB of RAM, you could not launch a 10GB application. Virtual Memory removes this barrier! It creates the illusion of an infinite RAM pool.

Instead of loading a whole 10GB program into RAM at startup, the OS uses a Lazy Pager. The Lazy Pager brings zero pages into RAM initially (Pure Demand Paging). When the CPU executes Instruction 1, it discovers the page isn't in RAM. The MMU raises a Page Fault Trap. The OS pauses the app, reads the missing 4KB page from the hard drive, places it into an open RAM frame, updates the Page Table, and restarts the instruction. The user never notices, except for a microsecond disk read pause!

3Real-Life Example / Analogy

Streaming a Movie on Netflix: When you watch a 2-hour 4K movie (15GB file), Netflix does not force you to download all 15GB to your phone's memory before hitting play. It downloads only the current 5-second scene (Demand Paging). As you watch, it streams future scenes on demand. If your internet stutters and a scene isn't ready, the video pauses briefly (Page Fault), loads the buffer, and resumes.

4Diagram / Visual Representation

Invalid · page fault

CPU references a page

Page table checks valid / invalid bit

Trap to OS kernel

Locate required page on secondary storage

Find free frame or select victim page

Load page into physical RAM

Update page table / TLB

Restart interrupted instruction

5Working / How It Works

  • Valid-Invalid Bit Scheme:
    • Bit = 1 (Valid): Page is legal and currently loaded in physical RAM frame.
    • Bit = 0 (Invalid): Page is either illegal OR valid but currently stored on secondary swap disk.
  • Detailed 6-Step Page Fault Handling Sequence:
    1. CPU generates logical address access to page marked Invalid (0) in Page Table.
    2. MMU hardware catches invalid bit and triggers a Page Fault Trap to OS kernel.
    3. OS checks process internal PCB table to verify if access is legal or illegal memory access (If illegal -> Abort process).
    4. OS locates missing page on secondary Swap-Space disk and allocates a free frame from the Free-Frame List.
    5. OS executes disk I/O operation to copy missing page into the allocated RAM frame.
    6. Disk read completes -> OS updates Page Table entry bit to Valid (1) -> OS restarts the interrupted CPU instruction seamlessly.

6Types / Components / Characteristics

  • Pure Demand Paging: Starting process execution with zero pages in RAM. The very first instruction immediately triggers a page fault.
  • Locality of Reference: Principle stating that program execution tends to cluster around specific localized memory regions (loops, functions) for periods of time, allowing demand paging to achieve near-RAM speed performance.
  • Swapper vs. Pager:
    • Swapper: Manipulates whole processes (Medium-Term Scheduler).
    • Pager: Manipulates individual pages of a process.

7Solved Examples / Problems

Problem: Calculate the Effective Access Time (EAT) of a Demand Paging system given:

  • Main Memory Access Time = 100 ns
  • Page Fault Service Time = 10 milliseconds (10,000,000 ns10,000,000\text{ ns})
  • Page Fault Rate p=0.001p = 0.001 (1 page fault per 1,000 accesses).

Solution: EAT=(1−p)×Memory Access Time+p×Page Fault Service Time\text{EAT} = (1 - p) \times \text{Memory Access Time} + p \times \text{Page Fault Service Time} EAT=(1−0.001)×100 ns+(0.001)×10,000,000 ns\text{EAT} = (1 - 0.001) \times 100\text{ ns} + (0.001) \times 10,000,000\text{ ns} EAT=(0.999×100)+10,000=99.9+10,000=10,099.9 ns (≈10.1μs)\text{EAT} = (0.999 \times 100) + 10,000 = 99.9 + 10,000 = \mathbf{10,099.9 \text{ ns } (\approx 10.1 \mu\text{s})}

  • Analysis: A page fault rate of just 0.1% slows effective memory access from 100ns to 10,100ns (a 100x slowdown!).

8Advantages, Disadvantages & Applications

  • Virtual Memory Advantages: Programs can be vastly larger than physical RAM; increases Degree of Multiprogramming and CPU utilization.
  • Disadvantages: Page fault disk I/O introduces memory latency; system can suffer from Thrashing if RAM is insufficient.

9Important Points to Remember

  • A 0 Valid/Invalid bit means the page is either out-of-bounds OR valid but residing on swap disk.
  • Page Fault handling requires restarting the interrupted instruction.

10Test Your Understanding

  1. MCQ: What hardware signal is triggered when a process attempts to access a page marked with an Invalid (0) bit in the Page Table?
    • A) System Call Interrupt
    • B) Page Fault Trap
    • C) Segmentation Fault Trap
    • D) Context Switch Signal
    Show answer
    B.
  2. Conceptual Question: Detail the exact 6-step sequence executed by the OS to service a Page Fault.
  3. Application Question: Calculate the Effective Access Time (EAT) for a system with p=0.002p = 0.002 and Page Fault Service Time = 5 ms5\text{ ms}.

Finished this topic?

0 of 27 topics read in Operating Systems

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic