Exp-5 Page Replacement Algorithm
Exp-5 Page Replacement Algorithm
a)FIFO:
The simplest page-replacement algorithm is a first-in, first-out (FIFO)
algorithm. A FIFO replacement algorithm associates with each page the time
when that page was brought into memory. When a page must be replaced, the
oldest page is chosen.
Program:
#include<stdio.h>
main()
{
int nof,nref,ref[50],frm[3],pf;
int r=-1;
int flag=0;
int i,j;
printf("enter number of frames:");
scanf("%d",&nof);
printf("enter number of references:");
scanf("%d",&nref);
printf("enter pages refrenced:");
for(i=0;i<nref;i++)
scanf("%d",&ref[i]);
for(i=0;i<nof;i++)
frm[i]=-1;
for(i=0;i<nref;i++)
{
flag=0;
for(j=0;j<nof;j++)
{
if(ref[i]==frm[j])
{
flag=1;
break;
}
}
if(flag==0)
{
pf++;
r++;
r=r%nof;
frm[r]=ref[i];
printf("Frames in Memory:");
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
printf("\n");
}
}
printf("Number of Page Faullts are:%d",pf);
}
OUTPUT:
b) LRU
LRU stands for Least Recently Used. As the name suggests, this algorithm is based on
the strategy that whenever a page fault occurs, the least recently used page will be
replaced with a new page. So, the page not utilized for the longest time in the memory
(compared to all other pages) gets replaced. This strategy is known as LRU paging.
#include<stdio.h>
#include<conio.h>
main() {
int i, j, k, min, rs[25], m[10], count[10], flag[25], n, f, pf = 0, next = 1;
// Input: Length of reference string
printf("Enter the length of reference string -- ");
scanf("%d", &n);
m[min] = rs[i];
count[min] = next;
next++;
}
pf++;
}
// Output: Display the frames and page faults
for (j = 0; j < f; j++)
printf("%d\t", m[j]);
if (flag[i] == 0)
printf("PF No. -- %d", pf);
printf("\n");
}
// Output: Display the total number of page faults
printf("\nThe number of page faults using LRU are %d", pf);
}
Output:
LFU
Description:
The least frequently used (LFU) page-replacement algorithm requires that the page
with the smallest count be replaced
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0; a[i]=-1;
}
printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
if(j==f)
{ min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i]; cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
}
printf(“\tPF No. %d”,pf);}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
OUTPUT: