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

Lab 13 OS

The document describes a lab exercise on simulating virtual memory page replacement algorithms FIFO, LRU, and LFU. It provides background on each algorithm, including that FIFO replaces the oldest page, LRU replaces the least recently used page, and LFU replaces the page with the lowest frequency count. The lab involves running C programs to simulate each algorithm with variable inputs and explaining the outputs, which include the page references, page faults, and contents of memory frames at each step. The goal is to understand how each algorithm handles page replacement in virtual memory management.

Uploaded by

Moiz Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lab 13 OS

The document describes a lab exercise on simulating virtual memory page replacement algorithms FIFO, LRU, and LFU. It provides background on each algorithm, including that FIFO replaces the oldest page, LRU replaces the least recently used page, and LFU replaces the page with the lowest frequency count. The lab involves running C programs to simulate each algorithm with variable inputs and explaining the outputs, which include the page references, page faults, and contents of memory frames at each step. The goal is to understand how each algorithm handles page replacement in virtual memory management.

Uploaded by

Moiz Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab 13

Virtual Memory
Objective

To simulate FIFO, LRU and LFU page replacement algorithm in Virtual Memory Management

Apparatus:

Hardware requirements:

Dual core CPU based on x64 architecture


Minimum of 1 GB RAM
800 MB of disk space
Software requirements:

Windows 7 SP1 (64 bit) or higher


Virtual Box
ISO Image of Lubuntu
Background

FIFO
FIFO (First in First Out) algorithm: FIFO is the simplest page replacement algorithm, the idea
behind this is, “Replace a page that page is oldest page of main memory” or “Replace the page
that has been in memory longest”. FIFO focuses on the length of time a page has been in the
memory rather than how much the page is being used.
LRU
LRU (Least Recently Used): the criteria of this algorithm are “Replace a page that has been used
for the longest period of time”. This strategy is the page replacement algorithm looking
backward in time, rather than forward.
LFU
LFU (Least Frequently Used): The least frequently used algorithm “select a page for
replacement, if the page has not been used for the often in the past” or “Replace page that page
has smallest count” for this algorithm each page maintains as counter which counter value shows
the least count, replace that page. The frequency counter is reset each time is page is loaded.
Lab Exercise – 1
Run the following C program to simulate FIFO Page Replacement. Run it at-least 3 time with
variable inputs. Also explain the outputs in the explaining area.
FIFO.c
#include<stdio.h>
int fr[3],m;
void display();
void main(){
int i,j,page[20];
int flag1=0,flag2=0,pf=0;
int n, top=0;
float pr;
system("clear");
printf("Enter length of the reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter no of frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=-1;
for(j=0;j<n;j++) {
flag1=0;
flag2=0;
for(i=0;i<m;i++) {
if(fr[i]==page[j]) {
flag1=1;
flag2=1;
break; } }
if(flag1==0) {
for(i=0;i<m;i++) {
if(fr[i]==-1) {
fr[i]=page[i];
flag2=1;
break; } } }
if(flag2==0) {
fr[top]=page[j];
top++;
pf++;
if(top>=m)
top=0; }
display(); }
pf+=m;
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr); }
void display() {
int i;
for(i=0;i<m;i++)
printf("%d\t", fr[i]);
printf("\n"); }
Explanation:
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 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. —>1 Page
Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page
Fault.
Finally when 3 come it is not avilable so it replaces 0 1 page fault

Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO) page replacement
algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots,
we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.

Optimal Page replacement –


In this algorithm, pages are replaced which would not be used for the longest duration of time
in the future.

Lab Exercise – 2
Run the following C program to simulate LRU Page Replacement. Run it at-least 3 time with
variable inputs. Also explain the outputs in the explaining area.
LRU.c
#include<stdio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
system("clear");
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++) {
scanf("%d",&rs[i]);
flag[i]=0;}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++) {
count[i]=0;
m[i]=-1; }
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++) {
for(j=0;j<f;j++){
if(m[j]==rs[i]) {
flag[i]=1;
count[j]=next;
next++; } }
if(flag[i]==0) {
if(i<f) {
m[i]=rs[i];
count[i]=next;
next++; }
else {
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++; }
pf++; }
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d\n",pf);}
Explanation of
Outputs LRU.c
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 —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.

Lab Exercise– 3
Run the following C program to simulate LFU Page Replacement. Run it at-least 3 time with
variable inputs. Also explain the outputs in the explaining area.
LFU.c
#include<stdio.h>
main() {
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
system("clear");
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++) {
cntr[i]=0;
a[i]=-1; }
printf("\nThe Page Replacement Process is – \n");
for(i=0;i<m;i++) {
for(j=0;j<f;j++)
if(rs[i]==a[j]){
cntr[j]++;
break;}
if(j==f){
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++; }
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf("\tPF No. %d",pf); }
printf("\n\n Total number of page faults -- %d\n",pf);}
Conclusion
Explanation of
Outputs FIFO.c
In this post, we will discuss the Least Frequently Used (LRU) Page Replacement Algorithm and
also write a program for the Least Frequently Used (LRU) Page Replacement algorithm. In this
algorithm, the operating system keeps track of all pages in the memory in a queue. When a
page needs to be replaced, the operating system chooses the page which is least frequently
used for the replacement with the incoming page.

We will use C++ to write this algorithm due to the standard template library support. Hence, we
will write the program of LFU Page Replacement Algorithm in C++, although, it’s very similar to
C.

INPUT:
The first line is the number of frames(n).
The second line is the number of processes (m).
The third line is an array of processes (p[m]).

OUTPUT:
Print the matrix for processes and frames.
Also, print the hit and page fault.

You might also like