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

OS Lab 9 22BLC1230

The document outlines an experiment for implementing page replacement algorithms (FIFO, LRU, and Optimal) in C programming. It includes code to track page faults based on user-defined page reference strings and frame sizes. The program displays the number of page faults and the sequence of occurrences for each algorithm.

Uploaded by

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

OS Lab 9 22BLC1230

The document outlines an experiment for implementing page replacement algorithms (FIFO, LRU, and Optimal) in C programming. It includes code to track page faults based on user-defined page reference strings and frame sizes. The program displays the number of page faults and the sequence of occurrences for each algorithm.

Uploaded by

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

Experiment No: 9​ ​ ​ ​ ​ ​ ​ ​ ​ Date:

25/03/25

Page Replacement Algorithms


BCSE303P - OPERATING SYSTEMS LAB

Name: Karthik Krishna B ​​

RegNo: 22BLC1230

Faculty: Dr. M.Revathi

____________________________________________________________________________
________

Device a c program to implement LRU, FIFO, optimal replacement algorithms. All frames are
initially empty. DIsplay the number of page faults by getting the page reference string and the
number of frames from the user. Display the sequence of occurrence of page faults.

Reference string : 3,1,4,2,5,4,1,3,5,2,0,1,1,0,2,3,4,5,0,1

Number of frames : 3,4

Code

#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>

#define MAX_FRAMES 20
#define MAX_PAGES 50

// Structure to track page fault details


typedef struct {
int page;
int index;
int* currentFrames;
int numFrames;
} PageFaultInfo;

// Function to check if a page exists in memory


bool isPageInMemory(int page, int* frames, int numFrames) {
for (int i = 0; i < numFrames; i++) {
if (frames[i] == page) {
return true;
}
}
return false;
}

// FIFO (First-In-First-Out) Page Replacement


int fifoPageReplacement(int* pages, int numPages, int numFrames, PageFaultInfo*
faultSequence, int* faultCount) {
int frames[MAX_FRAMES];
int pageFaults = 0;
int nextReplaceIndex = 0;

// Initialize frames to -1 (empty)


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

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


for (int i = 0; i < numPages; i++) {
if (!isPageInMemory(pages[i], frames, numFrames)) {
// Page fault occurred
pageFaults++;

// Create a copy of current frames for this page fault


int* currentFramesCopy = malloc(numFrames * sizeof(int));
for (int j = 0; j < numFrames; j++) {
currentFramesCopy[j] = frames[j];
}

// Store page fault information


faultSequence[*faultCount] = (PageFaultInfo){
.page = pages[i],
.index = i,
.currentFrames = currentFramesCopy,
.numFrames = numFrames
};
(*faultCount)++;

// Replace the oldest page


frames[nextReplaceIndex] = pages[i];
nextReplaceIndex = (nextReplaceIndex + 1) % numFrames;

// Print current state


printf("Page fault: Page %d | Frames: ", pages[i]);
for (int j = 0; j < numFrames; j++) {
printf("%d ", frames[j]);
}
printf("\n");
}
}

// Display page fault statistics


printf("\nFIFO Page Fault Statistics:");
printf("\nTotal Page Faults: %d", pageFaults);
//printf("\nPage Fault Rate: %.2f%%\n", (float)pageFaults / numPages * 100);

return pageFaults;
}

// LRU (Least Recently Used) Page Replacement


int lruPageReplacement(int* pages, int numPages, int numFrames, PageFaultInfo*
faultSequence, int* faultCount) {
int frames[MAX_FRAMES];
int lastUsed[MAX_FRAMES];
int pageFaults = 0;

// Initialize frames and lastUsed to -1


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

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


for (int i = 0; i < numPages; i++) {
if (!isPageInMemory(pages[i], frames, numFrames)) {
// Page fault occurred
pageFaults++;

// Create a copy of current frames for this page fault


int* currentFramesCopy = malloc(numFrames * sizeof(int));
for (int j = 0; j < numFrames; j++) {
currentFramesCopy[j] = frames[j];
}

// Find the least recently used page


int replaceIndex = 0;
int oldestTime = lastUsed[0];
for (int j = 1; j < numFrames; j++) {
if (lastUsed[j] < oldestTime) {
oldestTime = lastUsed[j];
replaceIndex = j;
}
}
// Store page fault information
faultSequence[*faultCount] = (PageFaultInfo){
.page = pages[i],
.index = i,
.currentFrames = currentFramesCopy,
.numFrames = numFrames
};
(*faultCount)++;

// Replace the least recently used page


frames[replaceIndex] = pages[i];
lastUsed[replaceIndex] = i;

// Print current state


printf("Page fault: Page %d | Frames: ", pages[i]);
for (int j = 0; j < numFrames; j++) {
printf("%d ", frames[j]);
}
printf("\n");
} else {
// Update last used time for the page
for (int j = 0; j < numFrames; j++) {
if (frames[j] == pages[i]) {
lastUsed[j] = i;
break;
}
}
}
}

// Display page fault statistics


printf("\nLRU Page Fault Statistics:");
printf("\nTotal Page Faults: %d", pageFaults);
//printf("\nPage Fault Rate: %.2f%%\n", (float)pageFaults / numPages * 100);

return pageFaults;
}

// Optimal Page Replacement


int optimalPageReplacement(int* pages, int numPages, int numFrames, PageFaultInfo*
faultSequence, int* faultCount) {
int frames[MAX_FRAMES];
int pageFaults = 0;

// Initialize frames to -1 (empty)


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

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


for (int i = 0; i < numPages; i++) {
if (!isPageInMemory(pages[i], frames, numFrames)) {
// Page fault occurred
pageFaults++;

// Create a copy of current frames for this page fault


int* currentFramesCopy = malloc(numFrames * sizeof(int));
for (int j = 0; j < numFrames; j++) {
currentFramesCopy[j] = frames[j];
}

// If frames are not full, add the page


bool frameFound = false;
for (int j = 0; j < numFrames; j++) {
if (frames[j] == -1) {
frames[j] = pages[i];
frameFound = true;
break;
}
}

// If frames are full, find the page to replace


if (!frameFound) {
int replaceIndex = -1;
int maxDistance = -1;

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


int distance = INT_MAX;

// Find the next occurrence of this page


for (int k = i + 1; k < numPages; k++) {
if (frames[j] == pages[k]) {
distance = k - i;
break;
}
}

// Replace the page that won't be used for the longest time
if (distance == INT_MAX) {
replaceIndex = j;
break;
}

if (distance > maxDistance) {


maxDistance = distance;
replaceIndex = j;
}
}

frames[replaceIndex] = pages[i];
}
// Store page fault information
faultSequence[*faultCount] = (PageFaultInfo){
.page = pages[i],
.index = i,
.currentFrames = currentFramesCopy,
.numFrames = numFrames
};
(*faultCount)++;

// Print current state


printf("Page fault: Page %d | Frames: ", pages[i]);
for (int j = 0; j < numFrames; j++) {
printf("%d ", frames[j]);
}
printf("\n");
}
}

// Display page fault statistics


printf("\nOptimal Page Replacement Statistics:");
printf("\nTotal Page Faults: %d", pageFaults);
//printf("\nPage Fault Rate: %.2f%%\n", (float)pageFaults / numPages * 100);

return pageFaults;
}

// Function to display page fault sequence


void displayPageFaultSequence(PageFaultInfo* faultSequence, int faultCount) {
printf("\nPage Fault Sequence:\n");
printf("Index | Page | Memory Frames\n");
printf("----------------------------\n");

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


printf("%5d | %4d | ", faultSequence[i].index, faultSequence[i].page);

// Display current memory frames


for (int j = 0; j < faultSequence[i].numFrames; j++) {
if (faultSequence[i].currentFrames[j] != -1) {
printf("%d ", faultSequence[i].currentFrames[j]);
} else {
printf("- ");
}
}
printf("\n");

// Free dynamically allocated memory


free(faultSequence[i].currentFrames);
}
}

// Function to get page reference string from user


void getUserPages(int* pages, int* numPages) {
printf("Enter the number of pages (max %d): ", MAX_PAGES);
scanf("%d", numPages);

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


for (int i = 0; i < *numPages; i++) {
scanf("%d", &pages[i]);
}
}

// Main menu function


void displayMenu() {
printf("\n--- Page Replacement Algorithms ---\n");
printf("1. FIFO (First-In-First-Out)\n");
printf("2. LRU (Least Recently Used)\n");
printf("3. Optimal Page Replacement\n");
printf("4. Exit\n");
printf("Enter your choice: ");
}

int main() {
int pages[MAX_PAGES];
int numPages = 0;
int numFrames = 0;
int choice;

// Allocate space for page fault sequence


PageFaultInfo faultSequence[MAX_PAGES];
int faultCount = 0;

// Get number of frames


printf("Enter the number of frames (max %d): ", MAX_FRAMES);
scanf("%d", &numFrames);

// Get page reference string


getUserPages(pages, &numPages);

while (1) {
// Reset fault count
faultCount = 0;

// Display menu and get user choice


displayMenu();
scanf("%d", &choice);

// Process user choice


switch (choice) {
case 1:
printf("\nTotal Pages: %d\n", numPages);
printf("Number of Frames: %d\n", numFrames);
fifoPageReplacement(pages, numPages, numFrames, faultSequence, &faultCount);
displayPageFaultSequence(faultSequence, faultCount);
break;
case 2:
printf("\nTotal Pages: %d\n", numPages);
printf("Number of Frames: %d\n", numFrames);
lruPageReplacement(pages, numPages, numFrames, faultSequence, &faultCount);
displayPageFaultSequence(faultSequence, faultCount);
break;
case 3:
printf("\nTotal Pages: %d\n", numPages);
printf("Number of Frames: %d\n", numFrames);
optimalPageReplacement(pages, numPages, numFrames, faultSequence,
&faultCount);
displayPageFaultSequence(faultSequence, faultCount);
break;
case 4:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

Output:
Frame Size = 3

FIFO
LRU
Optimal
Frame size = 4

FIFO
LRU

Optimal

You might also like