In Java implement the FIFO and LRU page-replacement algorithms. First, generate
ID: 3767692 • Letter: I
Question
In Java implement the FIFO and LRU page-replacement algorithms. First, generate a random page-reference string where page numbers range from 0 to 9.
For example: 59010280 or 21317, etc.
Apply the random page-reference string to each algorithm.
Initially set the number of page frames to 2 then increase one page frame to each test run until the number of page frames is 7.
Record the number of page faults incurred by each algorithm. Assume that demand paging is used.
Output format: first line is the random page-reference string you used for your test runs. From second line on, divide each line into 3 columns. First column is the number of page frames; second column is the number of page faults incurred by FIFO algorithms; third column is the number of page faults incurred by LRU algorithms.
Example:
21341890
2 8 8
3 … …
4 …
5
6
7
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class pgReplace
{
public static void main(String[] args) throws IOException
{
ArrayList<Integer> ref = new ArrayList<Integer>();
try {
Scanner filein = new Scanner(getInputFileNameFromUser());
while(filein.hasNext()) {
String frame = filein.next();
int frm = Integer.parseInt(frame);
ref.add(frm);
}
filein.close();
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
if(!ref.isEmpty())
{
int size = ref.size();
System.out.println("The length of the Reference String is: " + size);
}
Iterator<Integer> iter = ref.iterator();
while (iter.hasNext())
System.out.print(iter.next() +" ");
Integer frame[] = new Integer[ref.size()];
frame = ref.toArray(frame);
System.out.printf(" Enter your choice which replacement algorithm you want: ");
System.out.printf(" 1.FIFO 2.LRU ? ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next();
}
int ch = sc.nextInt();
switch(ch)
{
case 1: fifo(frame,ref.size());
break;
case 2: lru(frame,ref.size());
break;
default: System.out.printf(" Choice is Invlaid");
}
}
public static void lru(Integer[] page, int n) throws IOException {
int [] frame = new int[10];
int []used = new int[10];
int index = 0;
int a,b,c,temp;
int flag=0,pf=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" LRU Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next();
}
int nf = sc.nextInt();
for(a=0;a<nf;a++)
frame[a]= -1;
for(a=0;a<n;a++){
flag=0;
for(b=0;b<nf;b++){
if(frame[b]==page[a]){
System.out.printf(" %d: ", page[a]);
out.printf(" %d: ", page[a]);
flag=1;
break;
}
}
if(flag==0)
{
for(b=0;b<nf;b++)
used[b]=0;
try{
for(b = 0,temp= a-1;b < nf-1;b++,temp--){
for(c = 0;c < nf;c++){
if(frame[c]==page[temp])
used[c]=1;
}
}
}
catch(ArrayIndexOutOfBoundsException e){
}
for(b=0;b<nf;b++)
if(used[b]==0)
index=b;
frame[index]=page[a];
System.out.printf(" %d: ", page[a]);
System.out.printf("--->F ");
out.printf(" %d: ", page[a]);
out.printf("--->F ");
pf++;
}
for(c= nf-1;c>=0;c--)
if(frame[c] != -1){
System.out.printf(" %d",frame[c]);
out.printf(" %d",frame[c]);
}
}
System.out.printf(" Number faultsin pageis: %d ",pf);
out.printf(" Number of page faults is: %d ",pf);
out.close();
write.close();
}
public static void fifo(Integer[] pages, int pg) throws IOException
{
int [] frame = new int[25];
int a,c,avail,count=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" FIFO Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next();
}
int nof = sc.nextInt();
for(a=0;a <nof;a++)
frame[a]= -1;
int b=0;
System.out.printf(" ");
out.printf(" ");
for(a=0;a<pg;a++){
System.out.printf("%d ",pages[a]);
out.printf("%d ",pages[a]);
avail=0;
for(c=0;c<nof;c++)
if(frame[c]==pages[a])
avail=1;
if (avail==0)
{
frame[b]=pages[a];
b=(b+1) % nof;
count++;
for(c=0;c<nof;c++)
if(frame[c]!=-1){
System.out.printf("%d",frame[c]);
out.printf("%d",frame[c]);
}
System.out.printf("-->F");
out.printf("-->F");
}
if(avail==1){
for(c=0;c<nof;c++)
if(frame[c]!=-1){
System.out.printf("%d",frame[c]);
out.printf("%d",frame[c]);
}
}
System.out.printf(" ");
out.printf(" ");
}
System.out.printf(" No of Faults: %d",count);
out.printf(" No of Faults: %d",count);
out.close();
write.close();
}
static File getInputFileNameFromUser()
{
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.