Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

/*

Name: Christopher McMichael


Date: 3/28/2023
Title: Lab4 fifo
Description: This program uses a queue to imitate a FIFO memory buffer.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "queue.h"
#include "node.h"

int main(int argc, char *argv[]){


int C_SIZE = atoi(argv[1]); // Size of Cache passed by user
char pageCache[100]; // Cache that holds the input from test file

struct queue *q; // Main page queue


q = queue_create();

int totalFaults = 0; // keeps track of the total page faults


int totalHits = 0; // keeps track of the total page hits
int totalPages = 0; // keeps track of the total number of pages requests

while (fgets(pageCache, 100, stdin)){


int page_num = atoi(pageCache); //Stores number read from file as int
totalPages++;

if (queue_find(q, page_num) != NULL) { // Checks to see if the current page is in the


queue already
fprintf(stderr, "Hit: %d\n", page_num);
totalHits++;
}
else { // Page is not in the queue
printf("Miss: %d\n", page_num);
totalFaults++;
if (queue_length(q) >= C_SIZE) { // Checks to see if the queue is already at max
capacity or not
dequeue(q);
enqueue(q, page_num);
fprintf(stderr, "Old page removed, %d added\n", page_num);
}
else {
enqueue(q, page_num);
fprintf(stderr, "%d added\n", page_num);
}
}
}

printf("%d Total Page Faults\n", totalFaults);


printf("%d Total Page Hits\n", totalHits);
printf("%d Total Pages\n", totalPages);
printf("Cache Size: %d\n", C_SIZE);
printf("Hit Rate: %.2f\n", ((double) totalHits / (double) totalPages));
return 0;
}

You might also like