#include <stdio.h>
int main() {
int referenceString[10];
int pageFaults = 0;
int i, j, flag, pages, frames;
// Input the number of pages and the page reference string
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for(i = 0; i < pages; i++) {
printf("Value No. [%d]:\t", i + 1);
scanf("%d", &referenceString[i]);
}
// Input the number of frames
printf("\nWhat are the total number of frames:\t");
scanf("%d", &frames);
int temp[frames];
// Initialize frames with -1 (indicating empty slots)
for(i = 0; i < frames; i++) {
temp[i] = -1;
}
for(i = 0; i < pages; i++) {
flag = 0;
// Check if the page is already in the frames
for(j = 0; j < frames; j++) {
if(referenceString[i] == temp[j]) {
flag++;
pageFaults--;
}
}
pageFaults++;
// If page is not in frames and we have not filled all frames yet
if((pageFaults <= frames) && (flag == 0)) {
temp[i] = referenceString[i];
}
// If page is not in frames and all frames are filled
else if(flag == 0) {
temp[(pageFaults - 1) % frames] = referenceString[i];
}
// Print current state of frames
printf("\n");
for(j = 0; j < frames; j++) {
printf("%d\t", temp[j]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
FIFO Page Replacement Algorithm C
Code
Fell free to copy
some code