OSexpadvay
OSexpadvay
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
Total 20
Performance 5 3 2
Understanding 5 3 2
Checked by
Signature :
Date :
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
#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]);
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);
}
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 ");
farthest = j;
result = i;
}
break;
}
}
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];
Output FIFO
LRU
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.