Mapping a file
mmap() asks the kernel to make a file look like part of the program's memory. The program then reads and writes the file with plain loads and stores, as if it were an array. notes.txt holds 8192 bytes: two pages of 4096.
The system call mmap() asks the kernel for a range of addresses, p to p + 8191, backed by the file. The kernel records this but reads nothing yet: both page-table entries say not present. It returns the start address p.
The first touch
c = p[5000] is a plain load, not a system call. Byte 5000 sits on page 1 (5000 ÷ 4096 = 1, remainder 904). The CPU's MMU, the part that translates addresses, looks up page 1 in the page table and finds it not present.
The MMU raises a page fault, a trap into the kernel. The kernel checks the address: it lies inside the mapping of notes.txt, so this is not a bug. The kernel must bring in page 1 of the file.
Page 1 is not in the page cache yet. The kernel takes a free frame of RAM, frame 7, and reads those 4096 bytes from disk into it. Only page 1 is needed now; in this simple model page 0 stays on disk.
The kernel fills in entry 1 of the page table: page 1 is present, in frame 7. Then it returns from the fault, and the CPU runs the same load instruction again.
This time the MMU turns p + 5000 into frame 7, byte 904, and the load reads 'a' straight from the page cache. Nothing was copied for the program: its addresses lead to the kernel's own copy of the file.
Writing and comparing
p[5000] = 'Z' needs no system call and no disk read: page 1 is present. The CPU writes Z into frame 7. The page is now dirty, changed in RAM but not yet on disk. Other processes mapping the file see Z at once.
msync() with MS_SYNC asks the kernel to write the mapping's dirty pages back to the file, and waits until that is done. Page 1 goes to disk and is clean again. Without msync, the kernel still writes it back later, on its own schedule.
The usual read(fd, buf, 4096) works differently. The kernel copies the page out of the page cache into the program's own buffer. The same bytes now sit in RAM twice, and the CPU spent time copying them.
Mapping skips the copy, reads pages only when they are needed, and lets many processes share one cached copy. The price: each first touch costs a page fault, and touching a mapped page past the end of the file raises a SIGBUS signal.
What happens
;; step 1/11 · idea
file notes.txt 8192 bytes = 2 pages of 4096
page 0 bytes 0 .. 4095
page 1 bytes 4096 .. 8191 (byte 5000 is 'a')
the whole program (C99 + POSIX):
#define _POSIX_C_SOURCE 200809L
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#define RW (PROT_READ | PROT_WRITE)
int main(void) {
int fd = open("notes.txt", O_RDWR); /* 8192 bytes */
char *p, c;
if (fd < 0) return 1;
p = mmap(NULL, 8192, RW,
MAP_SHARED, fd, 0);
if (p == MAP_FAILED) return 1;
c = p[5000];
p[5000] = 'Z';
msync(p, 8192, MS_SYNC);
printf("%c\n", c);
munmap(p, 8192);
close(fd);
return 0;
}
Each page box shows only byte 5000 of its page, the one the program touches. Page 0 is drawn as … because the program never uses it. Frame 7 is just the free frame this run happens to get. Real kernels read ahead around a fault: Linux reads a window of pages around the faulting page (unless the program calls madvise(MADV_RANDOM)) and may also map nearby cached pages (fault-around). So neighbouring pages, like page 0 here, are often read too. The figure leaves this out. Real CPUs also cache translations in the TLB, so the retried load may first miss in the TLB and then walk the page table. The figure leaves the TLB out.