0% found this document useful (0 votes)
5 views2 pages

FIFO1 C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

FIFO1 C

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <conio.h>

void main() {
int pages[30], frames[10], n, frame_count, i, j, k;
int page_faults = 0, current = 0, flag, is_page_in_frame,page_hits;

clrscr();

printf("Enter the number of pages: ");


scanf("%d", &n);

printf("Enter the page reference string:\n");


for (i = 0; i < n; i++) {
printf("Page %d: ", i + 1);
scanf("%d", &pages[i]);
}

printf("Enter the number of frames: ");


scanf("%d", &frame_count);

// Initialize all frames to -1 (indicating empty frames)


for (i = 0; i < frame_count; i++) {
frames[i] = -1;
}

printf("\nPage Reference String\tFrame Contents\n");

for (i = 0; i < n; i++) {


flag = 0;
is_page_in_frame = 0;

// Check if the page is already in the frame


for (j = 0; j < frame_count; j++) {
if (frames[j] == pages[i]) {
is_page_in_frame = 1; // Page hit
break;
}
}

if (!is_page_in_frame) { // Page fault


frames[current] = pages[i]; // Replace the page in FIFO manner
current = (current + 1) % frame_count; // Update the FIFO pointer
page_faults++;
}

// Print the current page reference and frame contents


printf("%d\t\t\t", pages[i]);
for (k = 0; k < frame_count; k++) {
if (frames[k] != -1)
printf("%d ", frames[k]);
else
printf("- ");
}
printf("\n");
}

page_hits=(n-page_faults);
printf("\nTotal Page Faults: %d\n", page_faults);
printf("Page Fault Rate: %.2f%%\n", ((float)page_faults / n) * 100);
printf("\nTotal Page Faults: %d\n", page_hits);
printf("Page Fault Rate: %.2f%%\n", ((float)page_hits / n) * 100);

getch();
}

You might also like