OSL Pract 5

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

EXPERIMENT NO.

5
PAGE REPLACEMENT ALGORITHMS
AIM: Write a program to implement page Replacement strategies -
a) FIFO

b) LRU

c) OPTIMAL

DESCRIPTION:

Paging in Operating Systems (OS)


 Paging is a storage mechanism. Paging is used to retrieve processes
from secondary memory to primary memory.
 The main memory is divided into small blocks called pages. Now,
each of the pages contains the process which is retrieved into main
memory and it is stored in one frame of memory.

Virtual Memory in Operating Systems (OS)


 A storage method known as virtual memory gives the user the
impression that their main memory is quite large. By considering a
portion of secondary memory as the main memory, this is
accomplished.
 By giving the user the impression that there is memory available to
load the process, this approach allows them to load larger size
programs than the primary memory that is accessible.
 By doing this, the level of multiprogramming will be enhanced,
which will increase CPU consumption.

Demand Paging
 The Demand Paging is a condition which is occurred in the Virtual
Memory.
 We know that the pages of the process are stored in secondary
memory.
 The page is brought to the main memory when required. We do not
know when this requirement is going to occur. So, the pages are

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


brought to the main memory when required by the Page
Replacement Algorithms.

 Page replacement algorithms are an important part of virtual


memory management and it helps the OS to decide which memory
page can be moved out making space for the currently needed page.

 However, the ultimate objective of all page replacement algorithms


is to reduce the number of page faults.

Page Replacement Algorithms


There are three types of Page Replacement Algorithms. They are:

o Optimal Page Replacement Algorithm


o First In First Out Page Replacement Algorithm
o Least Recently Used (LRU) Page Replacement Algorithm

First in First out Page Replacement Algorithm


This is the first basic algorithm of Page Replacement Algorithms. This
algorithm is basically dependent on the number of frames used. Then
each frame takes up the certain page and tries to access it. When the
frames are filled then the actual problem starts. The fixed number of
frames is filled up with the help of first frames present. This concept is
fulfilled with the help of Demand Paging

After filling up of the frames, the next page in the waiting queue tries to
enter the frame. If the frame is present then, no problem is occurred.
Because of the page which is to be searched is already present in the
allocated frames.

Page Hit.

If the page to be searched is found among the frames then, this process is
known as Page Hit.

Page Fault

If the page to be searched is not found among the frames then, this process
is known as Page Fault.

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


 When Page Fault occurs this problem arises, then the First In First
Out Page Replacement Algorithm comes into picture.
 The First In First Out (FIFO) Page Replacement Algorithm removes
the Page in the frame which is allotted long back. This means the
useless page which is in the frame for a longer time is removed and
the new page which is in the ready queue and is ready to occupy
the frame is allowed by the First In First Out Page Replacement.
 1) FIFO Page replacement Algorithm
1. Start the process
2. Read number of pages n
3. Read number of pages no
4. Read page numbers into an array a[i]
5. Initialize avail[i]=0 .to check page hit
6. Replace the page with circular queue, while re-placing check page
availability in the frame Place avail[i]=1 if page is placed in the
frame Count page faults
7. Print the results.
8. Stop the process.

Example:

 Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2,


1, 2, 0 for a memory with three frames and calculate number of page
faults by using FIFO (First In First Out) Page replacement
algorithms.
 Solution:
 Points to Remember
 Page Not Found - - - > Page Fault
 Page Found - - - > Page Hit
 Reference String:

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


 Number of Page Hits = 8
 Number of Page Faults = 12
 The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
 The Page Hit Percentage = 8 *100 / 20 = 40%
 The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%

FIFO Page replacement program


#include <iostream>
using namespace std;
int main()
{
int incomingStream[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
cout<<" Incoming "<<" "<<" Frame 1 "<<" "<<"Frame 2"<<" Frame 3
";
int temp[ frames ];
for(m = 0; m < frames; m++)

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
cout<<"\n";
cout<<" "<<incomingStream[m]<<"\t\t";
for(n = 0; n < frames; n++)

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


{
if(temp[n] != -1)
cout<<" "<< temp[n];
else
cout<<" -";
}
}
cout<<"\nTotal Page Faults:\t"<< pageFaults;
return 0;
}
OUTPUT:

2) LRU : Least Recently Used Page replacement Algorithm

The Least Recently Used (LRU) Page Replacement Algorithms works on a certain
principle. The principle is:

Replace the page with the page which is less dimension of time recently used page in
the past.

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process

Example:
Suppose the Reference String is:

6, 1, 1, 2, 0, 3, 4, 6, 0

The pages with page numbers 6, 1, 2 are in the frames occupying the frames.

Now, we need to allot a space for the page numbered 0.

Now, we need to travel back into the past to check which page can be replaced.

