0% found this document useful (0 votes)
865 views7 pages

Program: (Fifo) : 10) A) Program To Implement Fifo Page Replacement Algorithm

The document contains programs to implement three page replacement algorithms: FIFO, LRU, and optimal. The FIFO program tracks page faults and replaces the oldest page. The LRU program also tracks faults and replaces the least recently used page. The optimal program minimizes faults by predicting future references to determine which page to replace. Sample outputs are provided for each algorithm.

Uploaded by

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

Program: (Fifo) : 10) A) Program To Implement Fifo Page Replacement Algorithm

The document contains programs to implement three page replacement algorithms: FIFO, LRU, and optimal. The FIFO program tracks page faults and replaces the oldest page. The LRU program also tracks faults and replaces the least recently used page. The optimal program minimizes faults by predicting future references to determine which page to replace. Sample outputs are provided for each algorithm.

Uploaded by

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

10) a) PROGRAM TO IMPLEMENT FIFO PAGE REPLACEMENT ALGORITHM

PROGRAM:(FIFO)
#include<stdio.h>
int m,n,i,j,k,flag,count=0, refer[100], page_frame[100][2], fault=0,min,no_frames;
void replace(int z)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;j<no_frames;j++)
if(refer[i]==page_frame[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;j<no_frames;j++)
if(page_frame[j][1]<min)
{
min=page_frame[j][1];
k=j;
}
page_frame[k][0]=refer[i];
page_frame[k][1]=++count;
for(j=0;j<no_frames;j++)
printf("\t %d",page_frame[j][0]);
printf("\n");
}
else
{
printf("No page fault \n");
if(z==2)
page_frame[m][1]=++count;
}
}
printf("Number of page fault is : %d \n", fault);
}
int main()
{
printf(" \n Enter the number of reference:");
scanf("%d", &n);
printf("\n Enter the number of frames:");
scanf("%d", &no_frames);
printf("\n Enter the number of reference string ");
for(i=0;i<n;i++)
scanf("%d", &refer[i]);
printf("\t\t\t FIFO ALGORITHM \n");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(1);
return 0;
}
OUTPUT:
Enter the number of reference:20
Enter the number of frames:3
Enter the number of reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
FIFO ALGORITHM
7 -1 -1
7 0 -1
701
201
No page fault
231
230
430
420
423
023
No page fault
No page fault
013
012
No page fault
No page fault
712
702
701
Number of page fault is : 15
b) PROGRAM TO IMPLEMENT LRU PAGE REPLACEMENT ALGORITHM

PROGRAM:(LRU)
#include<stdio.h>
int m,n,i,j,k,flag,count=0, refer[100], page_frame[100][2], fault=0,min,no_frames;
void replace(int z)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;j<no_frames;j++)
if(refer[i]==page_frame[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;j<no_frames;j++)
if(page_frame[j][1]<min)
{
min=page_frame[j][1];
k=j;
}
page_frame[k][0]=refer[i];
page_frame[k][1]=++count;
for(j=0;j<no_frames;j++)
printf("\t %d",page_frame[j][0]);
printf("\n");
}
else
{
printf("No page fault \n");
if(z==2)
page_frame[m][1]=++count;
}
}
printf("Number of page fault is : %d \n", fault);
}
int main()
{
printf(" \n Enter the number of reference:");
scanf("%d", &n);
printf("\n Enter the number of frames:");
scanf("%d", &no_frames);
printf("\n Enter the number of reference string ");
for(i=0;i<n;i++)
scanf("%d", &refer[i]);
printf("\t\t\t LRU ALGORITHM \n");
for(i=0;i<no_frames;i++)
{
page_frame[i][0]=-1;
page_frame[i][1]=count;
}
replace(2);
return 0;
}
OUTPUT:
Enter the number of reference: 20
Enter the number of frames:3
Enter the number of reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
LRU ALGORITHM
7 -1 -1
7 0 -1
701
201
No page fault
203
No page fault
403
402
432
032
No page fault
No page fault
132
No page fault
102
No page fault
107
No page fault
No page fault
Number of page fault is : 12
c) PROGRAM TO IMPLEMENT LFU(Optimal) PAGE REPLACEMENT ALGORITHM

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n................................");
printf("\nThe given string");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}

OUTPUT:

OPTIMAL PAGE REPLACEMENT ALGORITHM

Enter no.of Frames....3


Enter no.of reference string..6

Enter reference string..


654231

OPTIMAL PAGE REPLACEMENT ALGORITHM


The given reference string:
…………………. 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1

No.of page faults...6

You might also like