You are on page 1of 6

Simulation: FIFO, LFU and MFU Page Replacement Algorithms (VB)

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

Abstract:
Page replacement algorithm is an algorithm decides which pages should be writing to disk when new page needs to allocated. Page replacement increases the system performance. While there is also a possibility of picking a randomize page to remove during the page fault occurs, in this case system performance will be optimize if small sized pages is chosen rather than the heavily pages. Page replacement algorithms does the work on the basis of both theoretical and implementations. For example in case of web server, the web server keeps tracks of numbers of pages in the memory cache. A new page is referenced when the memory cache is full. In this case web server decides which page is to be removed.

1) Look out the location of the page on the memory disk. 2) Look for a free frame If free frame is available, use it. Use page replacement algorithm to pick the victim page in case of no free frame. Write the victim page to the memory disk. Also make changes the entries in the frame and page table. 3) Read the particular page into newly allocated free frame, change the frame and page tables accordingly. 4) Repeat the whole user process. There are many different page replacement algorithms. Every operating system probably has its own replacement scheme. Further we are going to discuss various types of algorithms of page replacement algorithms.

Introduction:
Page replacement takes the following procedure. If no frame is free, we find one that is not currently being used and free it. We can free the frame by making changing in the page table to point that the page is no longer available in the memory. Now we can modify and use the page fault procedure to include page replacement.

FIFO (First-In, First Out):


The simplest and low-overhead page replacement algorithm is the FIFO (First-In, First Out) algorithm. A FIFO replacement algorithm links with each page the time when that page was added into the memory; the oldest page is chosen when a page is going to be replaced. We can create a FIFO queue to hold all the pages present

in the memory disk. At the head of the queue we replace the page. We insert page at the tail of the queue when a page is added in into the memory disk.

time ago and is no longer needed. On the other hand, it could contain a heavily used variable that was initialized early and constant use.

Example: We consider the following reference string: 70120304230321201701 Buffer size: 3 7 7 0 7 0 1 7 0 1 2 2 0 1 0 2 0 1 3 2 3 1 0 2 3 0 4 4 3 0 2 4 2 0 3 4 2 3

Algorithm:
Public Sub loop1() flag1 = 0 flag2 = 0 For n = 0 To Val (Text2.Text) - 1 If fr(n) = page(m) Then flag1 = 1 flag2 = 1 Exit Sub

0 0 2 3

3 0 2 3

2 0 2 3

1 0 1 3

2 0 1 2

0 0 1 2

1 0 1 2

7 7 1 2

0 7 0 2

1 7 0 1 End If Next n End Sub

Total page fault occurs: 15 Initially the three frames are empty. The first three references (7, 0, 1) causes pages faults and are added into the three empty frames. The next reference is (2) which replaces the page (7) because page (7) was added in first. Since (0) is the next reference but (0) is already in the memory, so we have no page fault for this reference. This process continues as shown above. Every time a page fault occurs, we show which pages are in our three frames. There are total 15 page faults. The FIFO page replacement algorithm is easy to implement and understand but the performance wise is bad. The page replaced may be an initialization module that was used a long

Public Sub loop2() If flag1 = 0 Then For i = 0 To Val(Text2.Text) - 1 If fr(i) = -1 Then fr(i) = page(m) flag2 = 1 Exit Sub End If Next i

End If End Sub

which a page is used heavily during the staring phase of a process, but then is never again. Since it was used heavily, it 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 If top1 >= Val(Text2.Text) Then top1 = 0 End If End If End Sub 0 2 0 3 3 2 0 3 2 2 0 3 1 1 0 3 2 2 0 3 0 2 0 3 1 2 0 1 7 2 0 7 0 2 0 7 1 2 0 1 7 0 7 0 1 7 0 1 2 2 0 1 0 2 0 1 3 3 0 1 0 3 0 1 4 4 0 1 2 2 0 1 3 2 0 3

Simulation:

Algorithm:
For LFUk = 0 To Val(LFUbn) - 1 'match c If LFUpage(LFUi) = LFUb(LFUk) Then

LFU (Least Frequently Used):


In LFU (Least Frequently Used) algorithm the page which has with the smallest count is going to be replaced. The reason for this selection is that an mostly used page should have a large reference count. This algorithm suffers from the situation in

LFUr(LFUk) = Val(LFUr(LFUk)) + 1 LFUflag = 1 Exit For 'exit loop lfuk End If

Next LFUk

For LFUk = 0 To LFUi If LFUpage(LFUi) = LFUpage(LFUk) Then LFUc = Val(LFUc) + 1

If LFUflag = 0 Then If LFUbn < LFUbf Then 'MsgBox "flag0" LFUb(LFUbn) = LFUpage(LFUi) LFUr(LFUbn) = Val(LFUr(LFUbn)) + 1 LFUbn = Val(LFUbn) + 1 'MsgBox "bn :- " & LFUbn Else 'MsgBox "2" LFUl = LFUn 'MsgBox LFUn

LFUr(LFUlc) = LFUc End If Next LFUk End If End If

Simulation:

MFU (Most Frequently Used):


In MFU (Most Frequently Used) algorithm the page which

For LFUk = 0 To Val(LFUbf) - 1 If LFUr(LFUk) < LFUl Then LFUl = LFUr(LFUk) LFUlc = LFUk End If Next LFUk

has with the largest count is going to be replaced. Example: We consider the following reference string: 70120304230321201701 Buffer size: 3 7 7 0 7 0 1 7 0 1 2 2 0 1 0 2 0 1 3 2 3 1 0 0 3 1 4 4 3 1 2 2 3 1 3 2 3 1

LFUb(LFUlc) = LFUpage(LFUi) LFUc = 0

0 0 3 1

3 0 3 1

2 2 3 1

1 2 3 1

2 2 3 1

0 0 3 1

1 0 3 1

7 7 3 1

0 7 0 1

1 7 0 1

'MsgBox MFUn For MFUk = 0 To Val(MFUbf) - 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