Docsity

Page Replacement Algorithms: Understanding FIFO, LRU, and Optimal Algorithms - Prof. Jonat, Study notes of Operating Systems

Partial preview of the text

Download Page Replacement Algorithms: Understanding FIFO, LRU, and Optimal Algorithms - Prof. Jonat and more Study notes Operating Systems in PDF only on Docsity!

CS 333

Introduction to Operating Systems^ Class 14 – Page Replacement

Jonathan Walpole Computer Science Portland State University

Page replacement^ ‰^ Assume a normal page table (e.g., BLITZ)^ ‰^ User-program is executing^ ‰^ APageInvalidFault occurs!^ ™^

The page needed is not in memory ‰ Select some frame and remove the page in it ™ If it has been modified, it must be written back to disk^ •^ the “dirty” bit in its page table entry tells us if this isnecessary ‰ Figure out which page was needed from the faulting addr ‰ Read the needed page into this frame ‰ Restart the interrupted process by r

etrying the same

instruction

The optimal page replacement algorithm^ ‰^ Idea:^ ™^

Select the page that will not be needed for the longesttime

Optimal page replacement^ ‰^ Replace the page that will not be needed for the longest^ ‰^ Example:^ Time^

Requests

c^ a^

d^ b^

e^ b^

a^ b^

c^ d

Page^

0 a Frames 1

b 2 c 3 d Page faults

a^ a^

a^ a b^ b^

b^ b c^ c^

c^ c d^ d^

d^ d

X

The optimal page replacement algorithm^ ‰^ Idea:^ ™^

Select the page that will not be needed for the longesttime ‰ Problem: ™ Can’t know the future of a program ™ Can’t know when a given page will be needed next ™ The optimal algorithm is unrealizable

The optimal page replacement algorithm^ ‰^ However:^ ™^

We can use it as a control case for simulation studies^ •^ Run the program once^ •^ Generate a log of all memory references^ •^ Use the log to simulate various page replacementalgorithms^ •^ Can compare others to “optimal” algorithm

FIFO page replacement algorithm^ ‰^ Replace the page that was first brought into memory^ ‰^ Example: Memory system with 4 frames:^ Time^

Requests

c^ a^

d^ b^

e^ b^

a^ b^

c^ a

Page^

0 a Frames 1

b 2 c 3 d Page faults

a^ a^

a b c^ c^

c^ c d^ d

X

FIFO page replacement algorithm^ ‰^ Replace the page that was first brought into memory^ ‰^ Example: Memory system with 4 frames:^ Time^

Requests

c^ a^

d^ b^

e^ b^

a^ b^

c^ a

Page^

0 a Frames 1

b 2 c 3 d Page faults

a^ a^

a^ a^

a b^ b^

b

c^ c^

c^ c^

e^ e d^ d^

d^ d X

FIFO page replacement algorithm^ ‰^ Always replace the oldest page.^ ™

“Replace the page that has been in memory for thelongest time.” ‰ Implementation ™ Maintain a linked list of all pages in memory ™ Keep it in order of when they came into memory ™ The page at the front of the list is oldest ™ Add new page to end of list

FIFO page replacement algorithm^ ‰^ Disadvantage:^ ™^

The oldest page may be needed again soon ™ Some page may be important throughout execution ™ It will get old, but replacing it will cause an immediatepage fault

Page table: referenced and dirty bits^ ‰^ Each page has a^ ™^

Referenced Bit - set by MMU when page read / written ™ Dirty / Modified Bit - set when page is written ‰ On Some Hardware... ™ ReadOnly Bit but no Dirty Bit ‰ Idea: ™ Software sets the ReadOnly bit for all pages ™ When program tries to update the page...^ •^ A trap occurs^ •^ Software sets the Dirty Bit and clears the ReadOnly bit^ •^ Resumes execution of the program

Not recently used page replacement alg.^ ‰^ Use the Referenced Bit and the Dirty Bit^ ‰^ Initially, all pages have^ ™^

Referenced Bit = 0 ™ Dirty Bit = 0 ‰ Periodically... (e.g. whenever a timer interrupt occurs) ™ Clear the Referenced Bit

Second chance page replacement alg.^ ‰^ Modification to FIFO^ ‰^ Pages kept in a linked list^ ™^

Oldest is at the front of the list ‰ Look at the oldest page ™ If its “referenced bit” is 0...^ •^ Select it for replacement ™ Else^ •^ It was used recently; don’t want to replace it^ •^ Clear its “referenced bit”^ •^ Move it to the end of the list ™ Repeat ‰ What if every page was used in last clock tick? ™ Select a page at random ‰

Clock algorithm (same as second chance)^ ‰^ Maintain a circular list of pages in memory^ ‰^ Set a bit for the page when a page is referenced^ ‰^ Clock sweeps over memory looking for a victim page that doesnot have the referenced bit set^ ™^

If the bit is set, clear it and move on to the next page ™ Replaces pages that haven’t been referenced for one completeclock revolution

1

(^12) 3 5 4

0

clock bit^ frame #