The Least Recently Used (LRU) page replacement policy

  • The LRU page replacement policy:

    • Replace (= take out) the program page that has not been used for the longest time

  • Implementing the LRU page replacement policy:

     Assume the program has been allocated the following memory frames:
    
                                            Memory frames:
    				        +-----------+
    				        |           |
    				        +-----------+
    				        |           |
    				        +-----------+
    				        |           |
    				        +-----------+
    				          .........  
    				        +-----------+
    				        |           |
    				        +-----------+
    

The Least Recently Used (LRU) page replacement policy

  • The LRU page replacement policy:

    • Replace (= take out) the program page that has not been used for the longest time

  • Implementing the LRU page replacement policy:

     Maintain a time stamp on when a memory frame was accessed 
    
       Time stamp value                     Memory frames:
       increases with time		        +-----------+
    				    123 |           |
    				        +-----------+
    	 Earliest timestamp ------> 111 |           |
    				        +-----------+
    				    132 |           |
    				        +-----------+
    				          .........  
    				        +-----------+
    	   Latest timestamp ------> 145 |           |
    				        +-----------+
    

The Least Recently Used (LRU) page replacement policy

  • The LRU page replacement policy:

    • Replace (= take out) the program page that has not been used for the longest time

  • Implementing the LRU page replacement policy:

     Replace the page with the smallest time stamp value
    
                                            Memory frames:
                                            +-----------+
    				    123 |           |
    				        +-----------+
    	 Replace this page ------>  111 |           |
    	 (Least Recently Used)	        +-----------+
    				    132 |           |
    				        +-----------+
    				          .........  
    				        +-----------+
    	                            145 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Suppose the running program is assigned to use  3  memory frames

      Without loss of generality, we use the frame 0, 1, 2
      to study the performance of the LRU policy
    
                                            Memory frames:
    				        +-----------+
    	                              0 |           |
    	                                +-----------+
    	                              1 |           |
    				        +-----------+
    				      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Initialization:

      All frames are empty and all timestamps = 0
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   0          0 |           |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   0   ---> page fault

      Read page 0 from disk and place in memory frame 0(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   0          0 |           |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 1 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page fault

      Read page 4 from disk and place in memory frame 1(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 2 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   2          1 |     4     |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   1   ---> page fault

      Read page 1 from disk and place in memory frame 2(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   2          1 |     4     |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 3 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   2          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT !!    Update timestamp of page 4 !!!

      # page faults = 3
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	    ------------>  4          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   2   ---> page fault

      Read page 2 from disk and place in memory frame 0(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   1          0 |     0     |
    	                                +-----------+
    	                   4          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 4 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   5          0 |     2     |
    	                                +-----------+
    	                   4          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4 !!!

      # page faults = 4 
    
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   5          0 |     2     |
    	                                +-----------+
    	    -------------> 6          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   3   ---> page fault

      Read page 3 from disk and place in memory frame 2(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   5          0 |     2     |
    	                                +-----------+
    	                   6          1 |     4     |
    				        +-----------+
    			   3	      2 |     1     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 5 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   5          0 |     2     |
    	                                +-----------+
    	                   6          1 |     4     |
    				        +-----------+
    			   7	      2 |     3     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 5 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   5          0 |     2     |
    	                                +-----------+
    	     ------------> 8          1 |     4     |
    				        +-----------+
    			   7	      2 |     3     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   2   ---> page HIT    Update timestamp of page 2

      # page faults = 5 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	    -------------> 9          0 |     2     |
    	                                +-----------+
    	                   8          1 |     4     |
    				        +-----------+
    			   7	      2 |     3     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 5 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   9          0 |     2     |
    	                                +-----------+
    	      ----------> 10          1 |     4     |
    				        +-----------+
    			   7	      2 |     3     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   0   ---> page fault

      Read page 0 from disk and place in memory frame 2(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   9          0 |     2     |
    	                                +-----------+
    	                  10          1 |     4     |
    				        +-----------+
    			   7	      2 |     3     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 6 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   9          0 |     2     |
    	                                +-----------+
    	                  10          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 6 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   9          0 |     2     |
    	                                +-----------+
    	       ---------> 12          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   1   ---> page fault

      Read page 1 from disk and place in memory frame 0(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                   9          0 |     2     |
    	                                +-----------+
    	                  12          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 7 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	                  12          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 7 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	      ----------> 14          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   2   ---> page fault

      Read page 2 from disk and place in memory frame 2(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	                  14          1 |     4     |
    				        +-----------+
    			  11	      2 |     0     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 8 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	                  14          1 |     4     |
    				        +-----------+
    			  15	      2 |     2     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 8 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	         -------> 16          1 |     4     |
    				        +-----------+
    			  15	      2 |     2     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   3   ---> page fault

      Read page 3 from disk and place in memory frame 0(smallest timestamp)
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  13          0 |     1     |
    	                                +-----------+
    	                  16          1 |     4     |
    				        +-----------+
    			  15	      2 |     2     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Result:

      # page faults = 9 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  17          0 |     3     |
    	                                +-----------+
    	                  16          1 |     4     |
    				        +-----------+
    			  15	      2 |     2     |
    				        +-----------+
    

Example of the Least Recently Used (LRU) page replacement policy

  • Summary of the page requests made by the running program:

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

  • Requested page =   4   ---> page HIT    Update timestamp of page 4

      # page faults = 9 
      
    
                           Timestamp        Memory frames:
    				        +-----------+
    	                  17          0 |     3     |
    	                                +-----------+
    	       ---------> 18          1 |     4     |
    				        +-----------+
    			  15	      2 |     2     |
    				        +-----------+
    

Total # page faults = 9