Question
What will be the output of the following code snippet
implementing the Least Recently Used (LRU) page replacement algorithm? def lru(pages, capacity): page_frame = [] page_faults = 0 for page in pages: if page not in page_frame: if len(page_frame) < capacity: page_frame.append(page) else: lru_page = min(page_frame, key=lambda p: pages.index(p)) page_frame.remove(lru_page) page_frame.append(page) page_faults += 1 return page_faults pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3] capacity = 4 faults = lru(pages, capacity) print(faults)Solution
In the LRU page replacement algorithm, when a page fault occurs and the page is not in the page frame, the algorithm replaces the least recently used page. In the provided code, the sequence of page requests is [7, 0, 1, 2, 0, 3, 0, 4, 2, 3] with a capacity of 4. The pages that cause faults are 7, 0, 1, 2, 3, and 4, leading to a total of 6 page faults throughout the execution. Why Other Options Are Wrong: A) 7: This option is incorrect as it counts one additional fault that does not occur in the given page sequence. B) 5: This option is incorrect as it undercounts the number of faults that occur during the execution. C) 8: This option is incorrect as it overcounts the faults; only 6 pages lead to faults based on the implemented logic. E) 4: This option is incorrect because it suggests that only four pages caused faults, which does not reflect the sequence provided.
- What is the smallest 5-digit number divisible by 10, 18, 30 and 45?
A lending library has a fixed charge for the first 7 days and an additional charge for each day thereafter. Sahil paid Rs . 215 for a book kept for 15 ...
What least number should be subtracted from 5612 to make it exactly divisible by 25?
Which of the following pairs is NOT coprime?
22 44 176 1078 8448
...Some worksheets were distributed among (2x + 2) students such that each student received 9 worksheets. If there had been 8 students more, then worksheet...
In a kilometre race, A beats B by 30 metres or 6 seconds. Find the time taken by A to finish the race?
Evaluate 2.5 ÷ 0.0005.
The number of chocolates with ‘Rohan’ and ‘Sohan’ together is 20 more than that with ‘Mohan’, while the number of chocolates with ‘Sohan�...
Ravi has 90 pens of two different brands i.e., Parker and Cello. If he bought 30 more Parker pens, the ratio of number of Cello to Parker pens with him ...