virtual memory, MMUs, CPUs, RAM, page tables, page faults, forking
At its core, virtual memory is a clever trick that allows a computer to act as though it has more memory than it really does. It creates an illusion for programs, letting them think they have a large, private memory space, even though the actual physical memory (RAM) is limited and shared among all programs.
Think of your computer's memory as a library:
Programs Think They Have Their Own Memory:
Each program gets its own virtual memory space. For example, if two programs are running, they both think they're using the same memory addresses, but these addresses are mapped to different physical locations.
Memory Management Unit (MMU):
The MMU is hardware that translates virtual addresses (the pretend bookshelf) into physical addresses (the real bookshelf). It uses the page table, a structure maintained by the operating system (OS), to find where the data is located.
Page Table:
The page table maps virtual addresses to physical addresses. Each program has its own page table. It includes metadata about each page, such as:
Swapping (or Paging):
If there's not enough physical memory to hold everything, the OS temporarily moves some data to the hard drive (like putting books in the basement). When the program needs those books again, the OS swaps them back to the shelves. This is slower than using RAM but ensures programs don't crash due to memory shortages.
Demand Paging:
Only the portions of a program actively being used are loaded into RAM. This saves space by delaying the loading of unused sections.
A page fault occurs when a program accesses data not currently in RAM. Here's the process: