Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 4.5: Virtual memory and demand paging

Running programs that are only partly in memory: demand paging, the six steps of handling a page fault, effective access time, and copy-on-write.

Module 4 1 hour Theory CO4
← Before
4.4 Segmentation
Segment tables
You are here
4.5 Virtual memory and demand paging
Module 4 · Memory and File Management
Next →
4.6 Page replacement
FIFO, OPT, LRU, thrashing

Learning objectives

By the end of this session you should be able to:

  • State the benefits of virtual memory.
  • Explain demand paging and the role of the valid–invalid bit.
  • List the steps the OS takes to handle a page fault.
  • Compute effective access time for a given page-fault rate.
  • Explain copy-on-write and why it speeds up fork().

Lecture notes

1. Why virtual memory?

Programs rarely need all their code at once: error-handling routines, rarely used features and over-sized arrays sit unused. If a program could run with only part of it in memory:

  • programs are no longer limited by the size of physical memory;
  • more programs fit in memory at the same time → higher CPU utilisation and throughput;
  • less I/O is needed to load or swap a program, so it starts faster.
Virtual memory

The separation of the user's logical memory from physical memory, allowing an extremely large virtual address space to be provided on a smaller physical memory. Only the parts in use need to be in RAM; the rest stays on disk.

2. Demand paging

Load a page only when it is needed (on demand). A lazy swapper — called a pager — never brings a page into memory unless it is referenced. The page table's valid–invalid bit now means: valid = page is legal and in memory; invalid = page is either not legal or currently on disk.

3. Handling a page fault

Accessing a page marked invalid traps to the OS — a page fault.

The process executes load M; the MMU finds page of M marked invalid.

Hardware traps to the OS (an exception, Session 1.4). The OS checks an internal table: invalid reference → terminate; valid but not in memory → page it in.

Find a free frame (if none, run page replacement — Session 4.6).

Schedule a disk read of the page into the frame. The process waits (state waiting); the CPU runs someone else.

When the read completes (I/O interrupt), update the page table: frame number and valid.

Restart the instruction that faulted. It now finds the page in memory as if it had always been there.

Pure demand paging starts a process with no pages in memory — even the first instruction faults. Thanks to locality of reference (programs use a small set of pages at a time), the fault rate quickly drops.

4. Performance of demand paging

Let p be the page-fault rate (0 ≤ p ≤ 1):

\[ \text{EAT} = (1-p) \times \text{memory access} + p \times \text{page-fault service time} \]

Worked example

Memory access = 200 ns, page-fault service time = 8 ms = 8 000 000 ns.

EAT = (1 − p) × 200 + p × 8 000 000 = 200 + 7 999 800 p ns.

If 1 access in 1000 faults (p = 0.001): EAT ≈ 200 + 7 999.8 ≈ 8.2 µs — a slowdown by a factor of 40!

To keep degradation below 10% (EAT < 220 ns): 7 999 800 p < 20 → p < 0.0000025 — fewer than 1 fault in 400 000 accesses. Page-fault rate must be kept extremely low.

5. Copy-on-write

Recall that fork() copies the parent's address space (Session 2.2) — wasteful if the child immediately calls exec(). With copy-on-write (COW), parent and child initially share all pages, marked read-only. Only when either process writes to a shared page is a private copy of that page made. Unmodified pages are never copied.

Child's page table points to the parent's frames; all marked copy-on-write.

parent→page A · page B · page C←child

The child writes to page C → protection fault → kernel copies C into a new frame for the child.

parent→ A, B, C   |   child→ A, B, C′

VideoBytes

Demand paging and page faults

Video coming soon

Demand paging and page faults

Recorded by Dr. Krithika L.B. Slot m4-demand-paging

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Memory access 100 ns, fault service 10 ms, p = 0.0001. EAT?

0.9999 × 100 + 0.0001 × 10 000 000 ≈ 99.99 + 1000 ≈ 1.1 µs.

What is pure demand paging?

Starting a process with no pages in memory and bringing in every page only when first referenced.

Why does demand paging work despite the high cost of a fault?

Locality of reference: a process uses a small set of pages for a while, so faults are rare after the start.

Which pages are copied under copy-on-write?

Only pages that the parent or child actually writes to.

Key takeaways

  • Virtual memory separates logical from physical memory; only needed pages are loaded.
  • Page fault: trap → find frame → read from disk → update table → restart instruction.
  • EAT = (1 − p)·ma + p·fault time — p must be tiny; copy-on-write makes fork cheap.

Where this leads

Step 3 said “find a free frame”. When memory is full, the OS must evict a page — and choosing badly can bring the system to a crawl. Next: page replacement and thrashing.

Continue to 4.6 — Page replacement, thrashing and the working set →