Question: Have to modify the code below the three modifying questions below. Thanks. a. Modify the code to generate a random page-reference string with some length

Have to modify the code below the three modifying questions below. Thanks.

a. Modify the code to generate a random page-reference string with some length you specify where page numbers range from 0 to 9. Also, you should be able to set the number of page frames from 1 to 9.

b. Also, print out the current content of the frames after each page reference.

c. Print out the total number of page faults at the end.

// Java implementation of FIFO page replacement

// in Operating Systems.

import java.util.HashSet;

import java.util.LinkedList;

import java.util.Queue;

class Test

{

// Method to find page faults using FIFO

static int pageFaults(int pages[], int n, int capacity)

{

// To represent set of current pages. We use

// an unordered_set so that we quickly check

// if a page is present in set or not

HashSet s = new HashSet<>(capacity);

// To store the pages in FIFO manner

Queue indexes = new LinkedList<>() ;

// Start from initial page

int page_faults = 0;

for (int i=0; i

{

// Check if the set can hold more pages

if (s.size() < capacity)

{

// Insert it into set if not present

// already which represents page fault

if (!s.contains(pages[i]))

{

s.add(pages[i]);

// increment page fault

page_faults++;

// Push the current page into the queue

indexes.add(pages[i]);

}

}

// If the set is full then need to perform FIFO

// i.e. remove the first page of the queue from

// set and queue both and insert the current page

else

{

// Check if current page is not already

// present in the set

if (!s.contains(pages[i]))

{

//Pop the first page from the queue

int val = indexes.peek();

indexes.poll();

// Remove the indexes page

s.remove(val);

// insert the current page

s.add(pages[i]);

// push the current page into

// the queue

indexes.add(pages[i]);

// Increment page faults

page_faults++;

}

}

}

return page_faults;

}

// Driver method

public static void main(String args[])

{

int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,

2, 3, 0, 3, 2};

int capacity = 4;

System.out.println(pageFaults(pages, pages.length, capacity));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!