Question

This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Ecl...

This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Eclipse) Thanks

Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) 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 goes from 1 to 7 and you must compute the page fault for each of these frame numbers. Record the number of page faults with each of these different page frames numbers and each of the different algorithms. Assume that demand paging is used. Remember to count the first time a page comes in, as this is a page fault in demand paging.

Then do the same procedure, except use the following page-reference string instead of the random one:

0,7,1,1,2,0,8,9,0,3,0,4,5,6,7,0,8,9,1,2 and then lastly do it with the page-reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1

Note this last string appears in examples in your text on pages 387-390 (use this string to verify some of your code).

Have separate clearly marked classes or methods for LRU, FIFO, and Optimal replacement algorithms. Also have comments within your code.

Make certain to have your name, date, assignment number, and a brief description of the program at the top of your main method.

Your output should be in the following format (repeated seven times, one for each number of page frames, and the set of seven repeated three times, one for each of the three page-reference strings):

For x page frames, and using string page reference string nnnnnnnnnnnnnn:

          FIFO had ### page faults.

          LRU had ### page faults.

          Optimal had ### page faults.

Where ### is the number of page faults, x is the number of page frames, and nnnnnn is the page-reference string.

Remember to remove or turn off all of any debug output before you generate the output to send to me. Do not make the program interactive, it should run through the three strings with no human interaction.

Your name and the assignment number must appear at the top your output file by your program.

Note the Optimal algorithm is much harder than either of the other two.

Look for errors:

  • More page faults than items – not clearing a counter?
  • No page faults – initial fault at least.
  • Hand check your results.
  • Compare with the results in the book.
  • Optimal have more page fault than one of the other methods
0 0
Add a comment Improve this question Transcribed image text
Answer #1

For x page frames, and using string page reference string nnnnnnnnnnnnnn:

FIFO had ### page faults.

LRU had ### page faults.

Optimal had ### page faults.

Program:

// Define a header files

import java.io.IOException;

import java.util.Random;

import java.util.Scanner;

// Implement Class Replacement

public class Replacement

{

// define a read input

static Scanner scan=new Scanner(System.in);

int no_pages, page[], no_frames, frames[], faults, count;

double rate;

// define constructor

public Replacement() throws IOException

{

// Read Number of Pages

System.out.println("Enter Number of Pages");

no_pages=scan.nextInt();

page=new int[no_pages];

// Read Number of Frames

System.out.println("Enter Number of Frames");

no_frames=scan.nextInt();

frames=new int[no_frames];

count=1;

}

// Function to Reset the Frame Array

void Frame_reset()

{

int j;

for(j=0;j<no_frames;j++)

frames[j]=0;

faults=0;

count=1;

}

// Page Reference Randomly Generating

void Page_reference() throws IOException

{

int i;

// Rabdomly Generating the String

for(i=0;i<no_pages;i++)

{

page[i]=0 + (int)(Math.random() * ((9 - 0) + 1));

}

System.out.println("The Reference String is");

// Printing the String

for(i=0;i<no_pages;i++)

{

System.out.print(page[i]);

}

System.out.println();

for(i=0;i<no_frames;i++)

frames[i]=-1;

}

// FIFO Replacement

void FIFO_repacement()

{

int i,j,k=0;

// Reset the frame set

Frame_reset();

boolean fd=false;

for(i=0;i<no_pages;i++)

{

for(j=0;j<no_frames;j++)

{

if(page[i]==frames[j])

fd=true;

}

if(fd==false)

{

frames[k]=page[i];

if(k==no_frames-1)

k=0;

else

k++;

faults++;

}

Display();

fd=false;

}

System.out.println("Number of Page Faults = "+faults);

System.out.println("Fault Rate = "+(faults/no_pages));

}

// LRU Replacement

void LRU_replacement()

{

int i,j,dr[],max;

Frame_reset();

dr=new int[no_frames];

boolean found=false;

for(i=0;i<no_pages;i++)

{

for(j=0;j<no_frames;j++)

dr[j]++;

for(j=0;j<no_frames;j++)

{

if(page[i]==frames[j])

{

found=true;

dr[j]=0;

}

}

if(found==false)

{

max=0;

for(j=0;j<no_frames;j++)

{

if(dr[j]>dr[max])

max=j;

}

frames[max]=page[i];

dr[max]=0;

faults++;

}

Display();

found=false;

}

System.out.println("Number of Page Faults = "+faults);

System.out.println("Fault Rate = "+(faults/no_pages));

}

// Disply Function

void Display()

{

int i;

System.out.print("Page Frame "+count+" :");

for(i=0;i<no_frames;i++)

{

if(frames[i]==-1)

System.out.print(" -");

else

System.out.print(" "+frames[i]);

}

System.out.print(" o_pages");

count++;

}

public static void main(String[] args) throws IOException{

int me;

String cha;

Replacement p=new Replacement();

p.Page_reference();

do

{

System.out.println("1. FIFO");

System.out.println("2. LRU");

System.out.println("Enter option");

me=scan.nextInt();

switch(me)

{

case 1: p.FIFO_repacement();

break;

case 2: p.LRU_replacement();

break;

default: System.out.println("Invalid input");

}

System.out.println("Enter Y to continue");

scan.nextLine();

cha=scan.nextLine();

}

while(cha.compareToIgnoreCase("y")==0);

}

}

Output:

Output-JavaApplication28 (run) #2 2g un Enter Number of Pages 10 Enter Number of Frames ル! The Reference String is 2383970033

Add a comment
Know the answer?
Add Answer to:
This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Ecl...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT