TRT
Sign in

Core · Module 5 · Lesson 9 of 12

Memory-Mapped I/O with mmap and msync

Map disk files directly into process address space with mmap, eliminate user-kernel data copying, synchronize dirty pages with msync, and handle SIGBUS faults.

Pro — members onlyAbout 16 minutes

By the end you can

  • Map regular files into virtual address space using mmap with MAP_SHARED and MAP_PRIVATE
  • Trace demand paging and page fault resolution from virtual memory to the kernel page cache
  • Synchronize dirty memory pages to disk storage deterministically using msync
  • Diagnose and prevent SIGBUS crashes caused by file truncation and unaligned offsets

Builds on Directory Streams, Hard Links, and Symlinks.

01 · A question first

Why does reading a 4-gigabyte file with read() copy every single byte twice through RAM, while mmap() allows you to inspect file contents instantly without copying a single byte? The secret lies in how the CPU Memory Management Unit binds virtual addresses directly to the kernel page cache.

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem