Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 66 ยท Virtual Memory

Copy-on-Write, Memory-Mapped Files and Kernel Memory

Learn how fork uses copy-on-write to share pages, how memory-mapped files turn file I/O into page faults, and how the kernel allocates memory with buddy and slab allocators.

Virtual memory does more than let programs be bigger than RAM. Because the OS controls the mapping from pages to frames, it can play clever tricks: share memory until someone writes, map files into the address space, and hand out zeroed pages on demand. This lesson covers the three most important of them and how the kernel itself gets memory.

Copy-on-write (COW)

When a process calls fork(), the child must get a copy of the parent's address space. Copying hundreds of megabytes would be slow, and it is often pointless: a typical child immediately calls exec() and replaces everything it just copied.

Copy-on-write avoids the copy. After fork:

  1. The child gets its own page table, with every entry pointing to the same frames as the parent.
  2. All shared pages are marked read-only (and flagged "copy on write"), and a reference count per frame records how many page tables use it.
  3. When either process writes to such a page, the hardware raises a protection fault. The OS then allocates a new frame, copies the page, maps the copy writable into the writer's page table, and decrements the reference count of the original.
  4. If the reference count has dropped to 1, the remaining process simply gets write access back, with no copying.
sequenceDiagram
    participant P as Parent
    participant K as Kernel
    participant C as Child
    P->>K: fork()
    K->>C: new page table, same frames, all read-only (refcount 2)
    C->>K: write to page 3 (protection fault)
    K->>K: copy frame 3 to a new frame
    K->>C: map the new frame writable
    Note over P,C: Page 3 now private to each; other pages still shared

Only the pages that are actually modified are ever copied.

Python fork with copy-on-write

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

fork allocated no frames at all. The child's first write to the heap page created exactly one new frame (its second write caused no fault: the page was private by then). The parent's write then found that it was the last owner of the original frame, so it only regained write access and nothing was copied. Pages 0, 1 and 3 are still shared. COW is why fork + exec in a shell is cheap and why Redis snapshots and similar programs can fork a copy of a huge in-memory database in milliseconds.

A variation, vfork, suspends the parent and lets the child share the parent's address space with no copying at all until the child execs; it is dangerous, because the child can corrupt the parent's memory.

Zero-fill-on-demand

When a process asks for new memory (a malloc that grows the heap, a fresh stack page), the OS does not need to find a frame immediately. Pages are marked invalid; the first access faults and the OS then supplies a frame filled with zeros, so that a process can never see the leftover data of another. Many systems even map every untouched page to a single shared zero page, read-only, using COW.

Memory-mapped files

Normally, a file is read with read() system calls: the kernel copies data from its page cache into your buffer. Memory mapping (mmap on POSIX, CreateFileMapping on Windows) instead maps the file's pages straight into the process's address space. You then use ordinary memory accesses on a pointer.

  • The first access to a page of the mapping causes a page fault. The OS reads that part of the file into a frame and maps it, exactly like demand paging, except the backing store is the file instead of swap space.
  • Writes modify the frame; dirty pages are written back to the file later (or when you call msync).
  • Two processes mapping the same file share the same frames, which makes mmap a way to implement shared memory.
  • With MAP_PRIVATE the mapping is copy-on-write: your writes are not saved to the file. With MAP_SHARED they are.
Python Lazy loading with a memory-mapped file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the pages touched were read, so mapping a 10 GB file and reading a few records costs a few page faults, not 10 GB of I/O. Databases (SQLite, LMDB), search engines and the dynamic loader (which maps executables and libraries) use this. Cautions: errors on I/O appear as signals (SIGBUS) instead of return codes, and random access to a file much bigger than RAM can cause heavy page-fault traffic.

Allocating kernel memory

User processes get memory in page-sized units through paging. The kernel is different:

  • It often needs small objects (process descriptors, file structures, network buffers) much smaller than a page, and wasting a 4 KB page for each would be absurd.
  • Some memory (for device DMA) must be physically contiguous, so paging cannot help.

Kernels therefore use two cooperating allocators:

  • The buddy system (earlier lesson) hands out contiguous groups of pages.
  • The slab allocator sits on top: it takes a few pages from the buddy allocator, cuts them into equal-size objects of one type (a "cache" per type), and keeps lists of full, partial and empty slabs. Allocation takes an object from a partial slab, freeing puts it back already initialised, so it can be reused without rebuilding. There is no fragmentation within a slab and allocation is very fast. Linux uses SLUB (a streamlined slab); user-space malloc implementations such as jemalloc use similar size-class ideas.
Python A tiny slab cache

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other tricks that rely on paging

  • Huge pages (2 MB or 1 GB) reduce TLB misses for programs that use gigabytes.
  • Memory deduplication (such as KSM on Linux) finds identical pages in different processes or virtual machines and merges them with COW.
  • Guard pages (an inaccessible page at the end of a stack) turn overflows into immediate faults.
  • Swap compression (zswap) and memory overcommit (promising more memory than exists, relying on demand paging and the OOM killer when the bluff is called).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does fork do with the pages when copy-on-write is used?

    Parent and child share frames until a write triggers a protection fault; only then is that one page copied.
  2. 2. A frame has a reference count of 1 and its page is marked copy-on-write. A write fault occurs. What does the OS do?

    Nobody else shares the frame, so copying would be wasted work.
  3. 3. What is the backing store of a page in a memory-mapped file?

    Pages of a mapped file are read from, and (for shared mappings) written back to, the file.
  4. 4. Why does the kernel use a slab allocator for small objects?

    Slabs carve pages into equal-size objects of one type, with almost no fragmentation and fast alloc and free.
  5. 5. Why does a new heap page contain only zeros when first touched?

    Zero-fill-on-demand protects confidentiality: the OS never hands out a frame with someone else's old data.

Summary

  • Copy-on-write lets fork share all pages and copy only those that are modified; reference counts decide when a copy is needed.
  • Zero-fill-on-demand supplies new pages lazily, always cleared.
  • Memory-mapped files turn file access into demand-paged memory access and give processes shared memory.
  • The kernel allocates contiguous page groups with the buddy system and small objects with slab allocators.
  • Huge pages, deduplication, guard pages and overcommit are further uses of the same mapping machinery.

Next: Back to the Operating Systems track