More Fun With Virtual Memory: Paging, Page Replacement Strategies & Analysis, Page Table Management

Backing Store

All pages of the memory must be copied to backing store, i.e., disk, so that if their space is needed in memory they can be restored from disk. The analogy to swap space should be pretty obvious: both are copies of a runnable process on disk. The distinction is that paging backing store can contain copies of memory of processes that are currently executing, while swapped processes are always stopped. Paging systems use backing store, and allocate and use it on a per-page basis; swapping systems use swap space and allocate it in units of entire processes' address spaces.1 That said, people frequently say swap when they mean backing store; UNIX(R) certainly does.

It's expensive (in time) to move pages onto and off of backing store, so OSes take various steps to avoid it. Some systems page executables and other read-only memory from the filesystem directly, avoiding the copy to backing store. Others overcommit backing store by only copying a page to backing store if it must be removed from memory for some reason. The tradeoffs here are similar to those for swap spaces.

Page Faults

When a page is not present, the OS faces a page fault. The following are the basic steps in servicing a page fault:

The whole process is rather expensive, which is why adding memory to a paging system generally2 speeds up a paging system. More memory makes the expensive page faults less frequent.

Page Replacement

Page replacement is another form of scheduling, so these algorithms should look familiar:

Theory of Page Replacement

Paging algorithms are analyzed by studying the effects of the algorithm on reference strings - that is sequences of page references against which the algorithm is studied. Here's an example:

Reference String 1 3 0 2 0 1 2 1
 







  1 3 0 2 0 1 2 1
    1 3 0 2 0 1 2
 







      1 3 3 2 0 0
        1 1 3 3 3
 







l c c c c c c c c .                
Page Faults P P P P - P P -
Distance String 2 4 3 2

Belady's Anomaly

A group of researchers, led by a fellow named Belady, discovered a surprising fact about FIFO paging. It's possible (though unlikely) that adding memory to a FIFO paging system increases the number of faults. The example is below:

  0 1 2 3 0 1 4 0 1 2 3 4
 











Youngest Page 0 1 2 3 0 1 4 4 4 2 3 3
 











    0 1 2 3 0 1 1 1 4 2 2
 











Oldest Page     0 1 2 3 0 0 0 1 4 4
 











a c c c c c c c c c c c c .                        
  P P P P P P P     P P  
  0 1 2 3 0 1 4 0 1 2 3 4
 











Youngest Page 0 1 2 3 3 3 4 0 1 2 3 4
 











    0 1 2 2 2 3 4 0 1 2 3
 











      0 1 1 1 2 3 4 0 1 2
 











Oldest Page       0 0 0 1 2 3 4 0 1
 











a c c c c c c c c c c c c .                        
  P P P P     P P P P P P

This result has been generalized, and the key property is called the stack property: that increasing the size of memory only adds contents to it. In the FIFO case above, there are different contents in the upper 3 page frames in memory for several states. FIFO is not a stack algorithm. Any non-stack algorithm can display Belady's anomaly. LRU is a stack algorithm.

The Working Set

The working set of a process is the set of pages that it will reference in the near future - the pages it's working on. (That's intentionally vague, of course) If this set is in memory, the process will not fault. Consequently, some OSes try to identify and keep the working set in memory.

Just letting a process pull in its pages as it needs them is called demand paging, and is a common form of paging. In systems that try to identify the working set will try to prefetch pages that it thinks are or will be in the working set. A simple example is a process that sequentially fills an array from memory. It's a reasonable idea to prefetch pages sequentially.

More complex methods involve keeping the LRU approximations around for each process, and if the counters are non-zero, the page is in the working set. A variant of the clock algorithm that takes the working set counter into account rather than the referenced bit is called wsclock.

Nachos questions
Converted from groff by Ted Faber
Please mail me any problems or comments.