Operating Systems Module 6: Memory Lesson 6.5 14 min

Demand paging and page faults

Pages are loaded only when a program first touches them. What a page fault is, what the OS does about it, why the instruction is retried, and how copy-on-write makes fork cheap.

You start a program that is 200 MB on disk. It opens in a blink. Did the computer really read 200 MB from the disk in that time? No. It read almost none of it. The rest is read later, one piece at a time, only when the program first touches it.

This idea is demand paging: bring a page into memory only when it is demanded. It is why programs start fast, why a huge program can run on a small machine, and why the page fault is not an error but an everyday event.

A page fault is a question to the OS

Each page table entry has a present bit (Lesson 6.3). When a process starts, most of its pages are not in memory: the entries say “not present”. The first time the program uses an address in such a page, the MMU finds present = 0 and cannot go on. It raises a page fault, which is a trap into the kernel, like the ones in Lesson 1.3.

The kernel’s page fault handler then does the following:

  1. Is the address legal? The kernel knows which ranges of addresses the process is allowed to use (its code, heap, stack, mapped files). If the address is in none of them, the program has a bug: the kernel sends it the signal that ends it, the “Segmentation fault” you know.
  2. Find a frame. Take a free frame. If none is free, choose a page to evict (Lesson 6.6).
  3. Fill it. Read the page from disk into the frame, or, for a brand-new heap page, fill the frame with zeros. While the disk works, the process is waiting and the CPU runs another process (Lesson 2.2).
  4. Fix the table. Write the frame number in the entry and set present to 1.
  5. Retry. Return to the program at the very instruction that faulted and run it again.

Here is the whole thing in the simulator. The TLB has two entries (Lesson 6.4). First, the program reads page 3, which is in memory. Then it reads an address in page 9, which is not, and then the next address after it.

A page fault and the retry

  1. The address space 16-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 4 bits, so there are 16 pages. One flat page table has an entry for every page. 1 page is in memory so far, and the TLB (a small cache of translations) has room for 2.
    Page faults so far
    0
    TLB hits / misses
    0 / 0
    Page-table pages
    1
  2. The program reads 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    TLB hits / misses
    0 / 0
    Page-table pages
    1
  3. TLB miss The TLB does not hold page 3, so the hardware has to read the page table in memory.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    TLB
    miss
    Page faults so far
    0
    TLB hits / misses
    0 / 1
    Page-table pages
    1
  4. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    TLB
    miss
    Frame
    7
    Page faults so far
    0
    TLB hits / misses
    0 / 1
    Page-table pages
    1
  5. Frame 7 + offset = 0x7A7F Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F. The translation is also copied into the TLB.
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    TLB
    miss
    Frame
    7
    Physical address
    0x7A7F
    Page faults so far
    0
    TLB hits / misses
    0 / 1
    Page-table pages
    1
  6. The program reads 0x9123 The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 291 (0x123). The rest is the page number, 9. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    Page faults so far
    0
    TLB hits / misses
    0 / 1
    Page-table pages
    1
  7. TLB miss The TLB does not hold page 9, so the hardware has to read the page table in memory.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    TLB
    miss
    Page faults so far
    0
    TLB hits / misses
    0 / 2
    Page-table pages
    1
  8. Read the page table Level 1: entry 9 has its present bit at 0: page 9 is not in memory.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    TLB
    miss
    Page faults so far
    0
    TLB hits / misses
    0 / 2
    Page-table pages
    1
  9. Page fault The page is not present, so the CPU raises a page fault and runs the OS (Lesson 1.3). The OS finds the page on disk, picks the free frame 0, copies the page in, and writes the entry: page 9 is in frame 0. Then the faulting instruction runs again.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    TLB
    miss
    Frame
    0
    Page faults so far
    1
    TLB hits / misses
    0 / 2
    Page-table pages
    1
  10. Frame 0 + offset = 0x0123 The instruction runs again and this time succeeds. Frame 0 times the page size 4096, plus the offset 291, is the physical address 0x0123. The translation is also copied into the TLB.
    Virtual address
    0x9123
    Page number
    9
    Offset
    0x123
    TLB
    miss
    Frame
    0
    Physical address
    0x0123
    Page faults so far
    1
    TLB hits / misses
    0 / 2
    Page-table pages
    1
  11. The program reads 0x9124 The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 292 (0x124). The rest is the page number, 9. The offset will not change: it is the position inside the page, and the frame is the same size.
    Virtual address
    0x9124
    Page number
    9
    Offset
    0x124
    Page faults so far
    1
    TLB hits / misses
    0 / 2
    Page-table pages
    1
  12. TLB hit The TLB already knows page 9: it is in frame 0. No page table is read at all.
    Virtual address
    0x9124
    Page number
    9
    Offset
    0x124
    TLB
    hit
    Frame
    0
    Page faults so far
    1
    TLB hits / misses
    1 / 2
    Page-table pages
    1
  13. Frame 0 + offset = 0x0124 Frame 0 times the page size, plus the offset 292, is the physical address 0x0124.
    Virtual address
    0x9124
    Page number
    9
    Offset
    0x124
    TLB
    hit
    Frame
    0
    Physical address
    0x0124
    Page faults so far
    1
    TLB hits / misses
    1 / 2
    Page-table pages
    1

