CS 3733 Operating Systems Notes: Page Replacement
- When a new page needs to be brought in, there will not always be a free frame.
- Some process needs to give up a frame.
- Which page should be replaced?
- You want to choose the page which is least likely to be referenced in the future,
but this might not be easy to determine.
When a page is replaced you need to know whether the replaced page has
been modified.
- If it has, the page must be written back to disk.
- Otherwise, it is just necessary to mark it as invalid.
- One bit can be used to keep track of whether a page has been modified
since it was loaded into memory from disk.
- Note that is is not necessary to write it back to disk if the page
has been read but not written.
Four Page Replacement Algorithms
1: FIFO - first-in-first-out
- This is very simple to implement - just keep a FIFO queue and
add an entry to the back when a page is placed in a frame.
- Unfortunately the most heavily used pages are often the ones
brought in first.
- Note that the FIFO queue gets updated only when there is a page
fault, not on each reference. This implies that it is possible
to generate reference strings which do worse when there are more
frames.
Example
Calculate the number of page faults obtained for the following page reference
sequence:
1,2,3,4,1,2,5,1,2,3,4,5
When:
- The memory has 3 frames: ans: 1,2,3,4,1,2, 5,1,2,3,4,5 (9 faults)
- The memory has 4 frames: ans: 1,2,3,4,1,2,5,1,2,3,4,5 (10 faults)
This is sometimes called Belady's anomaly.
2: Optimal Replacement
Look to the future.
Impossible to implement, but gives the best possible result.
Example:
Optimal with three frames: 1,2,3,4,1,2,5,1,2,3,4,5
7 page faults.
3: Least Recently Used
- Each page keeps time of last use.
- Since this must be done on every reference (in contrast to FIFO which
only needs an update when the page is put in the frame), the bookkeeping
must be done in hardware.
(in fact it isn't done for page replacement,
but it is often used for TLB replacement).
- LRU cannot be implemented with current technology on modern machines
because of the amount of space needed and the complexity of the
algorithm.
- Note that information needs to be updated on every memory access.
- LRU can be used for the TLB which has a small number of entries.
- LRU would be a good algorithm if it could be implemented, so
real algorithms try to approximate it.
Example:
LRU with three frames: 1,2,3,4,1,2,5,1,2,3,4,5
10 page faults.
Least Recently Used Approximations
Using a reference bit (idea):
- Keep a reference bit in the page table entry. (Or keep a register with
all of the bits together.)
- When a page is referenced, the bit is set.
- Use this information to replace unused pages first.
For example, every so often clear the bits
When time to replace a page, do not replace those with bit set.
4: Second Chance = FIFO + reference bit
- Keep pages in a FIFO list.
- If a page is selected for replacement, look at reference bit.
If 0 replace.
If 1, clear the reference bit and put it at end of FIFO list
(give it a second chance).
- Pages which are used often won't get moved out.
Next Notes
Back to CS 3733 Notes Table of Contents