Linux - mmap
mmap (Memory Map) is a Unix system call that maps a file (or other object) into the memory address space of a process.
Instead of reading a file by using the read() function to copy data from the disk into a buffer, mmap tells the operating system: "Pretend this file is already in memory."
Once mapped, you can access the file data using standard pointers (like an array in C), and the Operating System handles the loading and saving of data in the background.
The Fundamental Difference
To understand mmap, you have to compare it to the "Standard I/O" way of doing things.
The Standard Way (read / write)
Imagine you want to read a 1GB file.
- Syscall: You call
read(). - Context Switch: The CPU switches to Kernel Mode.
- Disk Read: The Kernel reads data from the disk into the Kernel Page Cache.
- Copy: The Kernel copies that data from the Page Cache into your application's buffer (User Space).
- Return: The CPU switches back to User Mode.
- Cost: High CPU usage due to copying memory and context switching.
The mmap Way
- Syscall: You call
mmap(). - Mapping: The Kernel modifies the process's Page Table. It marks a range of virtual memory addresses as "pointing" to that file on the disk.
- Return: The call returns a pointer. No data has been read yet.
- Access: You try to read
pointer[0]. - Page Fault: The CPU realizes that memory address is empty. It pauses your program and triggers a "Page Fault."
- Lazy Load: The Kernel catches the fault, transparently reads the necessary block from the disk into the Page Cache, maps it to your pointer, and resumes your program.
- Benefit: Zero-copy. The application reads directly from the Kernel's Page Cache.
The Two Main Types of mmap
When you create a map, you must choose how it behaves using flags:
A. File-Backed Mapping (MAP_SHARED vs MAP_PRIVATE)
MAP_SHARED:- Changes you make to the memory are written back to the disk.
- Other processes mapping the same file see your changes instantly.
- Use Case: Inter-Process Communication (IPC) or high-performance database writes.
MAP_PRIVATE(Copy-on-Write):- You can read the file, but if you write to the memory, the OS creates a private copy of that specific page just for you.
- The file on disk is never modified.
- Use Case: Loading code libraries (
.sofiles) or initializing a process.
B. Anonymous Mapping (MAP_ANONYMOUS)
- This doesn't involve a file at all. It just asks the Kernel for a big chunk of raw RAM, filled with zeros.
- Fun Fact: This is actually how
malloc()works! If you askmallocfor a tiny amount of RAM, it uses the heap (brk). If you ask for a huge amount (e.g., 100MB),mallocusesmmapto grab a fresh block from the OS.
Why use mmap? (The Pros)
- Speed (Zero Copy): You avoid the CPU overhead of copying data from kernel space to user space.
- Lazy Loading: If you
mmapa 1TB file but only read the last byte, the OS only reads the last 4KB page from the disk. The rest of the file never touches RAM. - Random Access: Jumping around a file is as easy as moving a pointer/index (
data[5000]). You don't have tolseek()back and forth. - IPC (Inter-Process Communication): If two programs
mmapthe same file withMAP_SHARED, they can talk to each other extremely fast by just reading/writing memory.
The Downsides (The Cons)
- Page Fault Overhead:
mmapis great for large files, but for small files, the overhead of setting up the page tables and handling page faults is slower than a simpleread(). - Lack of Control: With
write(), you know exactly when the I/O happens. Withmmap, the OS decides when to flush dirty pages back to the disk (unless you force it withmsync). - The "SIGBUS" Error: If you
mmapa file, and someone else truncates (shortens) that file while you are using it, and you try to access the part that was cut off, your program crashes with aSIGBUSsignal.
Summary Analogy
read(): You go to the library (Disk), verify your ID (Syscall), the librarian finds the book, photocopies the page you want (Buffer Copy), and hands you the copy.mmap(): The librarian gives you a magic pair of glasses. You look at an empty desk, but the glasses make the book appear there. When you try to read a specific page, the librarian quietly slips that real page onto the desk before your eyes focus. You read the original page directly.