#include <stdio.h> #include <stdlib.h> #define MAX_FRAMES 100 int isPageInFrame(int frames[], int n, int page) { for (int i = 0; i < n; i++) { if (frames[i] == page) { return 1; } } return 0; } void printFrames(int frames[], int n) { for (int i = 0; i < n; i++) { if (frames[i] == -1) { printf("-"); } else { printf("%d", frames[i]); } if (i < n - 1) { printf(" "); } } printf("\n"); } int main() { int n, numFrames; int pageFaults = 0; printf("Enter the number of frames: "); scanf("%d", &numFrames); int frames[MAX_FRAMES]; for (int i = 0; i < numFrames; i++) { frames[i] = -1; } printf("Enter the number of page requests: "); scanf("%d", &n); int pageRequests[n]; printf("Enter the page requests: "); for (int i = 0; i < n; i++) { scanf("%d", &pageRequests[i]); } int currentFrame = 0; for (int i = 0; i < n; i++) { int page = pageRequests[i]; if (!isPageInFrame(frames, numFrames, page)) { pageFaults++; frames[currentFrame] = page; currentFrame = (currentFrame + 1) % numFrames; } printFrames(frames, numFrames); } printf("Total page faults: %d\n", pageFaults); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter