Page Replacement Algorithms



1. RAND (Random)

2. MIN (minimum) or OPT (optimal) :

3. FIFO (First In, First Out):

4. LRU (Least Recently Used):

5. NRU (Not Recently Used):


Variations of NRU:

Variation 1: Linear Scanning Algorithm


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:

  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:

Variation 3 (Additional-Reference-Bits Algorithm):


Example
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:

6. Enhanced FIFO Second Chance:


7. Working Set (WS)

8. Page Fault Frequency algorithm (PFF) -- variation of Working Set


Pool Method

Shared Pages

Paged Segmentation

Paging Costs




Table of Contents