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

FIFO Page Replacement

This C program simulates a First-In First-Out (FIFO) page replacement algorithm. It takes a sample reference string as input and checks each page access against a fixed number of frames. If the page is not already in frames, it causes a page fault. The oldest page in frames is replaced with the new page. The program outputs the reference string, the frames after each access, and the total number of page faults.

Uploaded by

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

FIFO Page Replacement

This C program simulates a First-In First-Out (FIFO) page replacement algorithm. It takes a sample reference string as input and checks each page access against a fixed number of frames. If the page is not already in frames, it causes a page fault. The oldest page in frames is replaced with the new page. The program outputs the reference string, the frames after each access, and the total number of page faults.

Uploaded by

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

#include <stdio.

h>

#define MAX_FRAMES 3 // Number of frames in memory

// Function to check if a page exists in frames


int isPagePresent(int frames[], int frameSize, int page) {
for (int i = 0; i < frameSize; i++) {
if (frames[i] == page) {
return 1; // Page found in frames
}
}
return 0; // Page not found in frames
}

int main() {
int referenceString[] = {7,0,1,2,0,3,0,4,2,3,0,3,1,2,0}; // Example reference string
int numReferences = sizeof(referenceString) / sizeof(referenceString[0]);

int frames[MAX_FRAMES]; // Array to hold frames in memory


int framePointer = 0; // Points to the oldest frame in FIFO

int pageFaults = 0; // Counter for page faults

// Initialize frames array with -1 (indicating empty frame)


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

printf("Page Reference String: ");


for (int i = 0; i < numReferences; i++) {
printf("%d ", referenceString[i]);
}
printf("\n\n");

printf("FIFO Page Replacement:\n");

for (int i = 0; i < numReferences; i++) {


int currentPage = referenceString[i];

// Check if the page is already in frames


if (!isPagePresent(frames, MAX_FRAMES, currentPage)) {
printf("Page %d caused a Page Fault.\n", currentPage);
frames[framePointer] = currentPage; // Replace the oldest page
framePointer = (framePointer + 1) % MAX_FRAMES; // Update framePoin
pageFaults++;
}

printf("Current frames: ");


for (int j = 0; j < MAX_FRAMES; j++) {
if (frames[j] != -1) {
printf("%d ", frames[j]);
} else {
printf("X "); // Empty frame
}
}
printf("\n");
}

printf("\nTotal Page Faults: %d\n", pageFaults);

return 0;
}

You might also like