0% found this document useful (0 votes)
29 views

Fifo Code

This C program simulates a CPU's memory management using page replacement. It defines an array to represent memory frames, reads in page references, initializes frame contents to -1, then loops through references checking for page faults. On a fault, it prints the fault, replaces the oldest frame, increments the pointer, and prints the updated frames. It concludes by outputting the total page faults.
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)
29 views

Fifo Code

This C program simulates a CPU's memory management using page replacement. It defines an array to represent memory frames, reads in page references, initializes frame contents to -1, then loops through references checking for page faults. On a fault, it prints the fault, replaces the oldest frame, increments the pointer, and prints the updated frames. It concludes by outputting the total page faults.
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/ 4

#include <stdio.

h>

#define MAX_FRAMES 3

int main() {

int frames[MAX_FRAMES]; // Array to represent memory frames

int pageReferences[20]; // Array to store page references

int numPages, pageFaults = 0;

int framePointer = 0; // Pointer to the current frame

// input the number of pages

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

scanf("%d", &numPages);

// input the page reference string

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

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

scanf("%d", &pageReferences[i]);

}
// Initialize frames to -1, indicating they are initially empty

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

frames[i] = -1;

// Loop through the page references

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

int currentPage = pageReferences[i];

int pageFound = 0;

// Check if the current page is already in the frames

for (int j = 0; j < MAX_FRAMES; j++)

if (frames[j] == currentPage)

pageFound = 1;

break;

}
if (pageFound == 0) {

// If the current page is not in the frames, it's a page fault

printf("Page %d caused a page fault.\n", currentPage);

pageFaults++;

// Replace the oldest page in the frame with the current page

frames[framePointer] = currentPage;

// Move the frame pointer to the next position in a circular


manner

framePointer = (framePointer + 1) % MAX_FRAMES;

printf("Frames: ");

for (int j = 0; j < MAX_FRAMES; j++) {

if (frames[j] == -1) {

printf("- ");

} else {

printf("%d ", frames[j]);

}
}

printf("\n");

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

return 0;

You might also like