0% found this document useful (0 votes)
18 views4 pages

EXP 10 Optimal Page Replacement PDF

optimal page

Uploaded by

ibmcomputer112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

EXP 10 Optimal Page Replacement PDF

optimal page

Uploaded by

ibmcomputer112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Objective: To implement optimal page replacement technique.

Theory: -Page replacement algorithms are an important part of virtual


memory management and it help 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.
In this algorithm, page are replaced which would not be used for the
longest duration of time in the future. This algorithm will give us less
page fault when compared to other page replacement algorithms.

ALGORITHMS:
1. Start Program
2. Read number of page and frames
3. Read each page values
4. Search for page in the frames
5. If no frames is free replace the page with the page that is leastly
used
6. Print page number of page faults
7. Stop the process.
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
int fr[3],n,m;
void display();
void main()
{
int i, j,page[20],fs[10];
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 reference string:");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("enter no of frames:");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=-1;pf=m;
for(j=0;j<n;j++)
{
flag1=0;flag2=0;
for(i=0;i<m;i++)
{
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++)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<m;i++)
{
if(lg[i]==0)
{
index=1;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];index=0;
for(i=0;i<m;i++)
{
if(max<lg[i])
{
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]);
print("\n");
}

OUTPUT:

You might also like