So far, we’ve been assuming that an address space is unrealistically small and fits into physical memory. But now it’s the time to talk about the real-life situation and take them into consideration

Swap Space

The first thing we can do is to reserve some space on the disk for moving pages back and forth. In OS, we generally refer to such space as swap space, because we swap pages out of memory to it and swap pages into memory from it. To do so, the OS will need to remember the disk address of a given page

The Present Bit

After having some space on the disk, we need to add some machinery higher up in the system in order to support swapping pages to and from the disk.

The way the hardware(or OS, in a software-managed TLB approach) determines if a page is not present in physical memory is through the new information in each page-table entry known as the present bit. If the present bit is 1, it is in the physical memory and the hardware can perform the extraction like before. But if it’s 0, the page is not in memory (most likely in disk) and that’s referred as a page fault.

Upon a page fault, page-fault handler will run and service that fault.

The Page Fault

If a page is not present and has been swapped to disk, the OS will need to swap the page into memory in order to service the page fault. The OS will look up in the PTE to find the disk address and issues the request to disk to fetch the page into memory.

When the disk I/O completes, the OS will update the page table to mark the page as present and update the PPN of the PTE to record the in-memory location of the page, then retry the instruction.

While processing the I/P, the process will be in the blocked state so the OS is free to run other ready processes while page fault is being serviced.

If memory is full, how should we decide what page to evict or replace?

Swapping Policies

Cache Management

Since the main physical memory can be understood as the cache for all virtual memory pages in the system, our goal for the policy is to minimize the number of cache misses and maximize the cache hits. Knowing those will allow us to calculate the average memory access time, AMAT, for a program

$$ \text{AMAT} = T_M + (P_{miss} \times T_D) $$

$T_m$ is the cost of accessing memory, $T_D$ is the cost of accessing disk and $P_{miss}$ is the percentage of miss rate.

The Optimal Replacement Policy

The optimal approach is simply to replace the page that will be accessed furtherest in the future.