0% found this document useful (0 votes)
267 views

Virtual Memory: Module - 3

Uploaded by

Subasish Nayak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
267 views

Virtual Memory: Module - 3

Uploaded by

Subasish Nayak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 27

VIRTUAL MEMORY

Module – 3

12/07/21 Copyright © 2009 www.egtoget.com 1


PAGE REPLACEMENT

What happens if there is no free frame?

Page replacement – find some page in memory, but not


really in use, swap it out
• Algorithm

• Performance – want an algorithm which will


result in minimum number of page faults

• Same page may be brought into memory several


times
12/07/21 Copyright © 2009 www.egtoget.com 2
Need For Page Replacement

12/07/21 Copyright © 2009 www.egtoget.com 3


Basic Page Replacement

1) Find the location of the desired page on disk

2) Find a free frame:


- If there is a free frame, use it
- If there is no free frame, use a page
replacement algorithm to select a victim frame

3) Bring the desired page into the (newly) free frame;


update the page and frame tables

4) Restart the process


12/07/21 Copyright © 2009 www.egtoget.com 4
Page Replacement

12/07/21 Copyright © 2009 www.egtoget.com 5


Page Replacement Algorithms

Want lowest page-fault rate

Evaluate algorithm by running it on a


particular string of memory references
(reference string) and computing the number
of page faults on that string

In all our examples, the reference string is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
12/07/21 Copyright © 2009 www.egtoget.com 6
First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
3 frames (3 pages can be in memory at a time per
process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4

4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3

Belady’s Anomaly: more frames  more page faults


12/07/21 Copyright © 2009 www.egtoget.com 7
We have seen that the number of faults for 4
Frames (10) is greater than the number of faults
for 3 Frames (9).

This most unexpected result is known as Belady’s


anomaly.

For some page-replacement algorithms, the page


fault rate may increase as the number of allocated
frames increases.

12/07/21 Copyright © 2009 www.egtoget.com 8


FIFO Page Replacement
When a page must be replaced, the oldest page is
chosen.

We can create a FIFO queue to hold all pages in


memory.

We replace the page at the head of the queue.

When a page is brought into memory, we insert it


at the tail of the queue.
12/07/21 Copyright © 2009 www.egtoget.com 9
FIFO Page Replacement
Initially 3 frames are empty. The 1st 3
references (7, 0, 1) cause page faults, and are
brought into these empty frames.

The next reference 2 replaces page 7, because it


was brought in first.

Since o is the next reference and 0 is already in


memory, we have no fault for this reference.

There are 15 faults altogether.


12/07/21 Copyright © 2009 www.egtoget.com 10
FIFO Page Replacement

12/07/21 Copyright © 2009 www.egtoget.com 11


Optimal Page Replacement

An optimal page replacement algorithm has the


lowest page-fault rate of all algorithms, and will
never suffer from Belady’s anomaly.

Replace the page that will not be used for the


longest period of time.

This Page-Replacement Algorithm guarantees the


lowest possible page fault rate for a fixed number
of Frames.
12/07/21 Copyright © 2009 www.egtoget.com 12
Optimal Page Replacement
The first 3 references cause faults that fill the 3 empty
Frames.

The reference to Page 2 replaces page 7, because 7 will


not be used until reference 18, were as page 0 will be
used at 5, and page 1 at 14.

The Optimal Page-Replacement algorithm would yield 9


Page faults.

The Optimal Page-Replacement algorithm is difficult to


implement, because it requires future knowledge of the
reference.
12/07/21 Copyright © 2009 www.egtoget.com 13
Optimal Page Replacement

12/07/21 Copyright © 2009 www.egtoget.com 14


LRU Page Replacement

If we use the recent past as an approximation of


the near future, then we will replace the page that
has not been used for the longest period of time.

This approach is Least-Recently-Used (LRU)


algorithm.

This strategy is the Optimal Page-Replacement


algorithm looking backward in time, rather than
forward.
12/07/21 Copyright © 2009 www.egtoget.com 15
LRU Page Replacement
The first 5 faults are same as the optimal replacement.

The reference to page 4 occurs, LRU sees that, of the


three frames in memory Page 2, was used least recently.

The most recently used page is page 0, and just before


that page 3 was used.

Thus, replace page 2 with page 4.

LRU replacement with 12 faults is still much better than


FIFO replacement with 15.
12/07/21 Copyright © 2009 www.egtoget.com 16
LRU Page Replacement

12/07/21 Copyright © 2009 www.egtoget.com 17


LRU Page Replacement
An LRU page-replacement algorithm may require
substantial hardware assistance.

The problem is to determine an order for the


frames defined by the time of last use.

Two implementations are feasible:

1) Counters:

2) Stack:
12/07/21 Copyright © 2009 www.egtoget.com 18
LRU Page Replacement
1) Counters:
We associate with each page-table entry a time-
of-use field, and add to the CPU a logical clock
or counter.
The clock is incremented for every memory
reference.
When a reference to a page is made, the contents
of the clock register are copied to the time-of-
use field in the page-table entry for that page.
12/07/21 Copyright © 2009 www.egtoget.com 19
LRU Page Replacement
In this way, we always have the “time” of the
last reference to each page.

We replace the page with the smallest time


value.

This scheme requires a search of the page table


to find the LRU page, and a write to memory
for each memory access.

12/07/21 Copyright © 2009 www.egtoget.com 20


LRU Page Replacement
2) Stack:
Whenever a page referenced, it is removed from
the stack and put on the top.

In this way the top of the stack is always the most


recently used page and the bottom is the LRU
page.

Because entries must be removed from the middle


of the stack, it is best implemented by a double
linked list.
12/07/21 Copyright © 2009 www.egtoget.com 21
LRU Page Replacement (Stack)

12/07/21 Copyright © 2009 www.egtoget.com 22


LRU Approximation Page Replacement
Many systems provides a reference bit.

The reference bit for a page is set, by the hardware whenever the
page is referenced.

This is associated with each entry in the page table.

Initially all bits are cleared (to 0) by the OS.

As the user process executes, the bit associated with each page
referenced is set (to 1) by the hardware.

After some time we can determine which page have been used and
which not by examining the reference bits.
12/07/21 Copyright © 2009 www.egtoget.com 23
Global vs. Local Allocation

Global replacement:
Process selects a replacement frame from the set of all frames;
even if that frame is currently allocated to some other process;
One process can take a frame from another
Local replacement:
Each process selects from only its own set of allocated frames.
Ex: consider an allocation scheme where we allow high-priority
processes to select frames from low-priority processes for
replacement.

12/07/21 Copyright © 2009 www.egtoget.com 24


Thrashing
Look at any process that does not have enough frames.
Although it is technically possible to reduce the number
of allocated frames to the minimum, there is some large
number of pages in active use.
If the process does not have this number of frames, it will
quickly page fault. At this point, it must replace some
page.
However, since all its pages are in active use, it must
replace a page that will be needed again right away.

12/07/21 Copyright © 2009 www.egtoget.com 25


Thrashing
If CPU utilization is too low, we increase the degree of
multiprogramming by introducing a new process to the system.
A global-page replacement algorithm is used; it replaces pages with
no regard to the process to which they belong.
Now suppose that a process enters a new phase in its execution and
needs more frames.
It starts faulting and taking frames away from other processes.

These processes need those pages, however, and so they also fault,
taking frames from other processes.

12/07/21 Copyright © 2009 www.egtoget.com 26


Recover From Thrashing
As the degree of multiprogramming increases, CPU utilization also
increases, although more slowly, until a maximum is reached.

If the degree of multiprogramming is increased even further,


thrashing sets in the CPU utilization drops sharply.

At this point, to increase CPU utilization and stop thrashing, we


must decrease the degree of multiprogramming.

We can limit the effects of thrashing by using a local replacement


algorithm.

12/07/21 Copyright © 2009 www.egtoget.com 27

You might also like