0% found this document useful (0 votes)
12 views6 pages

Exp-5 Page Replacement Algorithm

The document describes three page replacement algorithms: FIFO, LRU, and LFU. Each algorithm is explained with a brief description and accompanied by C programming code that simulates the page replacement process. The output of each algorithm includes the number of page faults that occur during the simulation.

Uploaded by

jojises632
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)
12 views6 pages

Exp-5 Page Replacement Algorithm

The document describes three page replacement algorithms: FIFO, LRU, and LFU. Each algorithm is explained with a brief description and accompanied by C programming code that simulates the page replacement process. The output of each algorithm includes the number of page faults that occur during the simulation.

Uploaded by

jojises632
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/ 6

FIFO Page Replacement Algorithm

1. Simulate all page replacement algorithms.


a) FIFO b) LRU c) LFU etc……

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);

// Input: Reference string


printf("Enter the reference string -- ");
for (i = 0; i < n; i++) {
scanf("%d", &rs[i]);
flag[i] = 0;
}
// Input: Number of frames
printf("Enter the number of frames -- ");
scanf("%d", &f);

// Initialize count and frames


for (i = 0; i < f; i++) {
count[i] = 0;
m[i] = -1;
}
// Output: Display the Page Replacement process
printf("\nThe Page Replacement process is -- \n");
// Loop through the reference string
for (i = 0; i < n; i++) {
if (flag[i] == 0) {
// If the page is not in the frame
if (i < f) {
m[i] = rs[i];
count[i] = next;
next++;
} else {
min = 0;
for (j = 1; j < f; j++)
if (count[min] > count[j])
min = j;

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:

You might also like