page replacement algorithms

20
Page Replacement Algorithms So, when we have a page fault we have to find an eviction candidate. Optimally, we would like to evict the page that will not be referenced again for the longest amount of time. In reality the OS has no way of knowing when each of the pages will be referenced next.

Upload: candra

Post on 11-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Page Replacement Algorithms. So, when we have a page fault we have to find an eviction candidate. Optimally, we would like to evict the page that will not be referenced again for the longest amount of time. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Page Replacement Algorithms

Page Replacement Algorithms

So, when we have a page fault we have to find an eviction candidate.

Optimally, we would like to evict the page that will not be referenced again for the longest amount of time. In reality the OS has no way of knowing

when each of the pages will be referenced next.

Page 2: Page Replacement Algorithms

Not Recently Used Examine M and R bits associated with

each page. Page fault occurs, OS places all pages in

1 or 4 classifications Class 0: R=0, M=0 Class 1: R=0, M=1 Class 2: R=1, M=0 Class 3: R=1, M=1

NRU Algorithm says to remove a page at random from the lowest nonempty class.

Page 3: Page Replacement Algorithms

FIFO Simple design of having a queue

maintained for pages in memory. The head of the queue contains oldest

page in memory. The tail of the queue contains the

newest page in memory. Is there a potential problem with

evicting the head of the queue each time?

Page 4: Page Replacement Algorithms

FIFO with second chance Performs like FIFO, but inspects the R bit. If R bit of the head page is 1, it is cleared and

placed at the tail. What happens if all pages in memory have the

R bit set to one?

Page 5: Page Replacement Algorithms

Clock Page Replacement

Behaves liked FIFO with second chance except that it is a circular linked list.

Page 6: Page Replacement Algorithms

Least Recently Used

Assume pages used recently will be used again soon

throw out page that has been unused for longest time Must keep a linked list of pages

most recently used at front, least at rear update this list every memory reference !!

Keep counter in each page table entry choose page with lowest value counter periodically zero the counter

Page 7: Page Replacement Algorithms

LRU (cont.) Alternatively used a n by n matrix. n is the

number of page frames. All values in matrix intially set to 0 When a page frame, k, is referenced,

the hardware first sets all the bits in row k to 1.

Then sets all the bits in column k to 0. The row whose binary value is lowest

is the LRU at any instant.

Page 8: Page Replacement Algorithms

Example of LRU using Matrix

Pages referenced in order 0,1,2,3,2,1,0,3,2,3

Page 9: Page Replacement Algorithms

Simulated LRU – Not Frequently Used Most machines do not have the

hardware to perform true LRU, but it may be simulated.

We can use a counter to keep track of each R bit for each page upon a timer tick.

Page with the lowest R-bit is evicted. What is the problem with this type of history

keeping?

Page 10: Page Replacement Algorithms

Modified NFU

Counters are each shifted right 1 bit before the R bit is added.

R bit is added to the leftmost, rather than the rightmost bit.

This modified algorithm is known as aging.

Page 11: Page Replacement Algorithms

Modified NFU (Aging)

Page 12: Page Replacement Algorithms

Issues with NFU

References can happen between timer interrupts.

What happens if we are comparing two pages which have identical references over the past n timer ticks? We make a random choice. Is it the right one?

Page 13: Page Replacement Algorithms

Working Set Page Replacement Locality of Reference – a process references

only a small fraction of its pages during any particular phase of its execution.

The set of pages that a process is currently using is called the working set.

Thrashing – results when a program causes a page fault every few instructions, causing the pages to have to pulled up from memory. No “real” useful work is being accomplished.

Page 14: Page Replacement Algorithms

Working Set Page Replacement (cont.) So, what happens in a multiprogramming

environment as processes are switched in and out of memory?

Do we have to take a lot of page faults when the process is first started?

It would be nice to have a particular processes working set loaded into memory before it even begins execution. This is called prepaging.

Page 15: Page Replacement Algorithms

Working Set Page Replacement (cont.)

The working set algorithm is based on determining a working set and evicting any page that is not in the current working set upon a page fault.

Page 16: Page Replacement Algorithms

Working Set Page Replacement (cont.)

Age = current virtual time – time of last use

Page 17: Page Replacement Algorithms

Working Set Page Replacement (cont.) What happens when there is more than

one page with R=0 and age less then or eq to tau?

What happens when all pages have R=1? This algorithm requires the entire page

table be scanned at each page fault until a suitable candidate is located. All entries must have their Time of last use

updated even after a suitable entry is found.

Page 18: Page Replacement Algorithms

WSClock Page Replacement

Page 19: Page Replacement Algorithms

WSClock Page Replacement (Cont.) What happens when R=0?

Is age > , and page is clean then it is evicted If it is dirty then we can proceed to find a page that

may be clean and avoid a process switch. We will still need to write to disk and this write is scheduled.

What happens if we go all the way around? (Two Scenarios)

At least one write has been scheduled. Keep Looking… someone eventually write to disk.

No writes have been scheduled… all pages are in the working set, so evict a clean page or the current page if a clean page does not exist.

Page 20: Page Replacement Algorithms

Page Replacement Algorithm Summary