Notice what the third access shows: the page is now in memory, and the TLB knows it, so the next address in page 9 does not fault or even walk the table.

Why retry the instruction? An instruction such as “add the number at this address to a register” cannot be left half done. When the MMU stops it, the CPU saves the program counter pointing at that same instruction, and the OS gives it back unchanged. After the OS has fixed the page table, the very same instruction succeeds. From the program’s point of view, memory was simply a little slow that time.

Minor and major faults

Not every fault costs a disk read. There are two kinds.

  • A minor fault can be settled without the disk. For example, a page of a heap that the program has never used only needs a frame full of zeros, or the page is already in memory because another process loaded it (a shared library) and only this process’s table needs an entry.
  • A major fault must read the page from disk (or from swap), so the process waits. A disk is far slower than memory (Lesson 1.2), so a major fault is the expensive kind.

A program that touches a lot of new memory causes many minor faults. A system that is short of memory causes many major faults, as Lesson 6.7 shows.

A file as memory

Demand paging also gives programs a neat way to use files. The system call mmap maps a file into the address space: the file’s bytes appear as ordinary memory. Nothing is read when mmap returns. Each page of the file is read in when the program first touches it, through a page fault.

This program makes a temporary file of three pages and maps it. It writes one letter at the start of each page through the pointer, and reads the third page back by using the file itself:

mmap_pages.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

int main(void) {
    long page = sysconf(_SC_PAGESIZE);          /* the size of one page on this machine */
    char path[] = "/tmp/pages_XXXXXX";
    int fd = mkstemp(path);                     /* a temporary file */
    if (fd < 0) { perror("mkstemp"); return 1; }
    unlink(path);                               /* it disappears when we close it */
    if (ftruncate(fd, 3 * page) != 0) { perror("ftruncate"); return 1; }

    char *p = mmap(NULL, 3 * page, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED) { perror("mmap"); return 1; }

    for (int i = 0; i < 3; i++)
        p[i * page] = 'A' + i;                  /* touching a page for the first time */

    printf("pages in the mapping: %ld\n", (3 * page) / page);
    for (int i = 0; i < 3; i++)
        printf("page %d starts with %c\n", i, p[i * page]);

    char c;
    if (pread(fd, &c, 1, 2 * page) != 1) { perror("pread"); return 1; }
    printf("the file itself says page 2 starts with %c\n", c);
    munmap(p, 3 * page);
    close(fd);
    return 0;
}

Output (one run)

pages in the mapping: 3
page 0 starts with A
page 1 starts with B
page 2 starts with C
the file itself says page 2 starts with C

The last line shows that the memory and the file are the same data: a write through the pointer is a write to the file’s page. The program ran the same way whatever the page size, because it asked for the size and never wrote it down.

fork without copying: copy-on-write

Now we can answer a question left open in Lesson 2.3. fork makes a child that is a copy of the parent. If that copied all of the parent’s memory, fork would be slow, and it would be wasted work in the most common case, where the child calls exec right away and throws everything away.

The trick is copy-on-write. The OS copies only the page table, not the pages. Parent and child now point to the same frames. The OS marks every shared page read-only in both. Reading is fine: both see the same data. The first time either one writes, the MMU refuses, because the page is read-only, and the CPU traps. The OS sees that this is a shared copy-on-write page, copies that one page into a free frame, points the writer’s table at the copy and makes it writable, and the write is retried. All the other pages stay shared.

In this simulator P (the parent) has two pages in memory and forks C (the child). Watch the frames at the bottom of the Table walk view, and the “frames shared” field. Then C reads, C writes, and P reads the same address.

