Tutorial 9 Page Replacement Algorithms In the following examples • A frame with a value of 0 is empty. A frame shown with an “=” sign holds the same value as it held in the previous time period First in first out (FIFO) • The reference string of pages is 123472512645 • When a page must be replaced, the oldest page is chosen 1 2 3 4 7 2 5 1 2 6 4 5 1 = = = 7 = = = = 6 = = 0 2 = = = = 5 = = = 4 = 0 = 3 = = = = 1 = = = 5 0 = = 4 = = = = 2 = = = – page faults: 11 Optimal • The reference string of pages is 123472512645 • Replace the page that will not be used for the longest period of time 1 2 3 4 7 2 5 1 2 6 4 5 1 = = = = = = = = 6 = = 0 2 = = = = = = = = = = 0 = 3 = 7 = 5 = = = = = 0 = = 4 = = = = = = = = – page faults: 7 Least recently used (LRU) • • • The reference string of pages is 123472512645 When a page must be replaced , LRU choses the page that has not been used for the largest period of time 1 2 3 4 7 2 5 1 2 6 4 5 1 = = = 7 = = = = 6 = = 0 2 = = = = = = = = = = 0 = 3 = = = 5 = = = 4 = 0 = = 4 = = = 1 = = = 5 page faults: 10 Second chance When a page is used its use bit is set to 1. We also use a pointer to the next victim which is initialized to the first page/frame. When a page is loaded, it is set to point to the next frame. The list of pages is considered as a circular queue. When a page is considered for replacement, its use bit is examined. If it is zero [that page is replaced] otherwise [the use bit is set to zero, the next victim pointer is advanced, and the process repeated until a page is found with a zero use bit]. Second chance 1 Bit 2 Bit 3 Bit 1 1 1 1 1 1 2 1 2 1 3 1 4 1 2 3 4 Bit 1 1 1 1 – 9 page faults. 7 7 2 3 4 Bit 1 0 0 0 2 7 2 3 4 Bit 1 1 0 0 5 7 2 5 4 Bit 1 0 1 0 1 7 2 5 1 Bit 1 0 1 1 2 7 2 5 1 Bit 1 1 1 1 6 6 2 5 1 Bit 1 0 0 0 4 6 4 5 1 Bit 1 1 0 0 5 6 4 5 1 Bit 1 1 1 0