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

OS Exp 9

The document contains code for implementing three page replacement algorithms - FIFO, LRU and OPR. It defines functions to implement the algorithms and finds the number of page faults for each. It takes input for the number of pages and frames and page reference string.

Uploaded by

Hardik kadam
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)
16 views

OS Exp 9

The document contains code for implementing three page replacement algorithms - FIFO, LRU and OPR. It defines functions to implement the algorithms and finds the number of page faults for each. It takes input for the number of pages and frames and page reference string.

Uploaded by

Hardik kadam
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/ 4

Experiment no.

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

// Function to find index of the page which will be replaced


int findReplacementIndex(int pageArray[], int frameArray[], int pageRef[], int n, int currentIndex)
{
int index = -1, farthest = INT_MIN;
for (int i = 0; i < n; i++)
{
int j;
for (j = currentIndex; j < n; j++)
{
if (frameArray[i] == pageRef[j])
{
if (j > farthest)
{
farthest = j;
index = i;
}
break;
}
}
if (j == n)
return i;
}
if (index == -1)
return 0;
return index;
}

// FIFO Page Replacement Algorithm


void fifo(int pageRef[], int n, int frames)
{
int frameArray[frames];
int pageFaults = 0;
int index = 0;
for (int i = 0; i < frames; i++)
frameArray[i] = -1; // Initialize frameArray to -1 indicating empty frames
for (int i = 0; i < n; i++)
{
int j, page = pageRef[i];
int found = 0;
for (j = 0; j < frames; j++)
{
if (frameArray[j] == page)
{
found = 1;
break;
}
}
if (!found)
{
frameArray[index] = page;
index = (index + 1) % frames;
pageFaults++;
}
}
printf("Page Faults (FIFO): %d\n", pageFaults);
}

// LRU Page Replacement Algorithm


void lru(int pageRef[], int n, int frames)
{
int frameArray[frames];
int pageFaults = 0;
for (int i = 0; i < frames; i++)
frameArray[i] = -1; // Initialize frameArray to -1 indicating empty frames
for (int i = 0; i < n; i++)
{
int j, page = pageRef[i];
int found = 0;
for (j = 0; j < frames; j++)
{
if (frameArray[j] == page)
{
found = 1;
break;
}
}
if (!found)
{
int replaceIndex = findReplacementIndex(pageRef, frameArray, pageRef, n, i + 1);
frameArray[replaceIndex] = page;
pageFaults++;
}
}
printf("Page Faults (LRU): %d\n", pageFaults);
}

// OPR Page Replacement Algorithm


void opr(int pageRef[], int n, int frames)
{
int frameArray[frames];
int pageFaults = 0;
for (int i = 0; i < frames; i++)
frameArray[i] = -1; // Initialize frameArray to -1 indicating empty frames
for (int i = 0; i < n; i++)
{
int j, page = pageRef[i];
int found = 0;
for (j = 0; j < frames; j++)
{
if (frameArray[j] == page)
{
found = 1;
break;
}
}
if (!found)
{
int replaceIndex = findReplacementIndex(pageRef, frameArray, pageRef, n, i + 1);
frameArray[replaceIndex] = page;
pageFaults++;
}
}
printf("Page Faults (OPR): %d\n", pageFaults);
}

int main()
{
int n, frames;

printf("Enter number of pages: ");


scanf("%d", &n);

int pageRef[n];
printf("Enter the page reference sequence: ");
for (int i = 0; i < n; i++)
scanf("%d", &pageRef[i]);

printf("Enter number of frames: ");


scanf("%d", &frames);

fifo(pageRef, n, frames);
lru(pageRef, n, frames);
opr(pageRef, n, frames);

return 0;
}

You might also like