G27
Self-checkedTier 1 12 min reados.virtual-memory-paging

Virtual memory and demand paging

Running programs that are not fully in memory: the idea of virtual memory, demand paging and the valid bit, what happens on a page fault step by step, the effective access time with page faults and the fault rate it allows, the modified bit, copy-on-write, and locality of reference.

Why it exists

A program rarely needs all of its code and data at once: error-handling routines almost never run, a large array is used a part at a time, and a program may be bigger than the physical memory. Virtual memory separates the memory the programmer sees (a large, uniform virtual address space) from the memory the machine has. Only the parts in use are kept in RAM, the rest waits on disk. This lets programs be larger than memory, lets more programs run at once (each uses less RAM), and makes starting a program faster. The cost appears when a needed part is not in RAM: a page fault. GATE asks what a page fault does, how to compute the effective access time with page faults, and how large a fault rate a system can tolerate.

Prerequisites

You need the page table, the valid bit and the translation of addresses from paging.

Precise definitions

Virtual memory. A technique that lets a process run although only part of it is in physical memory. The virtual address space (size fixed by the address width) can be much larger than physical memory. The disk area that holds the pages not in memory is the backing store or swap space.

Demand paging. Pages are brought into memory only when they are needed (when a reference to them is made), like a swapper that never loads a page that will not be used (a lazy swapper, or pager). In pure demand paging a process starts with no pages in memory at all and faults on its first instruction.

