C++ / a working model

125 / 163   ·   C11   ·   8 min

Beyond Physical Memory: Mechanisms

Keep this sentence

The OS employs slower secondary storage as swap area plus a present bit in each PTE so that many large address spaces can coexist even when they do not fit in RAM. Missing pages raise faults that a software handler resolves by transferring data from disk.

In this lesson
  1. Why Introduce Swap Area
  2. How the Present Bit Shows Page Location
  3. Steps Taken by the Page-Fault Handler
  4. Executable Pages and Multiprogramming
  5. Example
  6. Exercise

Official chapter PDF

Why Introduce Swap Area

Whenever the sum of all virtual address spaces exceeds DRAM, the OS must park unused pages on a larger but slower device. That reserved region is called swap space. Programmers no longer manage overlays by hand and the machine can keep more programs ready to run.

How the Present Bit Shows Page Location

A present flag is added to every page-table entry. Value 1 means the page occupies a physical frame; value 0 means it lives on disk. After a TLB miss the hardware inspects the flag and, if it is clear, traps to the OS rather than fabricating a bogus physical address.

Steps Taken by the Page-Fault Handler

Once trapped, the OS reads the disk-block number stored in the PTE, optionally writes a victim page back to swap, issues a read that fills a free or reclaimed frame, updates the present bit together with the frame number, and restarts the original memory instruction.

Executable Pages and Multiprogramming

Code pages need not consume swap slots because they can be fetched again from the original binary in the file system. The same mechanism also made genuine multiprogramming possible on early machines whose RAM could never hold every process at once.

Pitfalls

  • Assuming the processor itself performs the disk transfer
  • Forgetting that text pages can be demand-paged from the executable file
  • Mistaking a legal swapped-out reference for a protection violation

Run an example

Minimum C11 · complete program · Download .c

#include <stdio.h>
#include <stdbool.h>

#define N 8

typedef struct {
    bool present;
    unsigned loc;
} PTE;

static void dump(const PTE pt[]) {
    puts("VPN Present Location");
    for (int i = 0; i < N; i++)
        printf("%3d %7s %8u\n", i, pt[i].present ? "yes" : "no", pt[i].loc);
}

int main(void) {
    PTE pt[N] = {
        {true, 0}, {true, 1}, {false, 4}, {true, 2},
        {false, 6}, {false, 2}, {true, 3}, {false, 7}
    };
    puts("=== Tiny VM with Swap Simulator ===");
    puts("Initial page table:");
    dump(pt);
    puts("\nCPU references VPN 2 -> present bit clear -> PAGE FAULT");
    puts("Handler reads disk block 4, evicts VPN 0 to swap 0, places page in PFN 0");
    pt[0].present = false;
    pt[0].loc = 0;
    pt[2].present = true;
    pt[2].loc = 0;
    puts("Page table after fault handling:");
    dump(pt);
    puts("\nCPU references VPN 6 -> present, PFN 3, no fault");
    puts("Large virtual memory illusion maintained.");
    return 0;
}

Compile locally

gcc -std=c11 -Wall -Wextra -Wpedantic -Werror ostep-21-swapping-mechanisms.c -o example && ./example

Expected result

=== Tiny VM with Swap Simulator ===
Initial page table:
VPN Present Location
  0     yes        0
  1     yes        1
  2      no        4
  3     yes        2
  4      no        6
  5      no        2
  6     yes        3
  7      no        7

CPU references VPN 2 -> present bit clear -> PAGE FAULT
Handler reads disk block 4, evicts VPN 0 to swap 0, places page in PFN 0
Page table after fault handling:
VPN Present Location
  0      no        0
  1     yes        1
  2     yes        0
  3     yes        2
  4      no        6
  5      no        2
  6     yes        3
  7      no        7

CPU references VPN 6 -> present, PFN 3, no fault
Large virtual memory illusion maintained.

CHECK YOUR UNDERSTANDING

Close the answer. Explain it.

A PTE currently holds present=0 and disk block 17. After a successful page-fault, what do those two fields become?

Show a reference answer

present becomes 1 and the location field is overwritten with a physical frame number (for example 5).

Check the sources

Drafts and official chapters change. The version mark is only the example’s minimum.

Back to the catalog