Virtual Memory and Paging

Use pages to map virtual memory to physical memory. This prevents external fragmentation by dividing memory into fixed-size pages, and internal fragmentation by making the units of allocation smaller.

Fragmentation

Virtual address space is divided into pages, and physical address space is divided into frames. The page table maps pages to frames. The page table is stored in memory, and the page table base register (PTBR) points to the page table. The page table is indexed by the page number, and the value at that index is the frame number.

From the programmers perspective, memory is a giant contiguous block, completely independent of the physical memory and hardware.

Protection

One processes cannot "name" or address the memory of another process. This provides protection between processes.

Set the first page to be invalid so that if a process tries to access it (NULL pointer), it will cause an exception.

Address Translation

Page tables are managed by the operating system, and are stored in memory. There is one PTE for each page, ie one PTE per VPN. The page table maps VPNs to PFNs. Each process has its own page table, and the PTBR points to the page table.

Shared Frames

Multiple processes can share the same frame. This is useful for shared libraries, and for shared memory between processes. Can also be used when implementing copy-on-write (COW) to optimize things like read-only fork, or exec.

Page Table Entries

More functionality to the PTEs:

More out there.

Advantages of Paging

Disadvantages of Paging

Paged Virtual Memory

Page Faults

Demand Paging

Page Replacement

Page Replacement Algorithms

Belady's Optimal Algorithm
FIFO
LRU
Approximate LRU
LRU Clock

How do you load a program?

Locality

Locality means paging can be infrequent, and the OS can bring in multiple pages at once. This assumes that:

Local vs Global Page Replacement

Local page replacement means that each process has its own set of pages that it is replacing. Global page replacement means that the OS can choose any page to replace, regardless of which process it belongs to. Linux uses global page replacement.

This is typically implemented by keeping a pool of free pages, and when a page is needed, the OS can choose any page to evict. This is useful because it allows the OS to make better decisions about which pages to evict, and can reduce the number of page faults.

Working Set Model

$$ WS(t,w) = {\text{pages P such that P was referenced in the time interval } (t, t-w)} $$

$|WT(t, w)|$ is the number of pages in the working set at time $t$, and varies with time. During a time interval with particularly bad locality, the working set can be very large.

The goal is to reduce page faults by keeping the working set in memory. Thrashing is when a process is spending more time paging than executing, and keeping the working set in memory can help prevent thrashing.

Hard vs Soft Page Faults