0% found this document useful (0 votes)
84 views

Page Replacement Algorithm

The document describes the implementation of three page replacement algorithms - FIFO, LRU, and optimal page replacement. The FIFO algorithm replaces the oldest page in the frame. It tracks the page frames and number of page faults. The LRU algorithm replaces the least recently used page. It tracks the reference string, page frames, and compares pages to find the least recently used one. The optimal algorithm replaces the page that will not be used for the longest time in the future. It finds the page with the maximum distance to its next reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Page Replacement Algorithm

The document describes the implementation of three page replacement algorithms - FIFO, LRU, and optimal page replacement. The FIFO algorithm replaces the oldest page in the frame. It tracks the page frames and number of page faults. The LRU algorithm replaces the least recently used page. It tracks the reference string, page frames, and compares pages to find the least recently used one. The optimal algorithm replaces the page that will not be used for the longest time in the future. It finds the page with the maximum distance to its next reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IMPLEMENTATION OF FIFO PAGE REPLACEMENT ALGORITHM

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:

ENTER THE NUMBER OF PAGES: 20


ENTER THE PAGE NUMBER : 70120304230321201701
ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1

Page Fault Is 15
IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

OUTPUT:

Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2

The no of page faults is 10


IMPLEMENTATION OF OPTIMAL PAGE REPLACEMENT ALGORITHM

#include<stdio.h>
main()
{
int i,j,k,l,m,n,p,c=0,s;
int a[20],b[20],q,max;
printf("enter no. of reference string: ");
scanf("%d",&n);
printf("enter size of frame: ");
scanf("%d",&m);
printf("enter the elements of ref. string: \n");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(j=0;j<m;j++)
b[j]=-1; //initialize all frame elements with -1

for(i=0;i<n;i++)
{

for(k=0;k<m;k++)
if(b[k]==a[i])

for(j=0;j<m;j++)
{
if(b[j]==-1)//check if element already present in frame,if true then no page fault.
{
b[j]=a[i];
c++;

if(j==m)
{
l=i+1,max=0;
for(j=0;j<m;j++)
{

for(s=l;s<n;s++)
{
if(a[s]==b[j])
{
if(s>max)
{
max=s;
p=j;
}
break;
}
}
if(s==n)
{
max=s;
p=j;
}

}
}
b[p]=a[i];
c++;

printf("\n\n");
for(k=0;k<m;k++)
printf(" %d",b[k]);

}
printf("\n No of page fault is:%d",c);

[kannan@fosslabserver ~]$ cc a.c


[kannan@fosslabserver ~]$ ./a.out
enter no. of reference string: 5
enter size of frame: 1
enter the elements of ref. string:
2
3
1
3
2

2
No of page fault is:5
[kannan@fosslabserver ~]$

You might also like