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

page replacement algorithms programs (1)

The document provides implementations of three page replacement algorithms: FIFO, LRU, and Optimal. Each algorithm is demonstrated with C code, showing how page faults are calculated based on user input for pages and frames. The output includes the number of page faults for each algorithm after processing a series of page requests.

Uploaded by

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

page replacement algorithms programs (1)

The document provides implementations of three page replacement algorithms: FIFO, LRU, and Optimal. Each algorithm is demonstrated with C code, showing how page faults are calculated based on user input for pages and frames. The output includes the number of page faults for each algorithm after processing a series of page requests.

Uploaded by

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

5.

Simulate Page Replacement Algorithms


a. FIFO
b. LRU
c. Optimal Page Replacement

a FIFO Page Replacement Algorithm.

#include<stdio.h>
void main()
{
int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0;
printf("Enter No. of Pages: ");
scanf("%d",&n);
printf("Enter Pages: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter No. of Frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
q[i]=-1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(p[i]==q[j])
z++;
if(z==0)
{
fault++;
l=l%m;
q[l]=p[i];
l++;
z=0;
for(k=0;k<m;k++)
printf("%d\t",q[k]);
printf("\n");
}
z=0;
}
printf("Number of Page faults: %d",fault);
}

OUTPUT:
Enter No. of Pages: 8
Enter Pages: 7 0 1 2 0 3 0 4
Enter No. of Frames: 3
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 3 1
2 3 0
4 3 0
Number of Page faults: 7
****

b. LRU Page Replacement Algorithm.

#include<stdio.h>
void main()
{
int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0,x[3],max=0,c,b,a;

printf("Enter No. of Pages: ");


scanf("%d",&n);
printf("Enter Pages: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter No. of Frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
q[i]=-1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(p[i]==q[j]) z++;
if(z==0)
{
fault++;
if(i>m-1)
{
for(a=0;a<m;a++)
x[a]=0;
for(c=0;c<m;c++)
for(b=i;b>=0;b--)
{
if(q[c]!=p[b]) x[c]++;
else break;
}
max=x[0];l=0;
if(x[1]>max){ max=x[1];l=1;}
if(x[2]>max){ max=x[2];l=2;}
}
q[l]=p[i];
l++;
z=0;
for(k=0;k<m;k++)
printf("%d\t",q[k]);
printf("\n");
}
z=0;
}
printf("No. of Page Faults: %d",fault);
}

OUTPUT:
Enter No. of Pages: 10
Enter Pages: 7 0 1 2 0 3 0 4 2 3
Enter No. of Frames: 3
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 3
4 0 3
4 0 2
4 3 2
No. of Page Faults: 8

****

c. OPTIMAL Page Replacement Algorithm.

#include<stdio.h>
void main()
{
int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0,x[3],max=0,c,b,a;

printf("Enter No. of Pages: ");


scanf("%d",&n);
printf("Enter Pages: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter No. of Frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
q[i]=-1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(p[i]==q[j])
z++;
if(z==0)
{
fault++;
if(i>m-1)
{
for(a=0;a<m;a++)
x[a]=0;
for(c=0;c<m;c++)
for(b=i;b<n;b++)
{
if(q[c]!=p[b])
x[c]++;
else
break;
}
max=x[0];l=0;
if(x[1]>max){ max=x[1];l=1;}
if(x[2]>max){ max=x[2];l=2;}
}
q[l]=p[i];
l++;
z=0;
for(k=0;k<m;k++)
printf("%d\t",q[k]);
printf("\n");
}
z=0;
}
printf("No. of Page faults: %d",fault);
}

OUTPUT:
Enter No. of Pages: 8
Enter Pages: 7 0 1 2 0 3 0 4
Enter No. of Frames: 3
7 -1 -1
7 0 -1
7 0 1
2 0 1
3 0 1
4 0 1
No. of Page faults: 6

You might also like