VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/second-chance-or-clock-page-replacement-policy/

⇱ Second Chance (or Clock) Page Replacement Policy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Second Chance (or Clock) Page Replacement Policy

Last Updated : 15 Apr, 2026

The Second Chance Page Replacement Algorithm is an improvement over FIFO that avoids removing frequently used pages by using a reference bit. When a page is accessed, its reference bit is set to 1, and during replacement, pages with bit 0 are replaced while pages with bit 1 are given a second chance, making memory management more efficient.

  • Uses a reference bit to track page usage
  • Replaces pages with reference bit 0
  • Gives a second chance to pages with bit 1 by resetting it
  • More efficient and scalable than FIFO, and sometimes better than LRU in modern systems

Algorithm

The algorithm works by associating a reference bit with each page in memory:

  • Reference Bit = 1, The page has been recently used.
  • Reference Bit = 0, The page has not been recently used.

Steps:

1. Pages are maintained in a queue (like FIFO).
2. When a page fault occurs, the algorithm checks the page at the front of the queue.
3. If the reference bit = 0, the page is replaced.
4. If the reference bit = 1:

  • The reference bit is reset to 0.
  • The page is moved to the back of the queue (given a second chance).
  • The search continues until a page with reference bit = 0 is found.

Thus, frequently accessed pages are less likely to be replaced.

Example

Let's say the reference string is 0 4 1 4 2 4 3 4 2 4 0 4 1 4 2 4 3 4 and we have 3 frames. Let's see how the algorithm proceeds by tracking the second chance bit and the pointer. 

👁 page_replacement
Page Replacement
  • Initially, all frames are empty so after the first 3 passes they will be filled with {0, 4, 1} and the second chance array will be {0, 0, 0} as none has been referenced yet. Also, the pointer will cycle back to 0. 
  • Pass-4: Frame={0, 4, 1}, second_chance = {0, 1, 0} [4 will get a second chance], pointer = 0 (No page needed to be updated so the candidate is still page in frame 0), pf = 3 (No increase in page fault number). 
  • Pass-5: Frame={2, 4, 1}, second_chance= {0, 1, 0} [0 replaced; it's second chance bit was 0, so it didn't get a second chance], pointer=1 (updated), pf=4 
  • Pass-6: Frame={2, 4, 1}, second_chance={0, 1, 0}, pointer=1, pf=4 (No change) 
  • Pass-7: Frame={2, 4, 3}, second_chance= {0, 0, 0} [4 survived but it's second chance bit became 0], pointer=0 (as element at index 2 was finally replaced), pf=5 
  • Pass-8: Frame={2, 4, 3}, second_chance= {0, 1, 0} [4 referenced again], pointer=0, pf=5 
  • Pass-9: Frame={2, 4, 3}, second_chance= {1, 1, 0} [2 referenced again], pointer=0, pf=5 
  • Pass-10: Frame={2, 4, 3}, second_chance= {1, 1, 0}, pointer=0, pf=5 (no change) 
  • Pass-11: Frame={2, 4, 0}, second_chance= {0, 0, 0}, pointer=0, pf=6 (2 and 4 got second chances) 
  • Pass-12: Frame={2, 4, 0}, second_chance= {0, 1, 0}, pointer=0, pf=6 (4 will again get a second chance) 
  • Pass-13: Frame={1, 4, 0}, second_chance= {0, 1, 0}, pointer=1, pf=7 (pointer updated, pf updated) 
  • Page-14: Frame={1, 4, 0}, second_chance= {0, 1, 0}, pointer=1, pf=7 (No change) 
  • Page-15: Frame={1, 4, 2}, second_chance= {0, 0, 0}, pointer=0, pf=8 (4 survived again due to 2nd chance!) 
  • Page-16: Frame={1, 4, 2}, second_chance= {0, 1, 0}, pointer=0, pf=8 (2nd chance updated) 
  • Page-17: Frame={3, 4, 2}, second_chance= {0, 1, 0}, pointer=1, pf=9 (pointer, pf updated) 
  • Page-18: Frame={3, 4, 2}, second_chance= {0, 1, 0}, pointer=1, pf=9 (No change) 

Code Implementation:

Output:

Total page faults were 9
Total page faults were 11

Comment
Article Tags: