Virtual Memory
What it is Intended to Do
Virtual memory is intended to provide a process (and by extension a user) with the illusion that it can access as much memory as could possibly be accessed by the number of bits in an address (e.g., 32 bits) regardless of whether that much RAM is available or not and whether there are other processes active in RAM or not.
Why it is Cool
- Virtual memory allows a compiler to translate a program into machine language as if that program will exist by itself in RAM as it is executing.
- It allows a program to access all virtual memory addresses as if that much RAM were available.
How it Works
When a process is started in by the OS, two parts are created:
- An execution image of the process that includes its code, its static variables, a stack, and a heap. This execution image is kept by the OS on the hard drive in an area referred to as the "swap space," as sections of the execution image are brought into and out of RAM by the OS.
- A process control block that keeps track of the state of the process when it is interrupted so that it can be restarted later.
The OS must:
- construct and maintain data structures for each process that keep track of where the execution image of that process is in swap space
- bring in sections, called pages, of the execution image of a process from the swap space and put them in to RAM as they are needed
- construct and maintain data structures, called page tables, for each process to keep track of which pages of the execution image of that process is in RAM and where they are in RAM
- construct and maintain a data structure that keeps track of which frames of RAM (which have the same size as pages in the virtual execution image of the process) are in use, which process's pages are in those frames, and frames are free.
- put a value in a special page table register in the processor that is the address of the page table for the currently runing process.
The hardware must:
- resolve each virtual address that appears in the PC or as an operand address in an instruction to its real physical address in RAM by following the page tables pointed to by the page table register.