Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
1.5 kB
4
Indexable
Never
#include <stdio.h>
#define FRAME_SIZE 3
int isPageInFrames(int frames[], int frameCount, int page)
 {
    for (int i = 0; i < frameCount; i++) 
	{
        if (frames[i] == page) 
		{
            return 1; 
        }
    }
    return 0;
}
void printFrames(int frames[], int frameCount)
 {
    printf("Frames: ");
    for (int i = 0; i < frameCount; i++) 
	{
        if (frames[i] != -1) 
		{
            printf("%d ", frames[i]);
        }
    }
    printf("\n");
}
int main() {
    int frames[FRAME_SIZE];
    int pageReferenceString[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
    int pageReferenceSize = sizeof(pageReferenceString) / sizeof(pageReferenceString[0]);
    int pageFaults = 0, pageHits = 0, front = 0, rear = 0;
    for (int i = 0; i < FRAME_SIZE; i++)
	 {
        frames[i] = -1;
    }
    for (int i = 0; i < pageReferenceSize; i++)
	 {
        int currentPage = pageReferenceString[i];
        if (isPageInFrames(frames, FRAME_SIZE, currentPage))
		 {
            pageHits++;
            printf("Page %d hit\n", currentPage);
        } else {
            pageFaults++;
            printf("Page %d fault\n", currentPage);
            frames[front] = currentPage;
            front = (front + 1) % FRAME_SIZE;
        }
        printFrames(frames, FRAME_SIZE);
    }

    printf("Total page faults: %d\n", pageFaults);
    printf("Total page hits: %d\n", pageHits);

    return 0;
}
Leave a Comment