Home
Jun 14, 2026

Copy-on-Write Explained Simply

OSmemoryprocesses

When a process calls fork(), the child is supposed to get a complete copy of the parent’s memory. Taken literally, that’s absurd: forking a process using 2 GB would mean copying 2 GB of RAM, and the most common thing a child does is immediately call exec() and throw the whole copy away. The shell does exactly this for every command you run.

Operating systems avoid this waste with copy-on-write (COW).

The Trick

On fork(), the OS copies nothing. Parent and child get identical page tables, both pointing at the same physical pages. The only change: every writable page is marked read-only in both processes.

  • As long as everyone only reads, the shared pages work perfectly, and the fork cost was just duplicating the page table.
  • The moment either process writes to a page, the CPU raises a page fault (the page is read-only, remember). The OS looks at the fault, recognizes it’s a COW page, copies that one page, gives the writer its private copy, marks both copies writable, and resumes the process.

The write goes through as if nothing happened. The process never knows.

So the “copy the whole address space” contract is honored lazily, one 4 KB page at a time, and only for pages that actually get written. Fork a 2 GB process and exec() right after, and the total copied is close to zero.

Where Else It Shows Up

Once you see the pattern, share until someone writes, copy at the last possible moment, it turns up everywhere:

  • Redis uses COW for persistence: it forks, and the child snapshots the dataset to disk while the parent keeps serving writes. The kernel materializes copies only for the pages the parent modifies during the save, an almost-free consistent snapshot.
  • Filesystems like Btrfs and ZFS apply the same idea to disk blocks, which is what makes snapshots instant.
  • Immutable collections in functional languages use structural sharing, the same trick at the data-structure level.

The Redis case has a fun sharp edge: under heavy write load during a snapshot, memory usage can nearly double as pages get copied one fault at a time. COW makes copies cheap, not free, it just moves the bill to write time.

References

  • Silberschatz, A., Galvin, P. B., & Gagne, G. Operating System Concepts (10th ed.). John Wiley & Sons, 2018.