Course contentsShow
← Linux
Lesson 655 of 1,8197. Storage Management and Logical VolumesFree lesson

Kernel memory mapping of files

Explain how mmap integrates storage files into process memory spaces.

In this lesson, you will learn how the Linux kernel uses memory mapping to bridge the gap between persistent storage files and your process’s address space.

Bringing Files into Memory

Normally, when your application needs to read or write a file, it uses system calls like read() or write(). These functions require the kernel to copy data from the disk into the kernel’s page cache, and then copy that data again into your process's private memory. This is known as buffered I/O.

Memory mapping (via mmap) changes this paradigm. Instead of moving data back and forth, the kernel updates your process’s page tables to point directly to the physical pages in the kernel’s page cache where the file resides. Effectively, the file’s contents appear as if they are simply another part of your process’s RAM.

How it Integrates

When you map a file, the kernel does not immediately load the entire file into memory. Instead, it performs "lazy loading":

  1. Setup: The kernel reserves a range of virtual addresses in your process memory.
  2. Page Fault: When your code attempts to access an address within that range, a "page fault" occurs.
  3. Resolution: The CPU pauses your process, and the kernel intercepts the fault. It looks up the corresponding block on the disk, reads it into the page cache, and maps that page directly into your process's memory space.
  4. Resumption: Your process resumes, unaware that the data was just fetched from the disk.

This allows for incredibly efficient handling of large files, as the kernel only keeps the parts of the file you are actively using in physical memory.

// Conceptual view:
// The pointer 'ptr' now acts as an array representing the file content.
// Any change to 'ptr[0]' is eventually written back to the storage
// device by the kernel's memory management subsystem.
char *ptr = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Because this mapping is shared, if two different processes map the same file, they are looking at the exact same physical memory pages in the kernel's cache. This makes mmap a powerful tool for both memory-efficient file access and inter-process communication.

Key Takeaway: Memory mapping allows the kernel to bypass redundant data copying by pointing your process's virtual memory directly to the file's pages in the kernel cache, making disk files behave like local RAM.