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

An overview of page replacement algorithms, focusing on fifo (first in, first out), lru (least recently used), and the optimal algorithm. It explains the concept of page replacement, the need for algorithms, and the limitations of the optimal algorithm. The document also covers various page replacement algorithms, their implementation, and their advantages and disadvantages.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-ogb-1
koofers-user-ogb-1 🇺🇸

10 documents

1 / 71

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 333
Introduction to Operating Systems
Class 14 – Page Replacement
Jonathan Walpole
Computer Science
Portland State University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47

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 #