Valid-invalid bit. Each page-table entry has a bit. With virtual memory it means:

  • valid (1): the page is legal and is in memory (the entry holds its frame);
  • invalid (0): either the page is illegal (not part of the process's address space) or it is legal but not currently in memory. The operating system keeps, for such a page, its disk address.

Page fault. The trap that occurs when a process references a page whose valid bit is 0. Handling it:

  1. The hardware traps to the operating system (saving the state of the process).
  2. The operating system checks an internal table to see whether the reference was legal. If it was illegal, the process is terminated.
  3. If it was legal but the page is not in memory, the OS finds a free frame. If there is none it chooses a victim page with a page-replacement algorithm (page replacement); if the victim was modified (its dirty/modified bit is 1) it is first written back to disk.
  4. The OS schedules a disk read of the needed page into the frame. Meanwhile the process is blocked and the CPU may run another process.
  5. When the read completes (a disk interrupt), the OS updates the page table (frame number, valid bit 1) and the frame table.
  6. The OS makes the process ready; when it runs again, the instruction that faulted is restarted from the beginning, and now succeeds.

The hardware must be able to restart an instruction after a fault; some instructions that change several locations (block moves) need special care.

Effective access time (EAT) with page faults. If the page-fault rate (probability that a reference faults) is pp, the memory access time is mama and the time to service a fault is TfT_f,

EAT=(1−p) ma+p Tf.\text{EAT} = (1 - p)\,ma + p\,T_f.

TfT_f is dominated by the disk: a disk access takes milliseconds while a memory access takes nanoseconds, a ratio of about 10510^5 to 10610^6. If the victim is modified, the page must be written out as well as read in, so TfT_f is about doubled: with a probability qq that the victim is dirty, Tf≈Tread(1+q)T_f \approx T_{read}(1 + q).

Locality of reference. Programs tend to use the same pages again and again in a short period (temporal locality), and to use pages near those just used (spatial locality). Demand paging works only because the fault rate is tiny in practice: the set of pages in active use (the working set, thrashing) fits in memory.

Copy-on-write (COW). When a process is forked, parent and child share the same physical pages, all marked read-only. Only when one of them writes to a page does the OS make a private copy of that page for the writer. A fork of a process of 100 pages in which the child writes to 5 pages copies 5 pages, not 100; if the child calls exec at once nothing is copied.

Memory-mapped files. A file is mapped into the virtual address space; reading and writing memory reads and writes the file, with the file's disk blocks acting as the backing store and pages loaded on demand.

How it works

The animation shows demand paging for a process of 8 pages in a memory of only 3 frames. The page table (left) starts empty: every valid bit is 0. Seven references are made. A reference to a page that is already in memory (reference 3, to page 2) just translates. Otherwise there is a fault: the page is loaded into a free frame, or into the frame of the least recently used page when memory is full, the table changes, and the reference is restarted and succeeds. Watch the valid bits and frame numbers change in the table from step to step.

Demand paging: page faults load pages into frames: start

Translation computed, not typed
page table (8 pages)
pageframevalid
0-0
1-0
2-0
3-0
4-0
5-0
6-0
7-0

No address yet.

Step 0

Demand paging: no page is loaded until it is used. A process has 8 pages (1 KB each) but only 3 frames of memory, and its page table starts with every valid bit 0 ('not in memory': the page is on disk). A reference to a page whose valid bit is 0 causes a PAGE FAULT: the operating system loads the page into a frame, updates the table and RESTARTS the instruction. Frames are filled first; once all are in use the least recently used page is replaced.

0 / 19
computed by script: the algorithm is executed on the reference string, one reference per step. python3 scripts/gen-os-vm-traces.py

Worked examples

EX 1Effective access time with page faults

Easy

Memory access ma=200ma = 200 ns and servicing a page fault takes 8 ms.

#include <stdio.h>
 
int main(void) {
    /* effective access time with page faults: ma = 200 ns, fault service time 8 ms */
    double ma = 200, service = 8e6;
    double ps[3] = {0, 1e-3, 1e-6};
    for (int i = 0; i < 3; i++) {
        double p = ps[i];
        printf("page fault rate %.0e: effective access time %.1f ns (slowdown %.1f%%)\n", p, (1 - p) * ma + p * service, 100 * ((1 - p) * ma + p * service - ma) / ma);
    }
    /* the largest fault rate that keeps the slowdown below 10 percent */
    double pmax = 0.10 * ma / (service - ma);
    printf("for at most 10%% slowdown: p < %.4g, i.e. fewer than one fault per %.0f references\n", pmax, 1 / pmax);
    /* demand paging with 3 frames and LRU on the page references of the animation */
    int refs[7] = {2, 5, 2, 7, 0, 5, 3};
    int page[3] = {-1, -1, -1}, last[3] = {0, 0, 0}, faults = 0;
    for (int t = 0; t < 7; t++) {
        int slot = -1;
        for (int i = 0; i < 3; i++) if (page[i] == refs[t]) slot = i;
        if (slot < 0) {
            faults++;
            for (int i = 0; i < 3; i++) if (page[i] < 0) { slot = i; break; }
            if (slot < 0) { slot = 0; for (int i = 1; i < 3; i++) if (last[i] < last[slot]) slot = i; }
            page[slot] = refs[t];
        }
        last[slot] = t + 1;
    }
    printf("page faults for 7 references with 3 frames (LRU): %d\n", faults);
    return 0;
}
page fault rate 0e+00: effective access time 200.0 ns (slowdown 0.0%)
page fault rate 1e-03: effective access time 8199.8 ns (slowdown 3999.9%)
page fault rate 1e-06: effective access time 208.0 ns (slowdown 4.0%)
for at most 10% slowdown: p < 2.5e-06, i.e. fewer than one fault per 399990 references
page faults for 7 references with 3 frames (LRU): 6

Reading it. At p=0.001p = 0.001 (one fault per thousand references) the machine runs forty times slower: 0.999×200+0.001×8 000 000=8199.80.999 \times 200 + 0.001 \times 8\,000\,000 = 8199.8 ns. To keep the slowdown below 10%, 200+7 999 800 p≤220200 + 7\,999\,800\,p \le 220 gives p≤2.5×10−6p \le 2.5 \times 10^{-6}, fewer than one fault in about 400 000 references. That is why page replacement must keep the fault rate extremely low. The last line replays the seven references of the animation: six of them fault.

EX 2What the valid bit tells you

Easy

A process has 8 pages of which 3 are in memory. The valid bits are 1 for those three and 0 for the other five. A reference to a page with bit 0 is a fault. The OS then looks in its own table: if the page lies outside the process's address space the reference is illegal (a segmentation fault: the process is killed); if the page is part of the process but swapped out, the OS loads it. So "invalid" has two meanings, and only the OS's table can distinguish them.

EX 3A dirty victim costs twice the disk time

Medium

Take the numbers above, but suppose that in 50% of faults the victim page has been modified and must be written back first. The service time becomes 8+0.5×8=128 + 0.5 \times 8 = 12 ms. With p=10−4p = 10^{-4} the effective access time is (1−10−4)×200+10−4×12 000 000=1399.98(1 - 10^{-4}) \times 200 + 10^{-4} \times 12\,000\,000 = 1399.98 ns, instead of 999.98999.98 ns if no victim were dirty. A modified (dirty) bit in each page-table entry, set by the hardware on a write, lets the OS skip the write-back for pages that were only read: such a victim can simply be overwritten.

EX 4Walking through the fault in the animation

Medium

Reference 4 is address 7×1024+10=71787 \times 1024 + 10 = 7178, page 7. Before it, pages 2 and 5 are resident (frames 0 and 1). Page 7's valid bit is 0, so the hardware traps. A frame is free (frame 2), so no victim is needed; the OS reads page 7 into frame 2 and sets its table entry to (2, valid). The instruction is restarted: the physical address is 2×1024+10=20582 \times 1024 + 10 = 2058. Reference 5 is page 0: all three frames are now in use (pages 2, 5 and 7), so the OS needs a victim. The least recently used page is page 5 (last used at reference 2; page 2 was used at reference 3 and page 7 at reference 4), so page 5's valid bit goes back to 0 and page 0 takes its frame, frame 1.

EX 5Compute the effective access time

GATE level

Memory access takes 100 ns, a page fault takes 10 ms, and one reference in 10410^4 faults (p=10−4p = 10^{-4}). EAT =0.9999×100+0.0001×10 000 000=99.99+1000=1099.99= 0.9999 \times 100 + 0.0001 \times 10\,000\,000 = 99.99 + 1000 = 1099.99 ns, about eleven times the memory time. Questions of this kind may add a TLB or a two-level table to mama (TLB and effective access time), a fraction of dirty victims to TfT_f, or ask for the largest pp that keeps the EAT below a given value: solve (1−p) ma+p Tf≤target(1 - p)\,ma + p\,T_f \le \text{target} for pp.

Cheat box

Cheat box

  • Virtual memory: run with part of the process in memory; virtual space can exceed RAM. Demand paging: load a page only when referenced.
  • Valid bit 1: legal and in memory. 0: illegal OR not in memory.
  • Page fault: trap, check legality, find a frame (victim if none; write back if dirty), read page, update table, restart the instruction.
  • EAT=(1−p) ma+p Tf\text{EAT} = (1 - p)\,ma + p\,T_f. Disk time dominates TfT_f; a dirty victim adds a write.
  • pmax=allowed extra timeTf−map_{max} = \dfrac{\text{allowed extra time}}{T_f - ma}.
  • Locality of reference makes demand paging work. Copy-on-write: share until written.

Traps and GATE patterns

Connections

Demand paging is paging with the valid bit used to mean "on disk". Choosing a victim is the subject of page replacement; keeping the fault rate low enough needs each process to have its working set in memory (thrashing and the working set); the cost of the extra translation accesses is reduced by the TLB. A process that waits for the disk is in the Waiting state of the process life cycle, and copy-on-write is what makes fork cheap.

PYQs

9 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 6 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · MSQQ3 · page 340 of the book
Consider a system that has a cache memory unit and
a memory management unit (MMU). The address
input to the cache memory is a physical address. The
MMU has a Translation Lookaside Buffer (TLB).
Assume that when a page is evicted from main
memory, the corresponding blocks in the cache are
marked invalid.

For a given memory reference, which of the following
sequences of events can NEVER happen?
  • (a)TLB miss, Page table hit, Cache hit
  • (b)TLB hit, Page table miss, Cache hit
  • (c)TLB miss, Page table miss, Cache hit
  • (d)TLB miss, Page table miss, Cache miss
GATE 2024 · MSQQ10 · page 341 of the book
Which
of
the
following
tasks
is/are
the
responsibility/responsibilities
of
the
memory
management unit (MMU) in a system with paging-
based memory management?
  • (a)Allocate a new page table for a newly created process
  • (b)Translate a virtual address to a physical address using the page table
  • (c)Raise a trap when a virtual address is not found in the page table
  • (d)Raise a trap when a process tries to write to a page marked with read-only permission in the page table
GATE 2023 · MCQQ12 · page 342 of the book
Consider the following two-dimensional array D in
the C programming language, which is stored in
row-major order:

int D[128][128];

Demand paging is used for allocating memory and
each physical page frame holds 512 elements of the
array D. The Least Recently Used (LRU) page-
replacement policy is used by the operating system.
A total of 30 physical page frames are allocated to
a process that executes the following code snippet:

for (int i = 0; i < 128; i++)

for (int j = 0; j < 128; j++)

D[j][i] *= 10;

The number of page faults generated during the
execution of this code snippet is
  • (a)2048
  • (b)512
  • (c)4096
  • (d)1032
GATE 2020 · NATQ19 · page 343 of the book
Consider a paging system that uses a 1-level page
table residing in main memory and a TLB for
address translation. Each main memory access
takes 100 ns and TLB lookup takes 20 ns. Each
page transfer to/from the disk takes 5000 ns.
Assume that the TLB hit ratio is 95%, page fault
rate is 10%. Assume that for 20% of the total page
faults, a dirty page has to be written back to disk
before the required page is read in from disk. TLB
update time is negligible. The average memory
access time in ns (round off to 1 decimal place) is
______.
year not read yetQ21 · page 343 of the book
Consider a process executing on an operating system
that uses demand paging. The average time for a
memory access in the system is M units if the
corresponding memory page is available in memory
and D units if the memory access causes a page fault.
It has been experimentally measured that the average
time taken for a memory access in the process is X
units.

Which one of the following is the correct expression
for the page fault rate experienced by the process?
  • (a)(D-M)/(X-M)
  • (b)(X-M)/(D-M)
  • (c)(D-X)/(D-M)
  • (d)(X-M)/(D-X)
GATE 2011 · MCQ · 1 markQ38 · page 346 of the book
Let the page fault service time to 10 ms in a
computer with average memory access time being
20 ns. If one page fault is generated for every 106
memory accesses, what is the effective access time
for the memory?
  • (a)21 ns
  • (b)30 ns
  • (c)23 ns
  • (d)25ns
GATE 2006 · MCQQ48 · page 347 of the book
A computer system supports 32-bit virtual addresses
as well as 32-bit physical addresses. Since the virtual
address space is of the same size as the physical
address space, the operating system designers decide
to get rid of the virtual memory entirely. Which one
of the following is true?
  • (a)Efficient implementation of multi-user support is no longer possible
  • (b)The processor cache organization can be made more efficient now
  • (c)Hardware support for memory management is no longer needed
  • (d)CPU scheduling can be made more efficient now
GATE 2005 · MCQ · 1 markQ49 · page 347 of the book
Increasing the RAM of a computer typically improves
performance because:
  • (a)Virtual memory increases
  • (b)Larger RAMs are faster
  • (c)Fewer page faults occur
  • (d)Fewer segmentation faults occur OPERATING SYSTEMS
GATE 2005 · MCQQ29 · page 361 of the book
What is the swap space in the disk used for?
  • (a)Saving temporary HTML pages
  • (b)Saving process data
  • (c)Storing the super-block
  • (d)Storing device drivers

Self-check

Question 1

Memory access 150 ns, fault service 5 ms, fault rate 2×10−52 \times 10^{-5}. Effective access time?

Show answer

(1−0.00002)×150+0.00002×5 000 000=149.997+100=249.997(1 - 0.00002) \times 150 + 0.00002 \times 5\,000\,000 = 149.997 + 100 = 249.997 ns, about 250 ns.

Question 2

For the same machine, what fault rate keeps the EAT within 20% of mama?

Show answer

p=0.2×150/(5 000 000−150)=30/4 999 850≈6.0×10−6p = 0.2 \times 150/(5\,000\,000 - 150) = 30/4\,999\,850 \approx 6.0 \times 10^{-6}.

Question 3

A page's modified bit is 0 when it is chosen as the victim. What is saved?

Show answer

The write-back to disk: the disk copy is already identical, so the frame can simply be reused.

Question 4

What does copy-on-write do for fork?

Show answer

Parent and child share all pages read-only; a page is copied only when one of them writes it, so a fork followed by exec copies almost nothing.

Revision card

One-screen revision

  • Valid 1: in memory; 0: illegal or on disk. Fault: trap, check, frame (victim, write back if dirty), read, update, restart.
  • EAT=(1−p) ma+p Tf\text{EAT} = (1 - p)\,ma + p\,T_f; p=(EAT−ma)/(Tf−ma)p = (\text{EAT} - ma)/(T_f - ma).
  • Dirty victim: extra write. Locality makes it work. Copy-on-write shares until written.

References

  • ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 10 Virtual Memory (background, demand paging, copy-on-write)
  • ·Tanenbaum and Bos, Modern Operating Systems, Chapter 3 Memory Management (virtual memory, paging, page fault handling)
  • ·Stallings, Operating Systems: Internals and Design Principles, Chapter 8 Virtual Memory (hardware and control structures)

Verification

The demand-paging animation is produced by a script (scripts/gen-os-vm-traces.py): the page table changes at every fault, and tests/os-vm-traces.test.ts rechecks every physical address against the table at that step, that at most three pages are resident with distinct frames, and that each fault is followed by the load and a successful restart; scripts/enum-checks/os_vm.py replays it with an independent LRU model. The effective-access-time numbers are produced by the C program on this page (npm run verify:c) and by exact arithmetic in Python (including the largest fault rate for a 10 percent slowdown, about 1 in 399990, and the dirty-victim variant). The fault service time of 8 ms and the memory time of 200 ns are the textbook's illustrative values, not measurements. Copy-on-write and memory-mapped files are described, not simulated. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never