You are on page 1of 9

next occurences

b c a c b a d c b

b c a* a a a* d* d* d* Replace the one whose next occurence


b b b b* b b b b* in the input is furthest, or the one least
c* c*
recently needed( in the future).
* * c c* c c c
M M M H H H M H H
a

OPT
Least Recently Needed(LR N)

mri mri
mri
lri lri
lri

b c a c b a d c b not in in

mri b c a a a a d d b
b c c c c a a d
lri * * b* b* b* b* c* c* a*
M M M H H H M H M
b c
d *
mri mri
a a a no change
H A hit lri lri
mri
d
M A miss b
c c c
b lri
FIFO
Least Recently In (LR I)

not in in
b c a c b a d c b

mru b c a c b a d c b
b c a c b a d c
lru * * b* b* a* c* b* a* d*
M M M H H H M M M
c b a *
LRU
Least Recently Used

REPLACEMENT ALGORITHMS EXAMPLES:


OPT, FIFO (QUEUE, CIRCULAR), LRU
Same Page Input Sequence For All Three
not in in

b c a c b d b a c b
SECOND b1 c1 a1 a0 a0 d1 d0 d1 c1 b1
CHANCE
make 1
b0 c0 c1 c0* b0 b1 b0* a0 c0
FIFO *
* * *
b0 *
b0 b1 a0* a0 a1 d0 a0*
* *
M M M H H M H H M M
c0 b0 d0 if * is 0 if * is 1
d
next=0
mri mri lri-and not recently used
a0 a0 a0

lri since b was lri


b1 recently used b1 b1
c0 d1
mri
c0

referenced no referenced
in clock in clock
AGING period period
1 register replace low
110 1 011 0
per page 0 0 0 0 1 0 0 0 0 1 0 0 101 0
binary numbers

REPLACEMENT ALGORITHMS-SECOND CHANCE, AGING


out = T0 out = T0
Tj-1 = Tj T0 = NEW
equivalent
Tn = NEW j = ( j- 1)modn

4 f mri
out e 5

g
in
f e d c b a
out
f e d c b a
= 3
d
a
0
5 4 3 210 5 43 21 0 2 b
g in c 1
a 3 f
out e 4

=
g
h g f e d c b a f e d c b g 2
d
mri
5
in out
5 4 3 2 10 4 3 210 5 1c
b
0lri
a
h in
b a
out

h g f e d c b a f e d c h g
i out
in
3 2 1 0 5 4
i in

d c b a
out

d c b a f e j i h g
k j i h g f e
in out 1 0 5 4 32
k in
e d c b a
out
l k j i h g f out e d c b a f k j i h g
in
0 5 4 3 21
l in
f e d c b a
out
m l k j i h g f e d c b a l k j i h g
in out
5 43 21 0
m in
FIFO-Push Down FIFO-Cicular

RELATION BETWEEN THE FIFO QUEUE AND


THE FIFO CIRCULAR DATA STRUCTURES
Process
On Each Replacement @ Degree

360
0 tst

180

0
Time

Process
@ Degree
0 360

180
tst

Time

Process
@ Degree

0 360

180

0
tst Time
Test Hand- tests for 1 = recently
referenced, if so do not replace,
otherwise replace
Clear Hand- make 0 = not recently
referenced
interval during which a page can
be referenced and get a second
chance

1-2 Handed Clock Algorithm


Based On Second Chance-FIFO
LRU MATRIX ALGORITHM
The LRU Matrix has a distinct row and column for each page frame. Page frame i is assigned
row i and column i. When s psge i isreferenced all entries in row i are made 1 and then all entries
in column i are made 0. At any time the page(s) whose row has the most 0s id s the least recently
used.

1 2 3 4
1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 1 1 0 0 1 1 1
2 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1 2
3 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 3
4 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 0 1 0 4
at the end ( ) at the end at the end 0 in at the end
( 1 )
in row 3
0 in 2 0 in
in row 1 cols in row 2 0 in
in row 4 cols 2 2
cols 2 cols 2
1 1 1 only
4 4 only
the end
3 only
only

In the final matrix there is exactly one 0 in row , 2. There are exactly two 0s in row 1,
because the last two pages referenced are 1 and 2. There are exactly three 0s in row 4
because the last three pages referenced were 1, 2 and 4. We can ignore the fact thatt
another use of page 2 occured earlier. There are exactly four 0s in row 3 because the
last four pages referenced were 1, 2, 3, and 4

