Chapter 8
Virtual Memory
Implementing LRU
Perfect LRU:
- Keep a time stamp for each page with the time of the last access.
- Throw out the LRU page.
Problem:
O/S must record time stamp for each memory access. To find the page that
should be thrown out, O/S has to look at all pages.
Another way:
- Keep a stack of all pages, where the top of the stack is the most
recently used page and the bottom of the stack is the least recently used.
- When a page is accessed, move the page to the top of the stack.
- Doubly link the list.
Problem:
Still too expensive. The O/S must make 6 pointer references on each memory
access.
Approximating LRU
Hardware: Single reference bit. On each access to a page, the hardware
sets the reference bit to 1.
Second Chance (Clock) Algorithm:
- O/S arranges pointers to physical page frames in a circle, with a
"clock hand'' pointing to the first page.
- On a page fault, the O/S:
- Advances the clock hand;
- If the reference bit is 0, replaces the page and sets its reference bit
to 1;
- If the reference bit is 1, sets the bit to 0 and advances the clock hand.
Paging Algorithms - Clock Algorithm
Will the clock algorithm always find a page to replace?
What if all reference bits are 0?
What is happening when the hand is moving quickly?
What is happening when the hand is moving slowly?
One way to view the clock algorithm is as a crude partitioning into two
categories: young pages and old pages. Why not partition the pages into more
than two categories?
Paging Algorithms - Nth Chance Algorithm
Idea: Do not throw a page out until the clock hand has swept by it n
times.
- The hardware sets only one reference bit, but the O/S periodically shifts
the reference bit to the right in an N bit byte.
- For example, with a 4 bit counter, when a page is loaded, its status is 1000.
- If the page is not referenced in the second or third interval, its status
becomes 0010.
- If it is referenced in each of the next 4 intervals, its status is 1111.
- The page with the lowest binary number in its status bits is the LRU
page.
- Is this number unique?
- Why pick a large value for N?
- Why pick a small value for N?
Paging Algorithms - Enhanced Second Chance
It is cheaper to replace a page that has not been written to (a "clean" page)
since it need not be written back to disk. Idea: Have the O/S skip over
modified pages an extra time.
- Hardware keeps a "modify" bit. When this bit is set, then the page is
modified (different from the copy on disk).
- The reference and modify bits form a pair (r,m) where
- (0,0) -- neither recently used or modified. Replace this page!
- (0,1) -- not recently used but modified. Not as good since O/S must
write this page back to disk.
- (1,0) -- recently used but unmodified. Will probably be used again soon,
but O/S need not write it out before replacing it.
- (1,1) -- recently used and modified. Not a good candidate for replacement.
The Algorithm:
- On a page fault, the O/S searches for the first page in the lowest
non-empty class.
- If the O/S finds a (0,0) page, it replaces it.
- Otherwise, the O/S records the first instances of (0,1), (1,0), and
(1,1) pages it finds, puts a 0 in the reference bit and goes to the next page
frame.
- If the hand goes all the way around, replace the first page found in the
lowest non-empty class.
Multiprogramming Replacement Policies
Thrashing: Memory is over-committed and pages are tossed out while they
are still in use. Instead of maintaining the illusion of RAM speed, memory
references slow down to near disk access time.
What can we do in a multiprogramming environment to limit thrashing?
- Global replacement: Put all pages from all processes in one pool.
- Advantages: Flexible, adjusts to divergent processes needs.
- Disadvantages: Thrashing becomes more likely. Why?
- Per-process replacement: Each process has its own pool of pages.
- Advantages: Thrashing is less likely since the process competes only with itself.
- Disadvantages: The O/S has to figure out how many pages to give
each process and, if the working set grows dynamically, adjust its allocation.
- Per-user replacement:
- Advantages:} User fairness.
- Disadvantages: Inflexible.
Per-process replacement -- working sets
Implementing working sets
- We modify the clock algorithm so that it sweeps once every T seconds,
counting how many pages have been accessed since the last sweep. If a page
has been accessed, include it in the process's working set.
- Given the working set for each process, {\em balance} the set of
processes. If all processes fit in memory, we're done. If not, kick out the
biggest consumer of pages.
What happens if T is too small?
What happens if T is too large?