1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
dangina [55]
3 years ago
9

Write a program that implements the FIFO and LRU page-replacement algorithms learned in class. First, generate a random page ref

erence string where page numbers range from 0 to 9. Then, 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 can vary from 1 to 7. Assume demand paging is used.
Computers and Technology
1 answer:
erastova [34]3 years ago
3 0

Answer:

Code implemented using C++ below

Explanation:

sambswas

answered this

#include<iostream>

#include<cstdlib>

#define CYCLES 100

using namespace std;

struct page_table_entry {

int page_num;

int last_access_time;

int first_access_time;

};

int num_of_frames;

int sequence;

struct page_table_entry *page_table;

void init() {

page_table = new page_table_entry[num_of_frames];

for (int i = 0; i < num_of_frames; i++) {

page_table[i].page_num = -1;

page_table[i].first_access_time = -1;

page_table[i].last_access_time = -1;

}

sequence = 0;

}

int get_random_page() {

sequence++;

return rand() % 10;

}

int request_frame_LRU() {

for (int i = 0; i < num_of_frames; i++)

if (page_table[i].page_num == -1) {

cout << "Empty frame found at location " << i << endl;

return i;

}

int target = 0;

for (int i = 1; i < num_of_frames; i++)

if (page_table[i].last_access_time < page_table[target].last_access_time)

target = i;

cout << "target frame is " << target << ", containing page " << page_table[target].page_num << endl;

return target;

}

int request_frame_FIFO() {

for (int i = 0; i < num_of_frames; i++)

if (page_table[i].page_num == -1){

cout << "Empty frame found at location " << i << endl;

return i;

}

int target = 0;

for (int i = 1; i < num_of_frames; i++)

if (page_table[i].first_access_time < page_table[target].first_access_time)

target = i;

cout << "target frame is " << target << ", containing page " << page_table[target].page_num << endl;

return target;

}

int search_page(int page) {

for (int i = 0; i < num_of_frames; i++) {

if (page_table[i].page_num == -1)

continue;

if (page_table[i].page_num == page) {

cout << "page " << page << " found at location " << i << endl;

return i;

}

}

cout << "page " << page << " not found" << endl;

return -1;

}

void LRU() {

init();

int miss = 0;

int hit = 0;

for (int i = 0; i < CYCLES; i++) {

int page = get_random_page();

cout << "Requested page is: " << page << endl;

int location = search_page(page);

if (location > 0) {

page_table[location].last_access_time = sequence;

hit ++;

}

else {

location = request_frame_LRU();

page_table[location].page_num = page;

page_table[location].first_access_time = sequence;

page_table[location].last_access_time = sequence;

miss++;

}

cout << endl;

}

double hit_ratio = (double)hit/(hit+miss);

cout << "######## LRU ########" << endl;

cout << "hits: " << hit << endl;

cout << "misses: " << miss << endl;

cout << "hit ratio: " << hit_ratio << endl;

cout << "for frame size " << num_of_frames << endl;

cout << "#######################" << endl << endl;

}

void FIFO() {

init();

int miss = 0;

int hit = 0;

for (int i = 0; i < CYCLES; i++) {

int page = get_random_page();

cout << "Requested page is: " << page << endl;

int location = search_page(page);

if (location > 0) {

page_table[location].last_access_time = sequence;

hit ++;

}

else {

location = request_frame_FIFO();

page_table[location].page_num = page;

page_table[location].first_access_time = sequence;

page_table[location].last_access_time = sequence;

miss++;

}

cout << endl;

}

double hit_ratio = (double)hit/(hit+miss);

cout << "######## FIFO ########" << endl;

cout << "hits: " << hit << endl;

cout << "misses: " << miss << endl;

cout << "hit ratio: " << hit_ratio << endl;

cout << "for frame size " << num_of_frames << endl;

cout << "#######################" << endl << endl;

}

int main() {

srand(time(NULL));

num_of_frames = rand() % 7 + 1;

LRU();

FIFO();

}

You might be interested in
Please help me with these questions​
Ierofanga [76]

Answer:

cant see it properly :(

Explanation:

6 0
3 years ago
Read 2 more answers
In the maintenance phase of the waterfall model, the parts of a program are brought together into a smoothly functioning whole,
vivado [14]

Answer:

False

Explanation:

The maintenance phase of the waterfall model for software development involves making changes to the entire system or some parts of it for improved performance. Integration and testing is the step at which  all units developed at the implementation stage are brought together to form a whole functioning system. At this stage, the system is constantly checked for proper functionality so it can be deployed.

7 0
3 years ago
How does a firewall provide network security
ehidna [41]
Firewalls are connected with a network device which blocks untrusted network forming a barrier in between trusted and un-trusted network.
8 0
3 years ago
Different algorithms can be made to complete the same task in different ways.
musickatia [10]

Answer:

True hope this helps you and everyone!

7 0
3 years ago
Read 2 more answers
What type of internet connection do you think you'd get in Antarctica?
rusak2 [61]
Hello!
My best guess is you would have to use mediocre satellites that float over for internet connection.
3 0
3 years ago
Read 2 more answers
Other questions:
  • Which protocol is often used to publish web pages to a web server?
    5·1 answer
  • What does the picture indicate on the famous book “Dawn of the century”?​
    10·1 answer
  • Fundamental types of data, such as strings, integers, and real numbers, are known as
    5·1 answer
  • Olivia creates a personal budget. She enters her current savings account balance in cell D3. In cell A3, she calculates her inco
    8·1 answer
  • Assuming you have a TCF free student checking account, how many maximum overdraft fees can you incur in one day?
    12·1 answer
  • Audio editing software contains several codecs that allow you to
    15·1 answer
  • How can I do a project with a model on the steam machine?
    14·1 answer
  • Can you explain the difference between software and hardware? Tell me 3 examples of each one.
    10·1 answer
  • A manager suspects that one of his team members has been fraudulently accessing confidential and sensitive information and breac
    8·2 answers
  • External hard drives typically connect to a computer via an external port (such as a usb or ____ port) or a wireless connection.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!