In this lesson, you will learn how to bypass traditional read/write buffers by mapping file segments directly into your process's virtual memory space.
Why Avoid Standard read() and write()?
Usually, when you interact with a file, the kernel copies data from the disk into a kernel buffer, and then copies it again into your process's memory. For large files—like massive datasets or high-definition logs—this double-buffering becomes a significant performance bottleneck. Memory-Mapped I/O (MMIO) allows you to "map" a file region directly into your process's address space. Once mapped, the file acts as if it were a massive array already loaded in RAM.
Mapping the File
Instead of calling read(), you use the mmap system call. This instructs the kernel to associate a portion of the file with a pointer in your program. When your code accesses that pointer, the hardware triggers a page fault, and the kernel lazily loads that specific chunk of the file from disk directly into physical memory.
Here is a conceptual look at how you point to a file segment:
// Mapping the first 4KB of a file starting at offset 0
void *data = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
// Now, you can access the file content like a standard array:
char first_byte = ((char *)data)[0];
Efficiency Through Demand Paging
Because you are mapping the file into the virtual address space, you aren't actually loading the entire file into RAM at once. You are only loading what you touch. If you only read the first and last megabytes of a gigabyte-sized file, the Linux kernel manages those specific pages for you automatically. This is significantly more efficient than allocating a giant buffer and reading the whole file, which would waste memory and thrash the disk I/O controllers.
When you are finished, you simply "unmap" the segment, allowing the system to reclaim those memory pages gracefully. This strategy is essential for high-performance applications that need to process large files without exhausting system resources.
Key Takeaway: Memory-mapping turns disk files into virtual memory segments, eliminating redundant copying between kernel space and user space while leveraging demand paging for optimal performance.