Concept:
Last-in-first-out page replacement policy: It replaces the newest page that arrives at last in the main memory.
Optimal Page replacement policy: It replaces the page whose next use will occur farthest in the future.
Explanation:
Access sequence is (a1, a2,…,a20, a1, a2,…,a20)
a1 to a10 will result in page faults. Total 10 page faults in this.
Then a11 comes, it replaces a10, a12 replaces a11, a13 replaces a12 and so on,….10 page faults from a11 to a20.
Now when a1 to a9 occurs again, it will result in 0 page fault because these are already present in the page frame. Again a10 will replace a20, a11 replaces a10 and so on. So, in this way total 11 page faults from a10 to a20.
Total page faults = 10 + 10 + 11 = 31
Using optimal page replacement:
a1 to a10 will result in page faults. Total 10 page faults in this.
Then a11 will replace a10 because from a1 to a10, a10 will be used later, a12 replaces a11 and so on. 10 page faults from a11 to a20.
Again a1 to a9, 0 page faults. a10 will replace a1, a10 to a19 will have 10 page faults.
Total page faults in this = 10 + 10 + 10 = 30
So, difference in number of page faults = 31 – 30 = 1