Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I'm working on a replacement algorithm in C, I have been trying to implement a queue using Linked List. I don't know if I

Hi I'm working on a replacement algorithm in C, I have been trying to implement a queue using Linked List. I don't know if I am in the right track I just need something to run five scenarios that I commented out in the code I have provided. Please if someone can help me out I'm having a hard time trying to figure this out.

#include  #include  #include  #include  #define FRAME_NO 10 #define lrand() rand()*RAND_MAX + rand() int locate(int* frames, int size, int page_no) { for (int i = 0; i < size; i++) if (frames[i] == page_no) return i; return -1; } int fifo(int* ref_str, int size, int limit, int value) { //size is the # of frames allocated for the process //limit is the max # of cells that we look ahead in the RS to implement the optimal algorithm //initialize struct node { int data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; // Enqueue() operation on a queue void enqueue(int value); { struct node* ptr; ptr = (struct node*)malloc(sizeof(struct node)); ptr->data = value; ptr->next = NULL; if ((front == NULL) && (rear == NULL)) { front = rear = ptr; } else { rear->next = ptr; rear = ptr; } } // Dequeue() operation on a queue int dequeue(); { if (front == NULL) { printf(" Underflow "); return -1; } else { struct node* temp = front; int temp_data = front->data; front = front->next; free(temp); return temp_data; } } int page_faults = size; int frames[FRAME_NO], i, cur, page_no, frame_no; for (i = 0; i < size; i++) frames[i] = -1;//empty for (i = 0, cur = 0; i < size; i++, cur++) { //filling out the whole physical memory (frames array) page_no = ref_str[cur]; frame_no = locate(frames, size, page_no); if (frame_no + 1)//already exists i--; else frames[i] = page_no; } //main loop for (; cur < 1000000; cur++) { page_no = ref_str[cur]; //calls page number frame_no = locate(frames, size, page_no); //locate page number in frames if (frame_no != -1)//already exists no page fault continue; //look at ref_str[cur+1:cur+limit] to see which one has not been referred to the longest in the future page_faults++; } return page_faults; } int main(int argc, char** argv) { int e, m, P; double t; srand(time(NULL)); while (*++argv) { //./main -P 1048576 -e 10 -m 20 -t 1000 (Scenario #1) if (**argv != '-') return 1; switch ((*argv)[1]) { case 'P': P = atoi(*++argv); break; case 'e': e = atoi(*++argv); break; case 'm': m = atoi(*++argv); break; case 't': t = 1.0 / atoi(*++argv); break; default: //error return 1; } } //scenario 1 P = 1048576, e = 10, m = 20, t = 0.001; //scenario 2 //P = 4194304, e = 10, m = 20, t = 0.002; //scenario 3 //P = 1048576, e = 15, m = 20, t = 0.001; //scenario 4 //P = 4194304, e = 8, m = 50, t = 0.001; //scenario 5 //P = 262144, e = 15, m = 7, t = 0.002; printf("CLAs are P = %d, locus size = %d, m = %d, transition prob. = %f ", P, e, m, t); //initially locus: 0...e-1 int locus_position = 0;//s int ref_count = 0; int* ref_str = (int*)malloc(1000000 * sizeof(int)); while (ref_count <= 1000000) { int next_ref = rand() % e + locus_position; if (!ref_count || next_ref != ref_str[ref_count - 1]) ref_str[ref_count++] = next_ref; else//try again continue; if (ref_count % m == 0) {//let's displace the locus if (rand() < t * RAND_MAX)//jump locus_position = lrand() % (P - e + 1); else locus_position = (locus_position + 1) % (P - e + 1); } } int fifo_page_fault = fifo(ref_str, FRAME_NO, e * m); printf("FIFO page replacement causes %d page faults ", fifo_page_fault); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Brain part of logical and dicision making......?

Answered: 1 week ago

Question

The supporting and nurturing cells found in brains are........?

Answered: 1 week ago

Question

The outer covering of the brain is covered with..........?

Answered: 1 week ago

Question

The brain system is composed of...........?

Answered: 1 week ago