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

WRITE A C++ or Java PROGRAM THAT IMPLEMENTS THE OPTIMAL AND LRU PAGE REPLACEMENT

ID: 3776689 • Letter: W

Question

WRITE A C++ or Java PROGRAM THAT IMPLEMENTS THE OPTIMAL AND LRU PAGE REPLACEMENT ALGORITHMS.

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

java program for LRU

import java.util.*;
class LruAlgo
{
int p[],n,fr[],m,fs[],index,k,l,flag1=0,flag2=0,pf=0,frsize=3,i,j;
Scanner src=new Scanner(System.in);
void read()
{

System.out.println("Enter page table size");
n=src.nextInt();
p=new int[n];
System.out.println("Enter element in page table");
for(int i=0;ip[i]=src.nextInt();

System.out.println("Enter page frame size");
m=src.nextInt();
fr=new int[m];
fs=new int[m];
}

void display()
{

System.out.println(" ");
for(i=0;i{

if(fr[i]==-1)
System.out.println("[ ]");
else
System.out.println("["+fr[i]+"]");
}
}void lru()
{
for(i=0;i  

{
fr[i]=-1;
}
for(j=0;j   {
flag1=0;flag2=0;
for(i=0;i   {
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i   {
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
System.out.print("Page : "+p[j]);
display();
}
System.out.println(" no of page faults :"+pf);
}

public static void main(String args[])
{
LruAlgo a=new LruAlgo();
a.read();
a.lru();
a.display();
}
}

Program for optimal page replacement