#include #define MAX 50 int stack[MAX], top = -1; // check if page is in stack int inStack(int page, int frames) { for (int i = top; i > top - frames && i >= 0; i--) if (stack[i] == page) return i; return -1; } int main() { int pages[MAX], n, frames; printf("Enter number of pages: "); scanf("%d", &n); printf("Enter reference string: "); for (int i = 0; i < n; i++) scanf("%d", &pages[i]); printf("Enter number of frames: "); scanf("%d", &frames); int faults = 0; printf("\nPage\tFrames\t\t\tFault\n"); for (int i = 0; i < n; i++) { int pos = inStack(pages[i], frames); printf("%d\t", pages[i]); if (pos == -1) { // page not found — fault stack[++top] = pages[i]; faults++; // print current frames (top 'frames' elements) int start = top - frames + 1; if (start < 0) start = 0; for (int j = start; j <= top; j++) printf("%d ", stack[j]); printf("\t\tFault"); } else { // page found — move it to top (most recently used) for (int j = pos; j < top; j++) stack[j] = stack[j + 1]; stack[top] = pages[i]; // print current frames int start = top - frames + 1; if (start < 0) start = 0; for (int j = start; j <= top; j++) printf("%d ", stack[j]); } printf("\n"); } printf("\nTotal Page Faults: %d\n", faults); return 0; }