Demand Paging in Operating Systems: Implementing Virtual Memory Efficiently

Understand demand paging, a technique for implementing virtual memory. This guide explains how demand paging loads pages only when needed, the concept of page faults, and the challenges of excessive paging (thrashing), providing strategies for efficient virtual memory management.



Demand Paging in Operating Systems

Understanding Demand Paging

Virtual memory allows programs larger than available RAM to run by storing parts of a program on disk and loading them into RAM only when needed. Demand paging is a technique used to implement virtual memory. Instead of loading an entire program into RAM at once, it loads pages only when they're accessed for the first time. This significantly reduces the amount of RAM needed and avoids loading unneeded parts of a program.

Page Faults

A page fault (or page miss) occurs when a process tries to access a page that's not currently in main memory (RAM). When this happens, the OS needs to load the requested page from secondary storage (typically a hard disk), often using a page replacement algorithm to make room in RAM for the new page. A high page fault rate significantly slows down program execution because of the time it takes to retrieve pages from disk.

Thrashing

Thrashing is an extreme case of excessive paging where the system spends most of its time swapping pages between RAM and disk. This happens when a process needs so many pages that it constantly causes page faults. In this situation, the CPU becomes largely occupied with managing page faults rather than executing program instructions; this results in a drastic reduction in system performance, sometimes leading to a system freeze. The effective access time—the average time it takes to access a word in memory—becomes dominated by the disk access time.

The effective access time (EAT) can be calculated as:

EAT = PF * S + (1 - PF) * ma

Where:

  • PF = Page fault rate
  • S = Service time (time to load a page from disk)
  • ma = Main memory access time