The Second Chance page replacement policy

  • The Second Chance page replacement algorithm is an approximate algorithm for the LRU algorithm

  • Information used in the Second Chance algorithm:

    • Each memory frame has a SecondChance bit

    • When the (SecondChance bit == 1), the page in the memory frame is given a second chance (i.e., not replaced)

  • The Second Chance page replacement algorithm is:

    • Based on the FIFO algorithm

    • Uses the SecondChance bit to detect whether a page has been used recently:

      • Each time a memory frame is used, its SecondChance bit is set

        - This will give the page in the memory frame a second chance

The Second Chance page replacement policy

  • When there is a page hit accessing a program page in memory frame  k :

    • Set:    MemoryFrame[ k ].SecondChance =  1 

  • Second Chance page replacement algorithm using  M  memory frames:

     When a page fault occurs and a new page is fetched from disk:
    
        /* ------------------------------------
           Find a page with no second chance
           ------------------------------------ */
        while ( MemoryFrame[next].SecondChance == 1 )
        {  // Give page another chance
           MemoryFrame[next].SecondChance = 0;   // Take away 2nd chance
           next = (next + 1)%M;    // Try next memory frame
        }
    
        Place new page in MemoryFrame[next];
    
        next = (next + 1)%M;    // Use next memory frame for page fault
    

Example of the Second Chance 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 Second Chance policy
    
                                            Memory frames:
    				        +-----------+
    	                              0 |           |
    	                                +-----------+
    	                              1 |           |
    				        +-----------+
    				      2 |           |
    				        +-----------+
    

Example of the Second Chance 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 SecondChance bits = 0
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |           |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |           |
    	                                +-----------+
    	                   0          1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     0     |
    	                                +-----------+
    	                   0   next-> 1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     0     |
    	                                +-----------+
    	                   0   next-> 1 |           |
    				        +-----------+
    			   0	      2 |           |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     0     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0   next-> 2 |           |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     0     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0   next-> 2 |           |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     0     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0	      2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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 !!    Set SecondChance bit of page 4 !!!

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

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     0     |
    	                                +-----------+
    	                   1          1 |     4     |
    				        +-----------+
    			   0	      2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4 !!!

      # page faults = 4 
    
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	    -------------> 1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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 give memory frame 1 a second chance
      Try next memory frame...
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
     
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0   next-> 2 |     1     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     2     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0	      2 |     3     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4

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

Example of the Second Chance 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    Set SecondChance bit of page 2

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

Example of the Second Chance 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    Set SecondChance bit of page 4

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

Example of the Second Chance 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 give memory frame 0 a second chance
      Try next memory frame...
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   1   next-> 0 |     2     |
    	                                +-----------+
    	                   1          1 |     4     |
    				        +-----------+
    			   0	      2 |     3     |
    				        +-----------+
    

Example of the Second Chance 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 give memory frame 1 a second chance
      Try next memory frame...
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     3     |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
    
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     2     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0   next-> 2 |     3     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     2     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4

      # page faults = 6 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     2     |
    	                                +-----------+
    	       --------->  1          1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     2     |
    	                                +-----------+
    	                   1          1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     1     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4

      # page faults = 7 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     1     |
    	                                +-----------+
    	      ---------->  1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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 give memory frame 1 a second chance
      Try next memory frame...
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     1     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
    
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     1     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0   next-> 2 |     0     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     1     |
    	                                +-----------+
    	                   0          1 |     4     |
    				        +-----------+
    			   0	      2 |     2     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4

      # page faults = 8 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     1     |
    	                                +-----------+
    	         ------->  1          1 |     4     |
    				        +-----------+
    			   0	      2 |     2     |
    				        +-----------+
    

Example of the Second Chance 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 (no second chance)
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0   next-> 0 |     1     |
    	                                +-----------+
    	                   1          1 |     4     |
    				        +-----------+
    			   0	      2 |     2     |
    				        +-----------+
    

Example of the Second Chance 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 
      
    
                         SecondChance       Memory frames:
    				        +-----------+
    	                   0          0 |     3     |
    	                                +-----------+
    	                   1   next-> 1 |     4     |
    				        +-----------+
    			   0	      2 |     2     |
    				        +-----------+
    

Example of the Second Chance 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    Set SecondChance bit of page 4

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

Total # page faults = 9