#include <stdio.
h>
#define MAX_FRAMES 3
int main() {
int frames[MAX_FRAMES]; // Array to represent memory frames
int pageReferences[20]; // Array to store page references
int numPages, pageFaults = 0;
int framePointer = 0; // Pointer to the current frame
// input the number of pages
printf("Enter the number of pages: ");
scanf("%d", &numPages);
// input the page reference string
printf("Enter the page reference string: ");
for (int i = 0; i < numPages; i++) {
scanf("%d", &pageReferences[i]);
}
// Initialize frames to -1, indicating they are initially empty
for (int i = 0; i < MAX_FRAMES; i++) {
frames[i] = -1;
// Loop through the page references
for (int i = 0; i < numPages; i++) {
int currentPage = pageReferences[i];
int pageFound = 0;
// Check if the current page is already in the frames
for (int j = 0; j < MAX_FRAMES; j++)
if (frames[j] == currentPage)
pageFound = 1;
break;
}
if (pageFound == 0) {
// If the current page is not in the frames, it's a page fault
printf("Page %d caused a page fault.\n", currentPage);
pageFaults++;
// Replace the oldest page in the frame with the current page
frames[framePointer] = currentPage;
// Move the frame pointer to the next position in a circular
manner
framePointer = (framePointer + 1) % MAX_FRAMES;
printf("Frames: ");
for (int j = 0; j < MAX_FRAMES; j++) {
if (frames[j] == -1) {
printf("- ");
} else {
printf("%d ", frames[j]);
}
}
printf("\n");
printf("Total page faults: %d\n", pageFaults);
return 0;