Page Replacement
Page Replacement
EXPERIMENT 8
Asmi Bhanushali
2021700007
D1
Theory-
Basic to the implementation of virtual memory is the concept of demand
paging. This means that the operating system, and not the programmer,
controls the swapping of pages in and out of main memory as they are
required by the active processes. When a non-resident page is needed by a
process, the operating system must decide which resident page is to be
replaced by the requested page. The part of the virtual memory which makes
this decision is called the replacement policy.There are many approaches to
the problem of deciding which page to replace but the object is the same for
all--the policy which selects the page that will not be referenced again for the
longest time. First In First Out (FIFO): The page to be replaced is the "oldest"
page in the memory, the one which was loaded before all the others Least
Recently Used (LRU): The page to be replaced is the one which has not been
referenced since all the others have been referenced Last In First Out (LIFO):
The page to be replaced is the one most recently loaded into the memory
Least Frequently Used (LFU): The page to be replaced is the one used least
often of the pages currently in the memory Optimal (OPT or MIN): The page to
be replaced is the one that will not be used for the longest period of time. This
algorithm requires future knowledge of the reference string which is not
usually available. Thus, this policy is used for comparison studies.
Algorithm-
FIFO Replacement Algorithm:
Let capacity be the number of pages that memory can hold. Let set be the
current set of pages in memory.
1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one until the size of set reaches capacity or
all page requests are processed.
b) Simultaneously maintain the pages in the queue to perform FIFO.
c) Increment page fault
ii) Else If current page is present in set, do nothing.
iii) Else
a) Remove the first page from the queue as it was the first to be entered in the
memory
b) Replace the first page in the queue with the current page in the string.
c) Store current page in the queue.
d) Increment page faults.
2. Return page faults.
Flowchart-
Code-
Code-
#include<stdio.h>
void fifo(int f,int no,int a[]){
int m, n, s,miss=0,hit=0;
printf("Input \t Frames\n");
int temp[f];
for(m = 0; m < f; m++)
{
temp[m] = -1;
}
for(m = 0; m < no; m++)
{
s = 0;
for(n = 0; n < f; n++)
{
if(a[m] == temp[n])
{
s++;
hit++;
miss--;
}
}
miss++;
if((miss <= f) && (s == 0))
{
temp[m] = a[m];
}
else if(s == 0)
{
temp[(miss - 1) % f] = a[m];
}
printf("\n");
printf("%d\t",a[m]);
for(n = 0; n < f; n++)
{
if(temp[n] != -1)
printf(" %d\t", temp[n]);
else
printf(" - \t");
}
}
float mr=(float)miss/no;
float hr=(float)hit/no;
printf("\nTotal Page Misses:%d\n", miss);
printf("Miss ratio=%f\n",mr);
printf("Total Page Hits:%d\n", hit);
printf("Hit ratio=%f\n",hr);
}
int search(int num, int frames[], int full)
{
for (int i = 0; i < full; i++)
if (frames[i] == num)
return 1;
return 0;
}
void current(int num, int frames[], int full, int f){
printf("\n%d \t", num);
for(int i = 0; i < f; i++){
if(i < full)
printf("%d \t", frames[i]);
else
printf("- \t");
}
}
int predict(int a[], int frames[], int n, int index, int full)
{
int result = -1, notimp = index;
for (int i = 0; i < full; i++) {
int j;
for (j = index; j < n; j++){
if (frames[i] ==a[j]){
if (j > notimp) {
notimp = j;
result = i;}
break;
}}
if (j == n)
return i;}
if(result == -1)
return 0;
else
return result;
}
void optimal(int a[], int n,int f)
{
int frames[f];
int full= 0;
printf("Input\t Frames\n");
int hits = 0;
for (int i = 0; i < n; i++) {
if (search(a[i], frames,full)) {
hits++;
current(a[i], frames, full, f);}
if (!search(a[i], frames,full)){
if (full < f){
frames[full] = a[i];
full++;
current(a[i], frames, full, f);}
else {
int pos = predict(a, frames, n, i + 1, full);
frames[pos] = a[i];
current(a[i], frames, full, f);}}
}
float hr=(float)hits/n;
float mr=(float)(n-hits)/n;
printf("\nHits: %d\n", hits);
printf("Hit ratio=%f\n",hr);
printf("Misses: %d\n", n - hits);
printf("Miss ratio=%f\n",mr);
}
int main(){
int n,f,x;
printf("Enter no of elements:\n");
scanf("%d",&n);
int a[n];
printf("Enter no of frames:\n");
scanf("%d",&f);
printf("Enter the elements:\n");
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter 1 for FIFO policy 2 for Optimal Page Policy:\n");
scanf("%d",&x);
if(x==1)
fifo(f,n,a);
else if(x==2)
optimal(a,n,f);
return 0;
}
Output-
Conclusion- From this experiment I learnt how to perform FIFO and Optimal
Page Replacement policies and to implement them using C Programming
Language.