fork with copy-on-write

  1. The address space 16-bit addresses, 4K pages: the offset is the low 12 bits (a page holds 4096 bytes), and the page number is the other 4 bits, so there are 16 pages. One flat page table has an entry for every page. 2 pages are in memory so far.
    Process
    P
    Page faults so far
    0
    Frames shared
    0
    Pages copied
    0
    Page-table pages
    1
  2. P forks: C gets a copy of the page table fork (Lesson 2.3) makes a child, C. The OS does not copy any page of memory: it copies only the page table. Now P and C point at the same 2 frames. It marks every shared page read-only in both processes. This is copy-on-write: nothing is copied until somebody writes.
    Process
    P
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  3. C reads 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  4. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  5. Frame 7 + offset = 0x7A7F Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Physical address
    0x7A7F
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  6. C writes to 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  7. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  8. Protection fault: the page is shared C writes to page 3, but the page is marked read-only because P shares frame 7. The MMU refuses the write and traps to the OS. The OS sees that this is a copy-on-write page, not a mistake.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Frames shared
    2
    Pages copied
    0
    Page-table pages
    2
  9. The OS copies the page The OS copies the whole page from frame 7 into the free frame 0, points C's page table at the copy, and makes it writable. Only P is still using frame 7, so its page is made writable again too. Then the write runs again.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    0
    Page faults so far
    0
    Frames shared
    1
    Pages copied
    1
    Page-table pages
    2
  10. Frame 0 + offset = 0x0A7F The instruction runs again and this time succeeds. Frame 0 times the page size 4096, plus the offset 2687, is the physical address 0x0A7F. Frame 0 holds C's private copy of page 3. The original, in frame 7, is untouched.
    Process
    C
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    0
    Physical address
    0x0A7F
    Page faults so far
    0
    Frames shared
    1
    Pages copied
    1
    Page-table pages
    2
  11. P reads 0x3A7F The MMU (the memory management unit, hardware beside the CPU) splits the address. The low 12 bits are the offset, 2687 (0xA7F). The rest is the page number, 3. The offset will not change: it is the position inside the page, and the frame is the same size.
    Process
    P
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Page faults so far
    0
    Frames shared
    1
    Pages copied
    1
    Page-table pages
    2
  12. Read the page table Level 1: entry 3 says page 3 is in frame 7 (its present bit is 1).
    Process
    P
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Page faults so far
    0
    Frames shared
    1
    Pages copied
    1
    Page-table pages
    2
  13. Frame 7 + offset = 0x7A7F Frame 7 times the page size 4096, plus the offset 2687, is the physical address 0x7A7F.
    Process
    P
    Virtual address
    0x3A7F
    Page number
    3
    Offset
    0xA7F
    Frame
    7
    Physical address
    0x7A7F
    Page faults so far
    0
    Frames shared
    1
    Pages copied
    1
    Page-table pages
    2

At the end, P still reads its original page (frame 7) and C has its own copy (frame 0). Page 4, which nobody wrote, is still shared by both, and costs nothing extra. If the child had called exec instead, almost none of the shared pages would ever have been copied. That is how a shell can start a program at the cost of a few table copies.

In the real world

Linux counts the two kinds of page fault for every process. The command /usr/bin/time -v prints them when the program ends, and ps can show them for a running process:

Terminal
$ /usr/bin/time -v ./myprogram
...
	Major (requiring I/O) page faults: ...
	Minor (reclaiming a frame) page faults: ...
...
$ ps -o pid,min_flt,maj_flt,cmd -p 1

A healthy program shows a few major faults (loading its own code and data from disk the first time) and many minor ones. A program with a surprising number of major faults is being slowed by the disk, which is the usual sign that the machine is short of memory. Your numbers will differ on every machine, so read the pattern, not the values. Browsers and databases use mmap heavily, so their files are loaded page by page in the same way. :::

Quick check

Quick check

  1. What does a page fault mean?
  2. What is the difference between a minor and a major page fault?
  3. Why is the faulting instruction run again after the OS has loaded the page?
  4. What does copy-on-write save when a process forks?

Recap

  • Demand paging loads a page only when it is first used. Programs start fast, and big programs fit in small memories.
  • A page fault is a trap raised when the present bit is 0. The handler checks the address, finds a frame, fills it from disk or with zeros, fixes the entry and retries the instruction.
  • A minor fault needs no disk read; a major fault does, and the process waits.
  • mmap makes a file appear as memory, loaded page by page through faults.
  • Copy-on-write makes fork cheap: only the page table is copied, pages are shared read-only, and a page is copied on the first write (Lesson 2.3).

Next: the page fault handler needs a free frame, and sometimes there is none. Which page should the OS throw out?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion