Program:
#include<stdio.h> #include<limits.h> // This function is just use to check if the frame is containg our current page already. int checkHit(int incomingPage, int queue[], int occupied) { for(int i = 0; i < occupied; i++) { if(incomingPage == queue[i]) { return 1; } } return 0; } // This function is used to print thr frames. void printFrames(int queue[], int occupied) { for(int i = 0; i < occupied; i++) { printf("%d\t\t", queue[i]); } } int main() { int incomingStream[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,0,1,7,0,1}; int n = sizeof(incomingStream) / sizeof(incomingStream[0]); int frames = 4; int queue[n]; int distance[n]; int occupied = 0; int pageFaults = 0; int hit = 0; int first_in_index = 0; printf("Page:\tPage 1\t\tPage 2\t\tPage 3\t\tPage 4\n"); for(int i = 0; i < n; i++) { printf("%d:\t", incomingStream[i]); // Condition 1: When the page is already present in the frame, so dont do anything, just print the frame as it is. if(checkHit(incomingStream[i], queue, occupied)) { hit++; printFrames(queue, occupied); } // Condition 2: When the frame is not completely filled, so fill it directly and increment the pageFaults. else if(occupied < frames) { queue[occupied] = incomingStream[i]; occupied++; pageFaults++; printFrames(queue, occupied); } // Condition 3: When the frame is full, and our page is not present in the frame else { queue[first_in_index] = incomingStream[i]; // Update the first in index first_in_index = (first_in_index+1) % frames; printFrames(queue, occupied); pageFaults++; } printf("\n"); } printf("\n\n Page Faults: %d", pageFaults); printf("\n\n Number of Hits: %d", hit); return 0; }
Output: