WRITE A PROGRAM THAT IMPLEMENTS THE OPTIMAL AND LRU PAGE REPLACEMENT ALGORITHMS.
ID: 3776624 • Letter: W
Question
WRITE A PROGRAM THAT IMPLEMENTS THE OPTIMAL AND LRU PAGE REPLACEMENT ALGORITHMS.
You may use any of the following languages for your program: C, C++, Java.
Write a program that implements the OPTIMAL and LRU page-replacement algorithms presented in this chapter. First, generate a random page reference string that consist of atleast 10000 numbers where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, Using the same reference string, obtain the number of faults for each algorithm (Optimal and LRU) for each of the following number of page frames:1, 2, 3, 4, 5, 6, 7, 8.and record the number of page faults incurred by each algorithm. Implement the replacement algorithms so that the number of page frames can vary as well. Assume that demand paging is used. Plot a graph of page fault frequency (y-axis) versus number of page frames (x-axis). Draw both plots on the same graph. Label each plot by the algorithm identifier (Optimal, LRU).
Note: it is not expected that your program will produce the plot; rather, a separate application can be used to make the plot.
Design and implement two classes—OPTIMAL and LRU—that extend ReplacementAlgorithm. Each of these classes will implement the insert() method, one class using the LRU page-replacement algorithm and the other using the OPTIMAL algorithm.
The random page-reference string should consist of at least 10000 numbers.Using the same reference string, obtain the number of faults for each algorithm (Optimal and LRU) for each of the following number of page frames:1, 2, 3, 4, 5, 6, 7, 8. Plot a graph of page fault frequency (y-axis) versus number of page frames (x-axis). Draw both plots on the same graph. Label each plot by the algorithm identifier (Optimal, LRU).Note: it is not expected that your program will produce the plot; rather, a separate application can be used to make the plot.
AT THE END THERE SHOULD BE :
GRAPH of page fault frequency (y-axis) versus number of page frames (x-axis). Draw both plots on the same graph. Label each plot by the algorithm identifier (Optimal, LRU).
Table of results for the number of faults for each algorithm (Optimal and LRU) for each of the following number of page frames:1, 2, 3, 4, 5, 6, 7, 8.
program listing
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int nop,nof,page[20],i,count=0;
cout<<" Enter the No. of Pages:";
cin>>nop;
cout<<" Enter the Reference String:";
for(i=0;i<nop;i++)
{
cout<<" ";
cin>>page[i];
}
cout<<" Enter the No of frames:-";
cin>>nof;
int frame[nof],fcount[nof];
for(i=0;i<nof;i++)
{
frame[i]=-1;
fcount[i]=0;
}
i=0;
while(i<nop)
{
int j=0,flag=0;
while(j<nof)
{
if(page[i]==frame[j]){
flag=1;
}
j++;
}
j=0;
cout<<" ************************************** ";
cout<<" "<<page[i]<<"-->";
if(flag==0)
{
if(i>=nof)
{
int max=0,k=0;
while(k<nof)
{
int dist=0,j1=i+1;
while(j1<nop)
{
if(frame[k]!=page[j1])
dist++;
else
{
break;
}
j1++;
}
fcount[k]=dist; //Storing Distances into array
k++;
}
k=0;
while(k<nof-1)
{
if(fcount[max]<fcount[k+1]) //Finding out the maxximum distance
max=k+1;
k++;
}
frame[max]=page[i];
}
else {
frame[i%nof]=page[i];
}
count++; // Increasing Page Fault.
while(j<nof)
{
cout<<" |"<<frame[j]<<"|";
j++;
}
}
i++;
}
cout<<" ************************************** ";
cout<<" Page Fault is:"<<count;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.