Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the question is Write a program that implements the FIFO, LRU, and optimal page-

ID: 3829347 • Letter: T

Question

the question is Write a program that implements the FIFO, LRU, and optimal page- replacement algorithms presented in this chapter. First, generate a random page-reference string where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms so that the number of page frames can vary from 1 to 7. Assume that demand paging is used.

with the below modification:

Your program should execute like: ./a.out a b c, where

a is the number of page references,

here, each page reference is a random number in the range of [0, b], where 0 is minimum page number and b is maximum page number,

c is number of physical frames.

The output should be the number of page faults for each of the three algorithms.

and answer below question:

on page 413 of the textbook, it shows a graph that the number of page faults decreases with increase of number of frames, and finally stabilizes, at which moment, what is the value of the number of the page faults? Use user inputs a, b, c to answer the question, and demonstrate it using test cases with screen shots.

9.4 Page Replacement 16 14 12 10 number of frames Figure 9.11 Graph of page faults versus number of frames. 413

Explanation / Answer

#include<iostream>
void FIFO(char [],char [],int,int);
void lru(char [],char [],int,int);
void opt(char [],char [],int,int);
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];
clrscr();
system(“clear”);
printf(“ Enter the no of empty frames: “);
scanf(“%d”,&f);
printf(“ Enter the length of the string: “);
scanf(“%d”,&l);
printf(“ Enter the string: “);
scanf(“%s”,s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
system(“clear”);
printf(“ **MENU **”);
printf(“ 1:FIFO 2:LRU 3:OPT 4:EXIT”);
printf(“ Enter your choice: “);
scanf(“%d”,&ch);
system(“clear”);
switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
F[i]=-1;
}

FIFO(s,F,l,f);
break;
case 2:
for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;
case 3:
for(i=0;i<f;i++)
{
F[i]=-1;
}

opt(s,F,l,f);
break;
case 4:
exit(0);
}
printf(“ Do u want to continue IF YES PRESS 1 IF NO PRESS 0 : “);
scanf(“%d”,&YN);
}while(YN==1);return(0);
}
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf(“ PAGE FRAMES FAULTS”);
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}

if(flag==0)
{
printf(“ %c ”,s[i]);
F[j]=s[i];
j++;

for(k=0;k<f;k++)
{
printf(” %c”,F[k]);
}
printf(“ Page-fault%d”,cnt);
cnt++;
}

else
{
flag=0;
printf(“ %c ”,s[i]);
for(k=0;k<f;k++)
{
printf(” %c”,F[k]);
}

printf(“ No page-fault”);
}
if(j==f)
j=0;
}

}

//LRU
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,top=0;
printf(“ PAGE FRAMES FAULTS”);
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
{
flag=1;
break;
}
}
printf(“ %c ”,s[i]);
if(j!=f && flag!=1)
{
F[top]=s[i];
j++;

if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
}

F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}

F[top]=s[i];
}
}
for(k=0;k<f;k++)
{
printf(” %c”,F[k]);
}

if(flag==0)
{
printf(“ Page-fault%d”,cnt);
cnt++;
}
else
printf(“ No page fault”);
flag=0;
}

}
void opt(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,cnt=0,temp[10];
printf(“ PAGE FRAMES FAULTS”);
for(i=0;i<10;i++)
temp[i]=0;
for(i=0;i<f;i++)
F[i]=-1;
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}

if(j!=f && flag==0)
{
F[j]=s[i];
j++;
}
else if(flag==0)
{
for(m=0;m<f;m++)
{
for(k=i+1;k<l;k++)
{
if(F[m]!=s[k])
{
temp[m]=temp[m]+1;
}
else
break;
}
}
m=0;
for(k=0;k<f;k++)
{
if(temp[k]>temp[m])
{
m=k;
}
}
F[m]=s[i];
}
printf(“ %c ”,s[i]);
for(k=0;k<f;k++)
{
printf(” %c”,F[k]);
}
if(flag==0)
{
printf(“ Page-fault %d”,cnt);
cnt++;
}
else
printf(“ No Page-fault”);
flag=0;

for(k=0;k<10;k++)
temp[k]=0;
}
}