Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Demand Paging: Data Structures

Lesson 30 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

Introduction

Demand Paging is a more efficient memory management technique than swapping. Instead of moving entire processes, demand paging moves individual pages (fixed-size memory blocks, typically 4 KB) between memory and disk. Pages are loaded on demand — only when the process actually accesses them.

Why Demand Paging Over Swapping?

AspectSwappingDemand Paging
Unit of transferEntire processSingle page (4 KB)
GranularityCoarseFine
I/O overheadHigh (large transfers)Low (small transfers)
Startup timeMust load entire processLoad only needed pages
Memory utilizationWastes memory (unused parts loaded)Efficient (only active pages in memory)
Suitable forSmall processes, limited hardwareLarge processes, modern hardware

Key Data Structures for Demand Paging

---

1. Page Table Entry (PTE)

Each process has a page table that maps virtual pages to physical frames. Each entry in the page table is a Page Table Entry (PTE).

PTE Fields:

FieldDescription
Physical Frame NumberThe physical memory frame where this page is loaded
Valid Bit (V)1 = page is in physical memory; 0 = page is NOT in memory (on disk)
Protection BitsRead / Write / Execute permissions
Reference Bit (R)Set by hardware when the page is accessed (read or write)
Modify Bit (M) / Dirty BitSet by hardware when the page is written to
Copy-on-Write BitPage is shared; copy only when a process tries to write
AgeUsed by page replacement algorithms (how long since last access)
Swap AddressLocation on the swap device where this page is stored (when not in memory)

Example Page Table:

Virtual  Physical   Valid  Ref  Modify  Protection
Page #   Frame #
  0        12        1      1     0      R-X       (code page, in memory)
  1        --        0      0     0      R-X       (code page, on disk)
  2        45        1      1     1      RW-       (data page, modified)
  3        --        0      0     0      RW-       (data page, on disk)
  4        78        1      0     0      RW-       (stack page, in memory)

---

2. Page Frame Data Table (pfdata)

The kernel maintains a page frame data table (pfdata) — a global table with one entry per physical memory frame. It describes the current state of every frame in physical memory.

pfdata Entry Fields:

FieldDescription
StateFree, on free list, in use, on hash queue
Reference CountNumber of processes referencing this frame
Logical Device / BlockIdentifies the data on disk (for page-in/page-out)
Page NumberVirtual page number currently mapped to this frame
PointersHash queue pointers, free list pointers

Pfdata States:

┌─────────────────┐
│    In Use        │  → Being used by a process
├─────────────────┤
│    On Free List  │  → Available for allocation
├─────────────────┤
│    On Hash Queue │  → Contains valid data, can be reclaimed
└─────────────────┘
  • When a page is freed, its frame goes on the free list but the data remains.
  • If the page is needed again before the frame is reused, it can be reclaimed from the hash queue without disk I/O — this is called page reclaim.

---

3. Disk Block Descriptor

Each page in a process's virtual address space has a disk block descriptor that tells the kernel where to find the page's data on disk.

Disk Block Descriptor Fields:

FieldDescription
Swap DeviceWhich swap device holds this page
Block NumberBlock number on the swap device
TypeSwap file, executable file, or demand-zero

Types of Pages:

  • Demand Fill (from executable): Page is loaded from the executable file on disk.
  • Demand Zero: Page is initialized with zeros (BSS, new heap pages).
  • Swap: Page has been written to the swap device previously.

---

4. Swap Use Table

  • Tracks the number of references to each page on the swap device.
  • When a page is copied to swap, the reference count is set.
  • When all references are gone (all processes have loaded/freed the page), the swap space is freed.

Relationship Between Data Structures

Process Page Table              pfdata Table
┌────────────────────┐         ┌────────────────────┐
│ VPage 0 → Frame 12│────────►│ Frame 12: In Use   │
│ VPage 1 → (on disk)│         │   ref_count = 1    │
│ VPage 2 → Frame 45│────────►│ Frame 45: In Use   │
│ VPage 3 → (on disk)│         │   ref_count = 1    │
│ VPage 4 → Frame 78│────────►│ Frame 78: In Use   │
└────────────────────┘         └────────────────────┘

Disk Block Descriptors          Swap Device
┌────────────────────┐         ┌────────────────────┐
│ VPage 1 → Block 200│────────►│ Block 200: data    │
│ VPage 3 → Block 340│────────►│ Block 340: data    │
└────────────────────┘         └────────────────────┘

Summary

  • Page Table Entries map virtual pages to physical frames with status bits (valid, reference, modify).
  • pfdata tracks the state of every physical memory frame.
  • Disk Block Descriptors tell the kernel where to find each page on disk.
  • Swap Use Table tracks references to pages on the swap device.
  • Page reclaim allows reuse of freed pages without disk I/O if the frame hasn't been overwritten.