The following is to be written in C. Compare the performance of the following pa
ID: 3858024 • Letter: T
Question
The following is to be written in C. Compare the performance of the following page replacement algoirthm: FIFO. You will be provided with a .dat file containing the virtual addresses that are being referenced by a single program (a process). Run your program with the following parameters:
Page Size: 512, 1024, 2048 (words)
Number of frames allocated to the process: 4, 8, 12 (So you will have 9 runs, with each page size and number of frames combination.
Each run contains statistics that should be printed in the following format)
Page Size # of Pages Page Replacement Algorithm. Page Fault Percentage.
Explanation / Answer
#include<stdio.h>
typedef struct frame {
int page_num;
int time;
} frame;
frame *frames = NULL;
int num_of_frames = 0;
void init() {
int i;
if (frames!=NULL)
free(frames);
frames = malloc(sizeof(frame) * num_of_frames);
for (i = 0; i < num_of_frames; i++)
frames[i].page_num = frames[i].time = -1;
}
int page_search(int page_num) {
int i;
for (i = 0; i < num_of_frames; i++)
if (page_num == frames[i].page_num)
return i;
return -1;
}
int get_victim_frame() {
int i, victim = 0;
for (i = 0; i < num_of_frames; i++)
if (frames[i].time < frames[victim].time)
victim = i;
return victim;
}
int main() {
int i,j, page_num,time,hit, miss, victim, address;
double fault_rate;
int page_size[] = {512,1024,2048};
int frame_count[] = {4,8,12};
FILE *fp = fopen("a.dat","r");
for (i = 0; i<3; i++)
for (j = 0; j<3; j++) {
num_of_frames = frame_count[j];
init();
hit=miss=time = 0;
while(!feof(fp)) {
fscanf(fp,"%d",&address);
page_num = address/page_size[i];
time ++;
if (page_search(page_num) != -1)
hit ++;
else {
miss ++;
victim = get_victim_frame();
frames[victim].page_num = page_num;
frames[victim].time = time;
}
}
fault_rate = (double)miss*100/(hit+miss);
printf("%d %d FIFO %lf ",page_size[i], frame_count[j],fault_rate);
fseek( fp, 0, SEEK_SET );
}
fclose(fp);
}
Hope this satisfy you!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.