Page Replacement Algorithms
1. RAND (Random)
- choose any page to replace at random
- assumes the next page to be referenced is random
- can test other algorithms against random page replacement
2. MIN (minimum) or OPT (optimal) :
- Belady's optimal algorithm for the minimum number of page faults
- replace the page that will be referenced furthest in the future or not at all
- problem: we cannot implement it, because we cannot predict the future
- this is the best case
- can use it to compare other algorithms against
3. FIFO (First In, First Out):
- select the page that has been in main memory the longest
- use a queue (data structure)
- problem: although a page has been present for a long time, it may be really useful
- Windows NT and Windows 2000 use this algorithm, as a local page replacement algorithm
(described separately),
with the pool method (described in more detail separately)
- create a pool of the pages that have been marked for removal
- manage the pool in the same way as the rest of the pages
- if a new page is needed, take a page from the pool
- if a page in the pool is referenced again before being replaced in memory,
it is simply reactivated
- this is relatively efficient
4. LRU (Least Recently Used):
- choose the page that was last referenced the longest time ago
- assumes recent behavior is a good predictor of the near future
- can manage LRU with a list called the LRU stack or the paging stack (data structure)
- in the LRU stack, the first entry describes the page referenced least recently,
the last entry describes to the last page referenced.
- if a page is referenced, move it to the end of the list
- problem: requires updating on every page referenced
- too slow to be used in practice for managing the page table,
but many systems use approximations to LRU
5. NRU (Not Recently Used):
- as an approximation to LRU, select one of the pages that has not been used
recently (as opposed to identifying exactly which one has not been used for
the longest amount of time)
- keep one bit called the "used bit" or "reference bit",
where 1 => used recently and 0 => not used recently
- variants of this scheme are used in many operating systems,
including UNIX and MacIntosh
- most variations use a scan pointer and go through the page frames one by one, in
some order, looking for a page that has not been used recently.
Variations of NRU:
Variation 1: Linear Scanning Algorithm
- this algorithm is sometimes called the Clock Algorithm
or the Second Chance algorithm, but both terms are also used for
the FIFO Second Chance algorithm described below.
- only description of this algorithm in textbook is in Sec. 21.6.2 of
Silberschatz and Galvin, fifth edition
- used in BSD Unix
Method:
- if the requested page is in memory already, change its used bit as 1,
otherwise, execute the following algorithm
Algorithm:
repeat until page has been stored
if scan pointer is at frame whose used bit is 0
then select it
put new page here
set used bit to 1
move scan pointer to the next frame
else
set used bit to 0
move scan pointer to the next frame (wrap around to first frame
if at the last frame)
Example:
- assumes main memory is empty at the start
- page reference sequence is : 3 2 3 0 8 4 2 5 0 9 8 3 2
- one reference bit per frame (called the "used" bit)
P U 3 P U 2 P U 3 P U 0 P U 8 P U 4
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | 0 |* | 3 | 1 | | 3 | 1 | | 3 | 1 | | 3 | 1 | | 3 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | 0 | | | 0 |* | 2 | 1 | | 2 | 1 | | 2 | 1 | | 2 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | 0 | | | 0 | | | 0 |* | | 0 |* | 0 | 1 | | 0 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | 0 | | | 0 | | | 0 | | | 0 | | | 0 |* | 8 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| | 0 | | | 0 | | | 0 | | | 0 | | | 0 | | | 0 |*
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+----
P U 2 P U 5 P U 0 P U 9 P U 8 P U 3
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| 3 | 1 |* | 3 | 1 |* | 5 | 1 | | 5 | 1 | | 5 | 1 | | 5 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| 2 | 1 | | 2 | 1 | | 2 | 0 |* | 2 | 0 |* | 9 | 1 | | 9 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| 0 | 1 | | 0 | 1 | | 0 | 0 | | 0 | 1 | | 0 | 1 |* | 0 | 1 |*
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| 8 | 1 | | 8 | 1 | | 8 | 0 | | 8 | 0 | | 8 | 0 | | 8 | 1 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+---+
| 4 | 1 | | 4 | 1 | | 4 | 0 | | 4 | 0 | | 4 | 0 | | 4 | 0 |
+---+---+ +---+---+ +---+---+ +---+---+ +---+---+ +---+----
P U 2 P U
+---+---+ +---+---+
| 5 | 1 |* | 5 | 0 |
+---+---+ +---+---+
| 9 | 1 | | 9 | 0 |
+---+---+ +---+---+
| 0 | 0 | | 2 | 1 |
+---+---+ +---+---+
| 8 | 0 | | 8 | 0 |*
+---+---+ +---+---+
| 3 | 1 | | 3 | 1 |
+---+---+ +---+---+
* = indicates the pointer which identifies the next location to scan
P = page# stored in that frame
U = used flag, 0 = not used recently
1 = referenced recently
Variation 2:
- reset all bits to zero on a regular interval, say 60 times/second
Variation 3 (Additional-Reference-Bits Algorithm):
- Silberschatz, Sec. 10.4.5.1
- Keep several bits, say 32 bits, to represent
the history of usage of the page.
- Each bit corresponds to one time interval,
with the leftmost bit indicating the most recent time interval
and the rightmost bit indicating the earliest time interval
- Shift the bits to the right one position at the end of each time interval.
- Clock ticks determine intervals, as with variation 2
- The value stored in the bits,
when interpreted as an unsigned binary integer,
is used to choose the page to remove.
- The page with the lowest value for the bits is chosen;
if a tie occurs, we will arbitrarily choose the frame with the lowest number
from among those that tied.
- When a page is chosen, it is replaced with the new page
and the used bits are cleared, except for the leftmost
bit, which is set to 1.
- The page with the highest value is considered the most valuable.
Example
- I will use the leftmost bit to indicate whether
the page has been referenced in the current time interval.
- In the textbook, this information is stored in a separate Reference bit
and the U bits are only used for the history;
4 U bits with my approach is conceptually identical to
1 Reference bit plus 3 U bits with the textbook approach.
- Assume there are 5 frames in memory, and each frame
has a Page field (P) and 4 used bits (U3, U2, U1, and U0).
Problem: Show which page is in each frame
and the contents of the U bits for every page,
at the end of each time interval, for
the the following page reference string,
where T marks the end of each time interval:
3, 2, 3, T, 8, 0, 3, T, 3, 0, 2, T, 6, 3, 4, 7
Initial state
P U3 U2 U1 U0
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
During first time interval, pages 3, 2, and 3 are referenced.
First, page 3 is loaded.
To select a location for page 3, we examine the U bits.
The last three frames all have U binary values of 0000,
which corresponds to the
decimal integer 0.
We choose the first of these, load page 3 into it, and set the
U3 bit to 1.
Page 2 is handled in a similar manner.
Then U3 is set to 1 again (no noticeable effect) when it is
referenced again.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| 2 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
At the end of the first time interval,
all U bits are shifted right one position.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| 2 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
During second time interval, pages 8, 0, and 3 are referenced.
Pages 8 and 0 are loaded into empty positions and their U3 bits are
set to 1.
Then the U3 bit for page 3 is set to 1.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 1 | 0 | 0 |
+---+---+---+---+---+
| 2 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| 8 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
At the end of the second time interval,
all U bits are shifted right one position.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| 2 | 0 | 0 | 1 | 0 |
+---+---+---+---+---+
| 8 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| 0 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
During third time interval, pages 3, 0, and 2 are referenced, so
U3 is set for pages 3, 0, and 2.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 1 | 1 | 0 |
+---+---+---+---+---+
| 2 | 1 | 0 | 1 | 0 |
+---+---+---+---+---+
| 8 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+
| 0 | 1 | 1 | 0 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
At the end of the third time interval,
all U bits are shifted right one position.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 0 | 1 | 1 | 1 |
+---+---+---+---+---+
| 2 | 0 | 1 | 0 | 1 |
+---+---+---+---+---+
| 8 | 0 | 0 | 1 | 0 |
+---+---+---+---+---+
| 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| - | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
During third time interval, pages 6, 3, 4, and 7 are referenced.
First page 6 is loaded and its U3 bit is set to 1.
Then the U3 bit for page 3 is set to 1.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+
| 2 | 0 | 1 | 0 | 1 |
+---+---+---+---+---+
| 8 | 0 | 0 | 1 | 0 |
+---+---+---+---+---+
| 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| 6 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
Continuing in the same time interval,
when page 4 is required,
memory is full, so we choose the
page with the lowest U value, which is page 8.
Page 4 replaces page 8 and the U bits are set to 1000.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+
| 2 | 0 | 1 | 0 | 1 |
+---+---+---+---+---+
| 4 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| 6 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
Then
when page 7 is required,
memory is again full, so we choose the
page with the lowest U value, which is page 2.
Page 7 replaces page 2 and the U bits are set to 1000.
P U3 U2 U1 U0
+---+---+---+---+---+
| 3 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+
| 7 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| 4 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| 6 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+
6. FIFO Second Chance:
- variation of FIFO; textbook includes this algorithm
as a type of NRU algorithm
- have a circular queue of page frames, originally in FIFO order
- when a page must be selected, look at current position in the queue
- if the selected page has been referenced
we give it "one more chance" by setting the "used" bit to zero
and keep on looking
- somewhat similar to NRU/variation 1 (the scanning algorithm) in
that a search is made with a scanning pointer, but in the Second Chance
algorithm, the search is made through a circular queue stored in FIFO order,
instead of according to frame number
6.
Enhanced FIFO Second Chance:
- variation of FIFO second chance algorithm;
textbook includes this algorithm as a type of NRU algorithm
- pays attention to whether a page has been changed
as well as to whether it has been referenced
- Four cases, based on (R,M),
the pair of the Referenced bit and the Modified bit:
- (R,M) = (0,0) neither recently used nor modified -- best to replace
- (R,M) = (0,1) not recently used but modified -- second choice
- (R,M) = (1,0) recently used but not modified -- third choice
- (R,M) = (1,1) recently used and modified -- fourth choice
- Perform up to four passes over the circular queue, considering pages in
each class at a time. Choose the first page encountered in the lowest nonempty
class.
- MacOS used this
7. Working Set (WS)
- Directly address the problem of thrashing
- thrashing: when the computer system is preoccupied with paging, i.e.,
CPU has little to do but there is heavy disk traffic moving pages to and
from memory with little use of those pages
- working set: the pages that a process has used in the last w time intervals
- select any page that is not in the working set
- global: not in the working set of any ready process
- if no such page exists, swap out some process
- the medium-term scheduler places a process in the waiting-for-memory queue
Working Set Policy
- Restrict the number of processes in the ready list so
that all can have their working set of pages in memory
- Before starting a process, make sure its working set is in main memory
- Too expensive in practice, but there are some good approximations
8.
Page Fault Frequency algorithm (PFF) -- variation of Working Set
- when a page fault occurs, if the last page fault
for that process was recent, then increase the size
of its working set (up to a maximum)
- all processes start with a default ws size
- load first code pages, first data pages, first stack pages
- if a process has not faulted recently, reduce the size of its ws
i.e. remove all pages not used recently ("used bit")
- used in Windows NT and Windows 2000 as a global page replacement
algorithm (described separately)
-
they refer to it as automatic working-set trimming
- also, in WinNT, can call the process object service to
change working-set min and max for a process,
up to a defined max and min.
Pool Method
- once a page has been selected for replacement/removal, it joins a pool of pages waiting for
removal
- if referenced while in the pool, it is immediately reactivated and removed from the pool
- dirty pages in the pool are written to disk
whenever an i/o device is available,
- if a page has an up to date copy on disk, it is called
a clean page
- the "page cleaning process" is the process that writes the dirty pages
to disk
- when the pool is full and a page is needed,
remove any clean page
from the pool and reallocate it to another process
- only have to stop the running process and wait for a page out if there are no clean pages
- page faults take twice as long if we have to write dirty pages to disk
- often save a number of pages and write them out together in order of disk address
- used in Windows NT and Windows 2000.
Shared Pages
- pages can be marked as READ-ONLY, READ-WRITE, etc. according to what segment
they are in
- processes can share pages that are marked as "READ-ONLY"
- for example, the pages containing the executable code will be the same for
several users who are all running the same program ("vi")
- a page is kept if any process needs it
- other pages are marked as "READ-WRITE" or "COPY-ON-WRITE",
which means that a separate copy will be made
for any sharing process that changes the page
- with COPY-ON-WRITE pages, if any process tries to change a page,
then a copy is made, that process's page table is updated, and then
execution continues
Example: fork( )
- immediately after a fork, the two processes share as many pages as possible,
even the writable ones
- from a statement such as
at least one page is different (private, marked as READ-WRITE)
- code pages are marked as "READ-ONLY" and are sharable
- shared data pages are marked as "COPY-ON-WRITE"
- as long as the data on a page is not changed, the processes will continue
to share the page
- most versions of UNIX provide shared pages
Paged Segmentation
- divide big segments into pages
- described in some more detail under Segmentation
Paging Costs
- internal fragmentation
: wasted space within a page
- external fragmentation
: wasted space between pages ( = zero )
- table overhead
: space needed for the page table
- transportation cost
: cost of moving pages in and out of main memory
Table of Contents