Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Program-7 - Page Replacement Algorithms

Uploaded by

sreelakshmikn33
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)
29 views

Program-7 - Page Replacement Algorithms

Uploaded by

sreelakshmikn33
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/ 7

Department of Computer Science Engineering – ( Data Science)

Program 7: Develop a C program to simulate page replacement algorithms


a) FIFO b) LRU

Paging
 Paging is a memory management technique used by operating systems to optimize computer
memory usage.
 This technique divides the memory into small pages, and each process is allocated a set of
pages. Pages can be swapped in and out of memory as needed, which helps to improve memory
utilization.

Page Replacement Algorithms


 In operating systems that use paging for memory management, page replacement algorithm are
needed to decide which page needed to be replaced when new page comes in.
 Whenever a new page is referred and not present in memory, page fault occurs and Operating
System replaces one of the existing pages with newly needed page.
 Different page replacement algorithms suggest different ways to decide which page to replace.
 The target for all algorithms is to reduce number of page faults.

Operating Systems Lab[BCS303] 1


Department of Computer Science Engineering – ( Data Science)

First In First Out (FIFO) page replacement algorithm


 This is the simplest page replacement algorithm. In this algorithm, operating system keeps track
of all pages in the memory in a queue, oldest page is in the front of the queue.
 When a page needs to be replaced page in the front of the queue is selected for removal.
 Example : Consider page reference string 1, 3, 0, 3, 5, 6 and 3 page slots.
- Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots so
3 Page Faults.
- when 3 comes, it is already in memory so 0 Page Faults.
- Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. So 1
Page Fault.
- Finally 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3
so 6 Page Fault.
- So total page faults = 5.
F1 1 1 1 5 5
F2 3 3 3 6
F3 0 0 0

Program:
#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:");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :");
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");

Operating Systems Lab[BCS303] 2


Department of Computer Science Engineering – ( Data Science)

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("Number of Page Fault is: %d",count);
return 0;
}

Sample Output:

ENTER THE NUMBER OF PAGES: 6

ENTER THE PAGE NUMBER : 1 3 0 3 5 6

ENTER THE NUMBER OF FRAMES :3

ref string page frames


1 1 -1 -1
3 1 3 -1
0 1 3 0
3
5 5 3 0
6 5 6 0

Number of Page Fault is: 5

Operating Systems Lab[BCS303] 3


Department of Computer Science Engineering – ( Data Science)

Least Recently Used(LRU) Algorithm


 This algorithm is based on the strategy that whenever a page fault occurs, the least recently used
page will be replaced with a new page.
 So, the page not utilized for the longest time in the memory (compared to all other pages) gets
replaced.
 Example: Let say the page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 . Initially we have 4 page
slots empty.
- Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots so 4 Page
faults
- 0 is already there so 0 Page fault.
- when 3 came it will take the place of 7 because it is least recently used so1 Page fault
- 0 is already in memory so 0 Page fault.
- 4 will takes place of 1so 1 Page Fault
- Now for the further page reference string so 0 Page fault because they are already
available in the memory.

Operating Systems Lab[BCS303] 4


Department of Computer Science Engineering – ( Data Science)
Program
#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)
{
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;

Operating Systems Lab[BCS303] 5


Department of Computer Science Engineering – ( Data Science)
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);
}

Sample output
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9

Operating Systems Lab[BCS303] 6


Department of Computer Science Engineering – ( Data Science)
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2

The no of page faults is 10

Operating Systems Lab[BCS303] 7

You might also like