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

OSexpadvay

Uploaded by

advay.234486101
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)
3 views

OSexpadvay

Uploaded by

advay.234486101
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/ 10

EXPERIMENT ASSESSMENT

ACADEMIC YEAR 2024-25

Course: Operating System Lab


Course code: CSL403
Year: SE Sem: IV
Experiment No.: 9

Aim: - Program to demonstrate the concept of page replacement policies for


handlingpage faults eg: FIFO, LRU etc.

Name:advay joshi

Roll Number: 04

Date of Performance:26/03/25

Date of Submission:02/04/25

Evaluation
Performance Indicator Max. Marks Marks Obtained

Performance 5

Understanding 5

Journal work and timely submission. 10

Total 20

Performance Indicator Exceed Meet Expectations Below


Expectations (EE) (ME) Expectations
(BE)

Performance 5 3 2

Understanding 5 3 2

Journal work and 10 8 4


timely submission.

Checked by

Name of Faculty : Odilia Gonsalves

Signature :

Date :

CSL403- Operating System Lab

Experiment No. 9

Aim Write a program in C demonstrate the concept of page replacement policies for handling
page faults eg: FIFO, LRU etc.
Objective Page replacement algorithms are an important part of virtual memory management and
it helps the OS to decide which memory page can be moved out, making space for
the currently needed page. However, the ultimate objective of all page replacement
algorithms is to reduce the number of page faults.
Theory Virtual Memory

Virtual memory is a feature of an operating system that enables a computer to


be able to compensate for shortages of physical memory by transferring pages
of data from random access memory to disk storage.
Page Replacement Algorithms.
There are three types of page replacement algorithms namely FIFO, LRU and
optimal
● FIFO(First in First Out):- In this algorithm the block that entered in
the page first will be replaced during a fault or a miss
● LRU(Least Recently Used):-In this algorithm the block that was used
least recently before a page fault will be replaced
● Optimal:- In this algorithm while swapping a page the algorithm will
check the blocks that will enter in the future and swap the current
requested block with the page that will be used last.
Program FIFO:

#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);

CSL403- Operating System Lab

avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

LRU:

#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
CSL403- Operating System Lab

{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

CSL403- Operating System Lab

Optimal:

#include <stdio.h>
// This function checks if current strea item(key) exists in any of the frames or not
int search(int key, int frame_items[], int frame_occupied)
{
for (int i = 0; i < frame_occupied; i++)
if (frame_items[i] == key)
return 1;
return 0;
}
void printOuterStructure(int max_frames){
printf("Stream ");

for(int i = 0; i < max_frames; i++)


printf("Frame%d ", i+1);
}
void printCurrFrames(int item, int frame_items[], int frame_occupied, int
max_frames){
// print current reference stream item
printf("\n%d \t\t", item);

// print frame occupants one by one


for(int i = 0; i < max_frames; i++){
if(i < frame_occupied)
printf("%d \t\t", frame_items[i]);
else
printf("- \t\t");
}
}
// This Function helps in finding frame that will not be used
// for the longest period of time in future in ref_str[0 ... refStrLen - 1]
int predict(int ref_str[], int frame_items[], int refStrLen, int index, int
frame_occupied)
{
// For each current occupant in frame item
// we try to find the frame item that will not be referenced in
// for the longest in future in the upcoming reference string
int result = -1, farthest = index;
for (int i = 0; i < frame_occupied; i++) {
int j;
for (j = index; j < refStrLen; j++)
{
if (frame_items[i] == ref_str[j])
{
if (j > farthest) {

CSL403- Operating System Lab

farthest = j;
result = i;
}
break;
}
}

// If we find a page that is never referenced in future,


// return it immediately as its the best
if (j == refStrLen)
return i;
}

// If none of the frame items appear in reference string


// in the future then we return 0th index. Otherwise we return result
return (result == -1) ? 0 : result;
}
void optimalPage(int ref_str[], int refStrLen, int frame_items[], int max_frames)
{
// initially none of the frames are occupied
int frame_occupied = 0;
printOuterStructure(max_frames);

// Here we traverse through reference string


// and check for miss and hit.
int hits = 0;
for (int i = 0; i < refStrLen; i++) {

// If found already in the frame items : HIT


if (search(ref_str[i], frame_items, frame_occupied)) {
hits++;
printCurrFrames(ref_str[i], frame_items, frame_occupied, max_frames);
continue;
}

// If not found in frame items : MISS

// If frames are empty then current reference string item in frame


if (frame_occupied < max_frames){
frame_items[frame_occupied] = ref_str[i];
frame_occupied++;
printCurrFrames(ref_str[i], frame_items, frame_occupied, max_frames); }
// else we need to use optmial algorithm to find
// frame index where we need to do replacement for this
// incoming reference string item

CSL403- Operating System Lab

else {
int pos = predict(ref_str, frame_items, refStrLen, i + 1, frame_occupied);
frame_items[pos] = ref_str[i];
printCurrFrames(ref_str[i], frame_items, frame_occupied, max_frames); }

}
printf("\n\nHits: %d\n", hits);
printf("Misses: %d", refStrLen - hits);
}

// Driver Function
int main()
{
// int ref_str[] = {9, 0, 5, 1, 0, 3, 0, 4, 1, 3, 0, 3, 1, 3};
int ref_str[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,3};
int refStrLen = sizeof(ref_str) / sizeof(ref_str[0]);
int max_frames = 4;
int frame_items[max_frames];

optimalPage(ref_str, refStrLen, frame_items, max_frames);


return 0;
}

CSL403- Operating System Lab

Output FIFO
LRU

CSL403- Operating System Lab

Optimal
Conclusion
Page replacement policies are essential for managing memory efficiently when a page fault
occurs. FIFO (First-In-First-Out) replaces the oldest page in memory, which can sometimes
lead to Belady’s anomaly (increased page faults with more frames). LRU (Least Recently
Used) replaces the page that has not been used for the longest time, reducing page faults in
many cases. Optimal Page Replacement replaces the page that will not be used for the longest
future period, but it is impractical as it requires future knowledge. Other policies like LFU
(Least Frequently Used) consider usage frequency.

CSL403- Operating System Lab

You might also like