{"id":269,"date":"2018-07-20T06:10:05","date_gmt":"2018-07-20T06:10:05","guid":{"rendered":"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=269"},"modified":"2018-08-07T10:48:02","modified_gmt":"2018-08-07T10:48:02","slug":"page-replacement-algorithms-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/chapter\/page-replacement-algorithms-ii\/","title":{"rendered":"Page Replacement Algorithms &#8211; II"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>27.1 Introduction<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this module, we learn what is meant by page replacement and two algorithms used for page replacement. In an earlier module, we learnt what demand paging is. In demand paging, pages are brought into memory only when the pages are needed. All the pages of a process need not be kept in the main memory at the same time. Only the pages that are used currently need to be kept in the main memory. Suppose a page is brought into the main memory and if there is no free frame in the main memory, a victim page is chosen from the main memory, the victim page is moved out to the backing store and the required page is brought into the main memory. Choosing a victim page for replacement is done by the page replacement algorithms. In the previous module we learnt the working of the First-In-First-Out (FIFO) page replacement algorithm, in which the page that was brought into the memory first is chosen for replacement first. But, FIFO suffers from Belady\u2019s anomaly.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this module, we learn the working of two more page replacement algorithms, Optimal page replacement (OPT) and Least recently used (LRU) page replacement algorithms.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>27.2 Optimal (OPT) Page Replacement Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The optimal page replacement algorithm replaces the page that will not be used for longest period of time in future. This algorithm has the lowest page-fault of all algorithms. The OPT algorithm does not suffer from Belady\u2019s anomaly.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 27.1 shows an example of how page replacement is done using the OPT page replacement algorithm. The reference string 7, 0, 1, \u2026, 1 is shown in the figure. There are three free frames in the physical memory. The first reference is to page 7 and 7 is brought into one of the free frames. The second reference is to page 0. Since there is a free frame, that page is also brought into the physical memory. The next reference is to page 1. Page 1 is also brought into the physical memory as there is a free frame.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is not present in the physical memory and there is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (7, 0, 1) present in the main memory, the one that will not be used in the near future is chosen for replacement. Page 7 is the one that will not be used in the near future (this can be seen by observing the reference string). Before 7 will be used, pages 0 and 1 will be used. Therefore, page 7 is moved out to the disk and page 2 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 3. Page 3 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 1 present in the main memory, page 1 is the one that will not be used in the near future. Before 1 will be used, pages 2 and 0 will be used. Therefore, page 1 is moved out to the disk and page 3 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 4. Page 4 is not present in the main memory and hence, a page in the main memory has to be\u00a0<span style=\"font-size: 1em;text-align: initial\">chosen for replacement. Of 2, 0 and 3 present in the main memory, page 0 is the one that will not be used in the near future. Before 0 will be used, pages 2 and 3 will be used. Therefore, page 0 is moved out to the disk and page 4 is brought into that place.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n<img class=\"size-full wp-image-274 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT.png\" alt=\"\" width=\"565\" height=\"304\" \/>\r\n<p style=\"text-align: center\">Fig. 27.1 Example for OPT page replacement (Source: [1])<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore, it is a hit and page 2 is used from the main memory. The next reference is to page 3. Page 3 is already present in the main memory. Therefore it is a hit and page 3 is used from the main memory.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 4 and 3 present in the main memory, page 4 is the one that will not be used in the near future. Before 4 will be used, pages 2 and 3 will be used. Therefore, page 4 is moved out to the disk and page 0 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 3. Page 3 is already present in the main memory. Therefore, it is a hit and page 3 is used from the main memory. The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 1. Page 1 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 3 present in the main memory, page 3 is the one that will not be used in the near future. Therefore, page 3 is moved out to the disk and page 1 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore, it is a hit and page 2 is used from the main memory. The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 1. Page 1 is already present in the main memory. Therefore, it is a hit and page 1 is used from the main memory.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 7. Page 7 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 1 present in the main memory, page 2 is the one that will not be used in the near future. Therefore, page 2 is moved out to the disk and page 7 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore, it is a hit and page 0 is used from the main memory. The next reference is to page 1. Page 1 is already present in the main memory. Therefore it is a hit and page 1 is used from the main\u00a0<span style=\"text-align: initial;font-size: 1em\">memory.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The number of times when a page is brought into the main memory from the disk is the number of page faults. In the example shown above, the number of page faults is 9. This is less compared to the FIFO page replacement algorithm.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The disadvantage of the optimal page replacement algorithm is that it is difficult to implement. The OPT algorithm requires future knowledge of the replacement string. That is, the algorithm needs to know which pages will be referred to in future, which is practically impossible. This is similar to the situation in the SJF CPU scheduling algorithm. But, it is used mainly for comparison studies. When you devise your own page replacement algorithm, the performance of the devised algorithm can be compared with the performance of OPT.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">We now learn another algorithm for page replacement called the least recently used (LRU) page replacement algorithm.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>27.3 Least Recently Used (LRU) Algorithm\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Since the optimal algorithm is not practically feasible, an approximation to optimal algorithm was devised called the LRU algorithm. FIFO uses the time when a page was brought into memory. OPT uses the time when the page will be used in the future. In the LRU page replacement algorithm, recent past is looked at as an approximation of the near future. Here, the page that has not been used for the longest period of time in the past is chosen for replacement.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">To implement LRU page replacement, the time of last use is associated with each page. Whenever a page is used, the time of use is updated. The page that has not been used for the longest period of time is chosen for replacement. Compared to OPT, this is looking backward in time, rather than looking forward in time.<\/p>\r\n<img class=\"size-full wp-image-273 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU.png\" alt=\"\" width=\"596\" height=\"307\" \/>\r\n<p style=\"text-align: center\">Fig. 27.2 Example for LRU page replacement (Source: [1])<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 27.1 shows an example of how page replacement is done using the LRU page replacement algorithm. The reference string 7, 0, 1, \u2026, 1 is shown in the figure. There are three free frames in the physical memory. The first reference is to page 7 and 7 is brought into one of the free frames. The second reference is to page 0. Since there is a free frame, that page is also brought into the physical memory. The next reference is to page 1. Page 1 is also brought into the physical memory as there is a free frame.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is not present in the physical memory and there\u00a0<span style=\"font-size: 1em;text-align: initial\">is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (7, 0, 1) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 7 is the one that was not used in the recent past. Page 7 was used before pages 0 and 1 were used. Therefore, page 7 is moved out to the disk and page 2 is brought into that place.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 3. Page 3 is not present in the physical memory and there is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (2, 0, 1) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 1 is the one that was not used in the recent past. Page 1 was used before pages 2 and 0 were used. Therefore, page 1 is moved out to the disk and page 3 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">The next reference is to page 2. Page 2 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 0, 3) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 3 is the one that was not used in the recent past. Page 3 was used before pages 4 and 0 were used. Therefore, page 3 is moved out to the disk and page 2 is brought into that place.<\/span>The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 4. Page 4 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (2, 0, 3) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 2 is the one that was not used in the recent past. Page 2 was used before pages 0 and 3 were used. Therefore, page 2 is moved out to the disk and page 4 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 3. Page 3 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 0, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 0 is the one that was not used in the recent past. Page 0 was used before pages 4 and 2 were used. Therefore, page 0 is moved out to the disk and page 3 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 4 is the one that was not used in the recent past. Page 4 was used before pages 3 and 2 were used. Therefore, page 4 is moved out to the disk and page 0 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 1. Page 1 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (0, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 0 is the one that was not used in the recent past. Page 0 was used before pages 3 and 2 were used.<\/p>\r\n&nbsp;\r\n\r\nTherefore, page 0 is moved out to the disk and page 1 is brought into that place.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (1, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 3 is the one that was not used in the recent past. Page 3 was used before pages 1 and 2 were used. Therefore, page 3 is moved out to the disk and page 0 is brought into that place.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next reference to page 1 is a hit. The next reference is to page 7. Page 7 is not\u00a0<span style=\"font-size: 1em;text-align: initial\">present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (1, 0, 2) present in the main memory, page 2 is the one that was not used in the recent past. Page 2 was used before pages 0 and 2 were used. Therefore, page 2 is moved out to the disk and page 7 is brought into that place. The next two references to pages 0 and 1 are hits.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">In this example, the number of page faults is 12. It is seen that the number of page faults for LRU is less than that for FIFO (15 page faults) and greater than that for OPT (9 page faults) for the same reference string. The LRU algorithm is a good page replacement algorithm and is used often.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The LRU algorithm does not suffer from Belady\u2019s anomaly. It is a type of stack algorithm. In stack algorithms, the set of pages in memory for <em>n <\/em>pages is always a subset of the set of pages in memory for <em>n<\/em>+1 pages. Because the algorithm looks at recently used pages, the pages that were used in the last \u2018<em>n<\/em>\u2019 references will be a subset of the pages that were used in the last \u2018<em>n<\/em>+1\u2019 memory references.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.4\u00a0 Implementation of LRU\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The implementation of LRU requires hardware support to determine the order of use based on the time of last use. There are two methods to implement the LRU algorithm: Counter implementation and Stack implementation.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.4.1 LRU Algorithm \u2013 Counter Implementation\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Every page entry has a time-of use field. A counter is added to the CPU and the counter is initialized to zero. The counter is incremented for every memory reference. Every time page is referenced, the counter is copied into the time-of-use field. Therefore, the counter shows when the page was last referenced. When a page needs to be replaced, the page with the least value in the time-of-use field is picked.<\/p>\r\n&nbsp;\r\n\r\nThe overheads involved in this method are:\r\n\r\n&nbsp;\r\n\r\n(i)\u00a0 \u00a0 It is required to search the page table to find the LRU page.\r\n\r\n(ii)\u00a0 \u00a0It is required to write to the memory (Updating time-of-use field) for each memory access.\r\n\r\n(iii)\u00a0 The overflow of the clock must be considered. Since the clock is incremented for each memory reference, there is a possibility for the clock to overflow. This must be handled.\r\n<p style=\"text-align: justify\">(iv) Times must be maintained when page tables are changed (CPU scheduling).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Even when a process is moved out of ready state during CPU scheduling, and later, is given the CPU, the same times should be maintained.<\/p>\r\n&nbsp;\r\n\r\n<strong>27.4.2 LRU Algorithm \u2013 Stack implementation\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this method, a stack of page numbers is kept in a doubly linked list form. Whenever a page referenced, the page is moved to the top of stack. Then, the bottom of the stack has the LRU page. But this requires 6 pointers to be changed, if the page number that is moved is currently in the middle of the list. The\u00a0 advantage\u00a0 is\u00a0 that there is no\u00a0 need to search for replacement. It is enough to select the page whose number is kept in the bottom of the stack.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 27.3 shows an example of how a stack can be used for implementing the LRU algorithm. The recently used page number is kept in the top of the stack. The stack before position \u2018a\u2019 is shown. Then there is a reference to page 7. Therefore, the entry in the stack corresponding to page 7 is moved to the top of the stack. The contents of the stack, after the reference to page 7 (at position \u2018b\u2019), are also shown in the figure.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><img class=\"size-full wp-image-272 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record.png\" alt=\"\" width=\"418\" height=\"248\" \/><\/p>\r\n<p style=\"text-align: center\">Fig. 27.3 Use of a stack to record the most recent page references<\/p>\r\n&nbsp;\r\n\r\n<strong>27.5 <\/strong><strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module, we learnt the implementation of two page replacement algorithms, the optimal page replacement and the least recently used page replacement algorithms. In optimal page replacement algorithm the page that will not be used in the near future is chosen for replacement. In least recently used page replacement algorithm, the page that was not used in the recent past is chosen for replacement. We also learnt the counter and the stack methods used for implementing the LRU algorithm.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify\">Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d, Sixth Edition, John Wiley &amp; Sons Inc., 2003.<\/li>\r\n \t<li style=\"text-align: justify\">Andrew S. Tanenbaum, Herbert Bos, \u201cModern Operating Systems\u201d, Fourth Edition, Pearson Education, 2014.<\/li>\r\n \t<li style=\"text-align: justify\">Gary Nutt, \u201cOperating Systems\u201d, Third Edition, Pearson Education, 2009.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>27.1 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this module, we learn what is meant by page replacement and two algorithms used for page replacement. In an earlier module, we learnt what demand paging is. In demand paging, pages are brought into memory only when the pages are needed. All the pages of a process need not be kept in the main memory at the same time. Only the pages that are used currently need to be kept in the main memory. Suppose a page is brought into the main memory and if there is no free frame in the main memory, a victim page is chosen from the main memory, the victim page is moved out to the backing store and the required page is brought into the main memory. Choosing a victim page for replacement is done by the page replacement algorithms. In the previous module we learnt the working of the First-In-First-Out (FIFO) page replacement algorithm, in which the page that was brought into the memory first is chosen for replacement first. But, FIFO suffers from Belady\u2019s anomaly.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this module, we learn the working of two more page replacement algorithms, Optimal page replacement (OPT) and Least recently used (LRU) page replacement algorithms.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>27.2 Optimal (OPT) Page Replacement Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The optimal page replacement algorithm replaces the page that will not be used for longest period of time in future. This algorithm has the lowest page-fault of all algorithms. The OPT algorithm does not suffer from Belady\u2019s anomaly.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 27.1 shows an example of how page replacement is done using the OPT page replacement algorithm. The reference string 7, 0, 1, \u2026, 1 is shown in the figure. There are three free frames in the physical memory. The first reference is to page 7 and 7 is brought into one of the free frames. The second reference is to page 0. Since there is a free frame, that page is also brought into the physical memory. The next reference is to page 1. Page 1 is also brought into the physical memory as there is a free frame.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is not present in the physical memory and there is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (7, 0, 1) present in the main memory, the one that will not be used in the near future is chosen for replacement. Page 7 is the one that will not be used in the near future (this can be seen by observing the reference string). Before 7 will be used, pages 0 and 1 will be used. Therefore, page 7 is moved out to the disk and page 2 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 3. Page 3 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 1 present in the main memory, page 1 is the one that will not be used in the near future. Before 1 will be used, pages 2 and 0 will be used. Therefore, page 1 is moved out to the disk and page 3 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 4. Page 4 is not present in the main memory and hence, a page in the main memory has to be\u00a0<span style=\"font-size: 1em;text-align: initial\">chosen for replacement. Of 2, 0 and 3 present in the main memory, page 0 is the one that will not be used in the near future. Before 0 will be used, pages 2 and 3 will be used. Therefore, page 0 is moved out to the disk and page 4 is brought into that place.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-274 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT.png\" alt=\"\" width=\"565\" height=\"304\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT.png 565w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT-300x161.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-OPT-350x188.png 350w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 27.1 Example for OPT page replacement (Source: [1])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore, it is a hit and page 2 is used from the main memory. The next reference is to page 3. Page 3 is already present in the main memory. Therefore it is a hit and page 3 is used from the main memory.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 4 and 3 present in the main memory, page 4 is the one that will not be used in the near future. Before 4 will be used, pages 2 and 3 will be used. Therefore, page 4 is moved out to the disk and page 0 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 3. Page 3 is already present in the main memory. Therefore, it is a hit and page 3 is used from the main memory. The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 1. Page 1 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 3 present in the main memory, page 3 is the one that will not be used in the near future. Therefore, page 3 is moved out to the disk and page 1 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore, it is a hit and page 2 is used from the main memory. The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 1. Page 1 is already present in the main memory. Therefore, it is a hit and page 1 is used from the main memory.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 7. Page 7 is not present in the main memory and hence, a page in the main memory has to be chosen for replacement. Of 2, 0 and 1 present in the main memory, page 2 is the one that will not be used in the near future. Therefore, page 2 is moved out to the disk and page 7 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore, it is a hit and page 0 is used from the main memory. The next reference is to page 1. Page 1 is already present in the main memory. Therefore it is a hit and page 1 is used from the main\u00a0<span style=\"text-align: initial;font-size: 1em\">memory.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The number of times when a page is brought into the main memory from the disk is the number of page faults. In the example shown above, the number of page faults is 9. This is less compared to the FIFO page replacement algorithm.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The disadvantage of the optimal page replacement algorithm is that it is difficult to implement. The OPT algorithm requires future knowledge of the replacement string. That is, the algorithm needs to know which pages will be referred to in future, which is practically impossible. This is similar to the situation in the SJF CPU scheduling algorithm. But, it is used mainly for comparison studies. When you devise your own page replacement algorithm, the performance of the devised algorithm can be compared with the performance of OPT.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">We now learn another algorithm for page replacement called the least recently used (LRU) page replacement algorithm.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>27.3 Least Recently Used (LRU) Algorithm\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Since the optimal algorithm is not practically feasible, an approximation to optimal algorithm was devised called the LRU algorithm. FIFO uses the time when a page was brought into memory. OPT uses the time when the page will be used in the future. In the LRU page replacement algorithm, recent past is looked at as an approximation of the near future. Here, the page that has not been used for the longest period of time in the past is chosen for replacement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To implement LRU page replacement, the time of last use is associated with each page. Whenever a page is used, the time of use is updated. The page that has not been used for the longest period of time is chosen for replacement. Compared to OPT, this is looking backward in time, rather than looking forward in time.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-273 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU.png\" alt=\"\" width=\"596\" height=\"307\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU.png 596w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Example-for-LRU-350x180.png 350w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 27.2 Example for LRU page replacement (Source: [1])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 27.1 shows an example of how page replacement is done using the LRU page replacement algorithm. The reference string 7, 0, 1, \u2026, 1 is shown in the figure. There are three free frames in the physical memory. The first reference is to page 7 and 7 is brought into one of the free frames. The second reference is to page 0. Since there is a free frame, that page is also brought into the physical memory. The next reference is to page 1. Page 1 is also brought into the physical memory as there is a free frame.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is not present in the physical memory and there\u00a0<span style=\"font-size: 1em;text-align: initial\">is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (7, 0, 1) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 7 is the one that was not used in the recent past. Page 7 was used before pages 0 and 1 were used. Therefore, page 7 is moved out to the disk and page 2 is brought into that place.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 3. Page 3 is not present in the physical memory and there is no free frame in the physical memory. Therefore a page that is already present in the main memory is to be chosen for replacement. Of the three pages (2, 0, 1) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 1 is the one that was not used in the recent past. Page 1 was used before pages 2 and 0 were used. Therefore, page 1 is moved out to the disk and page 3 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">The next reference is to page 2. Page 2 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 0, 3) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 3 is the one that was not used in the recent past. Page 3 was used before pages 4 and 0 were used. Therefore, page 3 is moved out to the disk and page 2 is brought into that place.<\/span>The next reference is to page 0. Page 0 is already present in the main memory. Therefore it is a hit and page 0 is used from the main memory. The next reference is to page 4. Page 4 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (2, 0, 3) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 2 is the one that was not used in the recent past. Page 2 was used before pages 0 and 3 were used. Therefore, page 2 is moved out to the disk and page 4 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 3. Page 3 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 0, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 0 is the one that was not used in the recent past. Page 0 was used before pages 4 and 2 were used. Therefore, page 0 is moved out to the disk and page 3 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (4, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 4 is the one that was not used in the recent past. Page 4 was used before pages 3 and 2 were used. Therefore, page 4 is moved out to the disk and page 0 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 1. Page 1 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (0, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 0 is the one that was not used in the recent past. Page 0 was used before pages 3 and 2 were used.<\/p>\n<p>&nbsp;<\/p>\n<p>Therefore, page 0 is moved out to the disk and page 1 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 2. Page 2 is already present in the main memory. Therefore it is a hit and page 2 is used from the main memory.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference is to page 0. Page 0 is not present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (1, 3, 2) present in the main memory, the one that was not used in the recent past is chosen for replacement. Page 3 is the one that was not used in the recent past. Page 3 was used before pages 1 and 2 were used. Therefore, page 3 is moved out to the disk and page 0 is brought into that place.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next reference to page 1 is a hit. The next reference is to page 7. Page 7 is not\u00a0<span style=\"font-size: 1em;text-align: initial\">present in the physical memory and there is no free frame in the physical memory. Therefore, of the three pages (1, 0, 2) present in the main memory, page 2 is the one that was not used in the recent past. Page 2 was used before pages 0 and 2 were used. Therefore, page 2 is moved out to the disk and page 7 is brought into that place. The next two references to pages 0 and 1 are hits.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">In this example, the number of page faults is 12. It is seen that the number of page faults for LRU is less than that for FIFO (15 page faults) and greater than that for OPT (9 page faults) for the same reference string. The LRU algorithm is a good page replacement algorithm and is used often.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The LRU algorithm does not suffer from Belady\u2019s anomaly. It is a type of stack algorithm. In stack algorithms, the set of pages in memory for <em>n <\/em>pages is always a subset of the set of pages in memory for <em>n<\/em>+1 pages. Because the algorithm looks at recently used pages, the pages that were used in the last \u2018<em>n<\/em>\u2019 references will be a subset of the pages that were used in the last \u2018<em>n<\/em>+1\u2019 memory references.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.4\u00a0 Implementation of LRU\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The implementation of LRU requires hardware support to determine the order of use based on the time of last use. There are two methods to implement the LRU algorithm: Counter implementation and Stack implementation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.4.1 LRU Algorithm \u2013 Counter Implementation\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Every page entry has a time-of use field. A counter is added to the CPU and the counter is initialized to zero. The counter is incremented for every memory reference. Every time page is referenced, the counter is copied into the time-of-use field. Therefore, the counter shows when the page was last referenced. When a page needs to be replaced, the page with the least value in the time-of-use field is picked.<\/p>\n<p>&nbsp;<\/p>\n<p>The overheads involved in this method are:<\/p>\n<p>&nbsp;<\/p>\n<p>(i)\u00a0 \u00a0 It is required to search the page table to find the LRU page.<\/p>\n<p>(ii)\u00a0 \u00a0It is required to write to the memory (Updating time-of-use field) for each memory access.<\/p>\n<p>(iii)\u00a0 The overflow of the clock must be considered. Since the clock is incremented for each memory reference, there is a possibility for the clock to overflow. This must be handled.<\/p>\n<p style=\"text-align: justify\">(iv) Times must be maintained when page tables are changed (CPU scheduling).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Even when a process is moved out of ready state during CPU scheduling, and later, is given the CPU, the same times should be maintained.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.4.2 LRU Algorithm \u2013 Stack implementation\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this method, a stack of page numbers is kept in a doubly linked list form. Whenever a page referenced, the page is moved to the top of stack. Then, the bottom of the stack has the LRU page. But this requires 6 pointers to be changed, if the page number that is moved is currently in the middle of the list. The\u00a0 advantage\u00a0 is\u00a0 that there is no\u00a0 need to search for replacement. It is enough to select the page whose number is kept in the bottom of the stack.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 27.3 shows an example of how a stack can be used for implementing the LRU algorithm. The recently used page number is kept in the top of the stack. The stack before position \u2018a\u2019 is shown. Then there is a reference to page 7. Therefore, the entry in the stack corresponding to page 7 is moved to the top of the stack. The contents of the stack, after the reference to page 7 (at position \u2018b\u2019), are also shown in the figure.<\/p>\n<\/div>\n<p style=\"text-align: justify\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-272 aligncenter\" src=\"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record.png\" alt=\"\" width=\"418\" height=\"248\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record.png 418w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-content\/uploads\/sites\/49\/2018\/07\/Use-of-a-stack-to-record-350x208.png 350w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/p>\n<p style=\"text-align: center\">Fig. 27.3 Use of a stack to record the most recent page references<\/p>\n<p>&nbsp;<\/p>\n<p><strong>27.5 <\/strong><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module, we learnt the implementation of two page replacement algorithms, the optimal page replacement and the least recently used page replacement algorithms. In optimal page replacement algorithm the page that will not be used in the near future is chosen for replacement. In least recently used page replacement algorithm, the page that was not used in the recent past is chosen for replacement. We also learnt the counter and the stack methods used for implementing the LRU algorithm.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d, Sixth Edition, John Wiley &amp; Sons Inc., 2003.<\/li>\n<li style=\"text-align: justify\">Andrew S. Tanenbaum, Herbert Bos, \u201cModern Operating Systems\u201d, Fourth Edition, Pearson Education, 2014.<\/li>\n<li style=\"text-align: justify\">Gary Nutt, \u201cOperating Systems\u201d, Third Edition, Pearson Education, 2009.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":24,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-mary-anitha-rajam"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-269","chapter","type-chapter","status-publish","hentry","contributor-dr-mary-anitha-rajam"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/269\/revisions"}],"predecessor-version":[{"id":429,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/269\/revisions\/429"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/269\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapter-type?post=269"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/contributor?post=269"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/license?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}