6 is the oldest page which is available in the Frame.

So, replace 6 with the page numbered 0.

Let us understand this Least Recently Used (LRU) Page Replacement Algorithm
working with the help of an example.

Example:

Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0 for a


memory with three frames and calculate number of page faults by using Least Recently
Used (LRU) Page replacement algorithms.

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


Points to Remember

Page Not Found - - - > Page Fault

Page Found - - - > Page Hit

Reference String:

Number of Page Hits = 7

Number of Page Faults = 13

The Ratio of Page Hit to the Page Fault = 7 : 12 - - - > 0.5833 : 1

The Page Hit Percentage = 7 * 100 / 20 = 35%

The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 35 = 65%

SOURCE CODE :
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1; break;
}
}
if(flag1==0)
Page 33
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
break;
}

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


}
}
if(flag2==0)
{
flag2=1;
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k]) fs[i]=1;
}}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf+frsize);
getch();
}

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


void display()
{
int i; printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}
OUTPUT:
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
No of page faults: 7

3) OPTIMAL Page Replacement Algorithm


 This is the second basic algorithm of Page Replacement Algorithms. This
algorithm is basically dependent on the number of frames used. Then each
frame takes up the certain page and tries to access it. When the frames are filled
then the actual problem starts. The fixed number of frames is filled up with the
help of first frames present. This concept is fulfilled with the help of Demand
Paging

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


 After filling up of the frames, the next page in the waiting queue tries to enter
the frame. If the frame is present then, no problem is occurred. Because of the
page which is to be searched is already present in the allocated frames.
 The OPTIMAL Page Replacement Algorithms works on a certain principle. The
principle is:
 Replace the Page which is not used in the Longest Dimension of time in future
 This principle means that after all the frames are filled then, see the future pages
which are to occupy the frames. Go on checking for the pages which are already
available in the frames. Choose the page which is at last.

ALGORTHIM:
1. Start Program
2. Read Number Of Pages And Frames
3.Read Each Page Value
4. Search For Page In The Frames
5.If Not Available Allocate Free Frame
6. If No Frames Is Free Repalce The Page With The Page That Is
Leastly Used
7.Print Page Number Of Page Faults
8.Stop process.
Example:

Example:

Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 4, 0 for a


memory with three frames and calculate number of page faults by using OPTIMAL
Page replacement algorithms.

Points to Remember

Page Not Found - - - > Page Fault

Page Found - - - > Page Hit

Reference String:

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


Number of Page Hits = 8

Number of Page Faults = 12

The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66

The Page Hit Percentage = 8 *100 / 20 = 40%

The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%

SOURCE CODE:
/*
Program to simulate optimal page replacement */
#include<stdio.h>
#include<conio.h>
int fr[3], n, m;
void
display();
void main()
{
int i,j,page[20],fs[10];

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


int
max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
float pr;
clrscr();
printf("Enter length of the reference string: ");
scanf("%d",&n);
printf("Enter
the
for(i=0;i<n;i++)
reference
scanf("%d",&page[i]);
string:
printf("Enter no of frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=-1; pf=m;
");
Page 36
for(j=0;j<n;j++)
{
flag1=0;
flag2=0;
for(i=0;i<m;i++)
{
Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)
if(fr[i]==page[j])
{
flag1=1; flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j]; flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<m;i++)
lg[i]=0;
for(i=0;i<m;i++)
{
for(k=j+1;k<=n;k++)
Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<m;i++)
{
if(lg[i]==0)
{
index=i;
found = 1;
break;
}
}
if(found==0)
{
max=lg[0]; index=0;
for(i=0;i<m;i++)
{
if(max<lg[i])
Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)
{
max=lg[i];
index=i;
}
}
}
fr[index]=page[j];
pf++;
}
display();
}
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr); getch();
}
void display()
{
int i; for(i=0;i<m;i++)
printf("%d\t",fr[i]);
printf("\n");
}
OUTPUT:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)
Enter no of frames: 3
1 -1 -1
1 2 -1
123
124
124
124
125
125
125
325
425
425
Number of page faults : 7 Page fault rate = 58.333332
Conclusion:
In summary, page replacement algorithms are essential for
managing a computer’s memory efficiently. They help ensure
that the system runs smoothly by reducing the number of times
it needs to fetch data from slower storage. Different algorithms,
like FIFO and LRU , have their own pros and cons, and choosing
the right one depends on the specific needs of the system.
Understanding these algorithms can help improve system
performance and make our computers faster and more efficient.
VIVA QUESTIONS
1) What is meant by page fault?
2) What is meant by paging?

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)


3) What is page hit and page fault rate?
4) List the various page replacement algorithm
5) Which one is the best replacement algorithm?

Mrs.Nitanjali Mane (AI-DS Dept,DYPCOE)

You might also like