Page - New Replacement Algorithm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Simulation: FIFO, LFU and MFU Page Replacement

Algorithms (VB)
Shaify Mehta (http://www.shaify.com)

Abstract: 1) Look out the location of the page on the memory


disk.
Page replacement algorithm is an algorithm decides which 2) Look for a free frame
pages should be writing to disk when new page needs to  If free frame is available, use it.
allocated.  Use page replacement algorithm to pick the
victim page in case of no free frame.
Page replacement increases the system performance. While
 Write the victim page to the memory disk.
there is also a possibility of picking a randomize page to
Also make changes the entries in the frame
remove during the page fault occurs, in this case system
and page table.
performance will be optimize if small sized pages is chosen
3) Read the particular page into newly allocated free
rather than the heavily pages.
frame, change the frame and page tables accordingly.
Page replacement algorithms does the work on the basis of 4) Repeat the whole user process.
both theoretical and implementations. For example in case of
There are many different page replacement algorithms.
web server, the web server keeps tracks of numbers of pages
Every operating system probably has its own replacement
in the memory cache. A new page is referenced when the
scheme. Further we are going to discuss various types of
memory cache is full. In this case web server decides which
algorithms of page replacement algorithms.
page is to be removed.

FIFO (First-In, First Out):


Introduction:
The simplest and low-overhead page replacement
Page replacement takes the following procedure. If no frame
algorithm is the FIFO (First-In, First Out) algorithm. A
is free, we find one that is not currently being used and free it.
FIFO replacement algorithm links with each page the
We can free the frame by making changing in the page table
time when that page was added into the memory; the
to point that the page is no longer available in the memory.
oldest page is chosen when a page is going to be replaced.
Now we can modify and use the page fault procedure to
We can create a FIFO queue to hold all the pages present
include page replacement.
in the memory disk. At the head of the queue we replace time ago and is no longer needed. On the other hand, it could
the page. We insert page at the tail of the queue when a contain a heavily used variable that was initialized early and
page is added in into the memory disk. constant use.

Example: Algorithm:

We consider the following reference string: Public Sub loop1()

70120304230321201701 flag1 = 0

Buffer size: 3 flag2 = 0

For n = 0 To Val (Text2.Text) - 1


7 0 1 2 0 3 0 4 2 3
7 7 7 2 2 2 2 4 4 4 If fr(n) = page(m) Then

0 0 0 0 3 3 3 2 2 flag1 = 1
1 1 1 1 0 0 0 3
flag2 = 1

Exit Sub
0 3 2 1 2 0 1 7 0 1
End If
0 0 0 0 0 0 0 7 7 7
2 2 2 1 1 1 1 1 0 0 Next n

3 3 3 3 2 2 2 2 2 1 End Sub

Total page fault occurs: 15 Public Sub loop2()

Initially the three frames are empty. The first three references If flag1 = 0 Then
(7, 0, 1) causes pages faults and are added into the three empty
For i = 0 To Val(Text2.Text) - 1
frames. The next reference is (2) which replaces the page (7)
If fr(i) = -1 Then
because page (7) was added in first. Since (0) is the next
reference but (0) is already in the memory, so we have no fr(i) = page(m)
page fault for this reference. This process continues as shown
flag2 = 1
above. Every time a page fault occurs, we show which pages
are in our three frames. There are total 15 page faults. Exit Sub

End If
The FIFO page replacement algorithm is easy to implement
and understand but the performance wise is bad. The page Next i
replaced may be an initialization module that was used a long
End If which a page is used heavily during the staring phase of a
process, but then is never again. Since it was used heavily, it
End Sub
has a large frequency (count) and remains in memory ever
through it are no longer needed.

Public Sub loop3()


Example:
If flag2 = 0 Then
We consider the following reference string:
fr(top1) = page(m)
70120304230321201701
top1 = Val(top1) + 1
Buffer size: 3
pf = Val(pf) + 1
7 0 1 2 0 3 0 4 2 3
If top1 >= Val(Text2.Text) Then 7 7 7 2 2 3 3 4 2 2
0 0 0 0 0 0 0 0 0
top1 = 0
1 1 1 1 1 1 1 3
End If

End If 0 3 2 1 2 0 1 7 0 1
2 2 2 1 2 2 2 2 2 2
End Sub
0 0 0 0 0 0 0 0 0 0
3 3 3 3 3 3 1 7 7 1

Simulation:

Algorithm:

For LFUk = 0 To Val(LFUbn) - 1 'match c

If LFUpage(LFUi) = LFUb(LFUk) Then


LFU (Least Frequently Used):
LFUr(LFUk) = Val(LFUr(LFUk)) + 1
In LFU (Least Frequently Used) algorithm the page which has
LFUflag = 1
with the smallest count is going to be replaced. The reason for
this selection is that an mostly used page should have a large Exit For 'exit loop lfuk
reference count. This algorithm suffers from the situation in
End If
Next LFUk For LFUk = 0 To LFUi

If LFUpage(LFUi) = LFUpage(LFUk) Then

LFUc = Val(LFUc) + 1

If LFUflag = 0 Then LFUr(LFUlc) = LFUc

If LFUbn < LFUbf Then End If

'MsgBox "flag0" Next LFUk

LFUb(LFUbn) = LFUpage(LFUi) End If

LFUr(LFUbn) = Val(LFUr(LFUbn)) + 1 End If

LFUbn = Val(LFUbn) + 1 Simulation:

'MsgBox "bn :- " & LFUbn

Else

'MsgBox "2"

LFUl = LFUn
MFU (Most Frequently Used):
'MsgBox LFUn
In MFU (Most Frequently Used) algorithm the page which
For LFUk = 0 To Val(LFUbf) - 1 has with the largest count is going to be replaced.

If LFUr(LFUk) < LFUl Then Example:

LFUl = LFUr(LFUk) We consider the following reference string:

LFUlc = LFUk 70120304230321201701

End If Buffer size: 3

Next LFUk 7 0 1 2 0 3 0 4 2 3
7 7 7 2 2 2 0 4 2 2
0 0 0 0 3 3 3 3 3
LFUb(LFUlc) = LFUpage(LFUi) 1 1 1 1 1 1 1 1

LFUc = 0
0 3 2 1 2 0 1 7 0 1 'MsgBox MFUn
0 0 2 2 2 0 0 7 7 7
For MFUk = 0 To Val(MFUbf) - 1
3 3 3 3 3 3 3 3 0 0
1 1 1 1 1 1 1 1 1 1 If MFUr(MFUk) > MFUl Then

MFUl = MFUr(MFUk)
Algorithm:
MFUlc = MFUk
For MFUk = 0 To Val(MFUbn) - 1 'match c
End If
If MFUpage(MFUi) = MFUb(MFUk) Then
Next MFUk
MFUr(MFUk) = Val(MFUr(MFUk)) + 1
MFUb(MFUlc) = MFUpage(MFUi)
MFUflag = 1
MFUc = 0
Exit For 'exit loop MFUk
For MFUk = 0 To MFUi
End If
If MFUpage(MFUi) = MFUpage(MFUk) Then
Next MFUk
MFUc = Val(MFUc) + 1

MFUr(MFUlc) = MFUc
If MFUflag = 0 Then
End If
If MFUbn < MFUbf Then
Next MFUk
'MsgBox "flag0"
End If
MFUb(MFUbn) = MFUpage(MFUi)
End If
MFUr(MFUbn) = Val(MFUr(MFUbn)) + 1

MFUbn = Val(MFUbn) + 1
Simulation:
'MsgBox "bn :- " & MFUbn

Else

'MsgBox "2"

MFUl = 0
References:

en.wikipedia.org/wiki/Page_replacement_algorithm

You might also like