1 (N-> 2) 2 (N- 3) (N-> N-2) N-2 (N-> N-1) N-1 (Ns) N has
0 in 0 in 0 in 0 in 0 in
cols N cols N cols N cols N cols N
N-1 N-1 N-1 N-1
N-2 N-2 N-2

3
2

GENERAL

REPLACEMENT ALGORITHMS LRU MATRIX IMPLEMENTATION


0 10 10 20

0 10 20 30

0 10 20 30 30 40 50 50 60 70 80 90
Process or Virtual Times)

0 10 20 30 40 60 80 90 100 110 120 130


Actual Times

Working Set of Process P = The set of P’s pages in current use

Formally w(k,t) = set of k pages most recently referenced at time t

An approximation: w(I,t) = set of pages referenced within the last


interval ot time I. I is determined by simulations or other experimental
methods.

I1 w(I1,t) = {p2,p4}

p1. p2. p1.p1. . p2. . p4.p3. . . p2. . . p1. p2. p1. . p3. . p1. p2p1. . . p2. . p4
w(I2,t) = {p1,p2,p3}
I2

A working set replacement algorithm replaces any page in MM which is not in


the working set.
To obtain a useful workiing set algorithm: keep the last virtual time at which a process
has been used. If it is within an interval, I,before the current virtual time it is in the
working set and should not be replaced.. f it was not used within that interval I
it may be replaced.

WORKING SET
Given that MM1 has fewer page frames than MM2:

If whenever ALG is used to allocate the same page input sequence to both MM! and MM2., every page
in MM1 is always also in MM2 , ALG is a STACK REPLACEMENT ALGORITHM..

ALG pages in MM2


abcabcdedab....
pages in MM1
ALG

Page Input
Sequence
a b c a d
1 a b c a d
2 _* a* b* c* a
U U U U U
Page
Frames
1 a b c c d
2 _*
a b b c
3 _ _* a* a* b*
FIFO Replacement Algorithm
So for FIFO it is necessary to keep tables having 1, 2, ...., n rows to record
the result of a simulation of all MMs having any number of pages from 1 to n.
So 1 + 2 +...+ n = O(n2) rows are required.
.
For a Stack Algorithm only n rows are required to record the result of
a simulation of all MMs having any number of pages from 1 to n.

STACK ALGORITHM
FIFO IS NOT ONE
Page Input Sequence
a b c a d e c e
1 a* b* c* a* d* e*c* e*
2 _ a* b* c* a* d*e* c*
Page
3 _ _ a* b* c* a*d* d*
Frames 4 _ _ _ _ b* c * a* a*
5 _ _ _ _ _ b* b*
b*
_ _ _ _ _ _ _ _
6
3 4 2

8
8
8
8

8
The Distance String

To represent the result of LRU run on a given page input sequence:


1) put new page arrivals in row 1 and push all others
down if that page is not already in any row of the table.
2) If the incoming page, P, is already in a row of the table put P in row 1, and now remove Ps
other occurence from the row it was in, and finally push down all other entries filling in the
blank left.
This is the same procedure one would use if there are a fixed number of pages, (therefore of
rows,) except when a new page came in and the MM was already filled the page at the bottom
of table would be removed.

At any stage in the reception of the input page sequence the page that would be removed
if the next page input required a replacement is the dispensable one.

The star (*) in this representation has significance:


x* in row k signifies that x is dispensable in a k page memory and, if the next row
with a starred (*) entry is in row k+ i, then x is dispensable also in row k + 1 to row
k +i + 1 . For LRU all entries are starred, so row n in a table with n rows (MM with
n pages) is the dispensabble page. This is not the case for OPT considered next.

LRU Stack Replacement Algorithm


The algorithm for filling in the table for OPT is not as simple as with LRU .

Unloke when using LRU, using OPT all pages in the table are starred. The
dispensable pages in each column are determined by the order in which those
pages in MM will again arrive in the future.

a b c a d e c e d b a e
1 a* b*c* a* d* e* c* e* d*
_ a a c c c e c* e
2
Page 3 _ _ b* b b* d* d d c*
Frames 4 _ _ _ _
a* b* b b b
5 _ _ _ _ _ a* a a a
_ _ _ _ _ _ _ _ _
6
2 2 2 3
8
8
8

8
8

C =5 C3 = 1 C2= 3
8

Ck is the number of hits in an MM with k page frames,but not in


an MM with fewer page frames. With k page frames there are
C1 +...+ Ck hits

OPT - A STACK REPLACEMENT ALGORITHM

You might also like