Abstract

Modern operating systems utilize virtual memory indirection to implement sophisticated performance optimizations beyond standard process isolation. Features like Shared Memory enable high-speed inter-process communication, Copy-on-Write (CoW) eliminates redundant memory duplication during process creation (fork), and Memory-Mapped Files (mmap) allow file system I/O to be performed directly using standard memory instructions.

  • Category: Advanced OS Virtual Memory Capabilities
  • Primary System Calls: shm_open, fork, mmap.
  • Core Driver: Manipulating Page Table Entries and protection bits to trigger lazy evaluation.

Shared Memory

By default, virtual memory enforces strict process isolation—each process possesses a disjoint set of physical memory frames. Shared Memory overrides this default by configuring page table entries in two or more distinct processes to map to the exact same physical page frames in RAM.

  • API Usage: Configured via Unix system calls such as shm_open and shm_unlink.
  • Address Flexibility: The shared physical page frame can be mapped at different virtual addresses in each process’s address space.
  • Zero-Copy Efficiency: Data written by one process is instantly accessible to other processes without passing through kernel buffers.

Copy-on-Write (CoW)

When a process executes fork() to create a child process, copying the entire address space in physical RAM is extremely expensive and often wasted if the child immediately calls exec().

Copy-on-Write (CoW) optimizes process creation by sharing physical pages lazily:

  1. Lazy Page Sharing: During fork(), parent and child page table entries are set to point to the same physical pages and marked as Read-Only.
  2. Protection Fault: If either process attempts to write to a shared page, the hardware detects a protection violation and traps to the OS kernel.
  3. Page Replication: The kernel allocates a new physical frame, copies the 4 KB page contents, updates the faulting process’s PTE to point to the new frame with Read/Write permissions, and restarts the write instruction.

Memory-Mapped Files (mmap)

Standard file I/O uses open(), read(), and write() system calls, which require copying data between kernel disk buffers and user-space memory buffers.

With Memory-Mapped Files (mmap), the OS maps file system blocks directly into the application’s virtual address space:

  • Direct Instruction I/O: Processes access file contents using standard pointer dereferences and memory instructions (e.g., lw and sw).
  • Lazy Demand Paging: Pages of the file are loaded into RAM lazily via Demand Paging as they are referenced.
  • Automatic Synchronization: Modified pages are marked dirty in their PTEs and written back to disk by the kernel page flushing subsystem.