Page Replacement Algorithm (Operating System)
Page Replacement Algorithm (Operating System)
[ college logo ]
A STYDY ON
1
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION, MUMBAI
Certificate
This is to certify that Mr. /Mrs.
in Engineering & Technology at [ your college name ] , has completed the Micro
Head of
Institute
2
INDEX
1 Abstract 4
2 Introduction 5
5 References 24
Abstract
3
A virtual memory system requires efficient page replacement algorithms to make a decision
which pages to evict from memory in case of a page fault. Many algorithms have been
proposed for page replacement. Each algorithm is used to decide on which free page frame a
page is placed and tries to minimize the page fault rate while incurring minimum overhead.
As newer memory access patterns were explored, research mainly focused on formulating
newer approaches to page replacement which could adapt to changing workloads. This paper
attempts to summarize major page replacement algorithms. This paper is about algorithms
specific to paging. For outline of general cache algorithms (e.g. processor, disk, database,
web),In a computer operating system that uses paging for virtual memory management, page
replacement algorithms decide which memory pages to page out (swap out, write to disk)
when a page of memory needs to be allocated.
Paging happens when a page fault occurs and a free page cannot be used to satisfy the
allocation, either because there are none, or because the number of free pages is lower than
some threshold. When the page that was selected for replacement and paged out is referenced
again it has to be paged in (read in from disk), and this involves waiting for I/O completion.
This determines the quality of the page replacement algorithm: the less time waiting for
page-ins, the better the algorithm. A page replacement algorithm looks at the limited
information about accesses to the pages provided by hardware, and tries to guess which
pages should be replaced to minimize the total number of page misses,
while balancing this with the costs (primary storage and processor time) of the algorithm
itself.
4
Introduction
The full potential of multiprogramming systems can be realized by interleaving the execution of
more programs. Hence we use a two level memory hierarchy consisting of a faster but costlier
main memory and a slower but cheaper second memory. In virtual memory the combined size of
program code, data and stack may exceed the amount of main memory available in the system.
This is made possible by using secondary memory, in addition to main memory . Pages are
brought into main memory only when the executing process demands them, this is known as
demand paging. A page fault typically occurs when a process references to a page that is not
marked present in main memory and needs to be brought from secondary memory. In such a case
an existing page needs to be discarded. The selection of such a page is performed by page
replacement algorithms which try to minimize the page fault rate at the least overhead. This
paper outlines the major advanced page replacement algorithms.
A virtual memory system requires efficient page replacement algorithms to make a decision
which pages to evict from memory in case of a page fault. Many algorithms have been proposed
for page replacement. Each algorithm is used to decide on which free page frame a page is
placed and tries to minimize the page fault rate while incurring minimum overhead. As newer
memory access patterns were explored, research mainly focused on formulating newer
approaches to page replacement which could adapt to changing workloads.This paper attempts to
summarize major page replacement algorithms. This paper is about algorithms specific to
paging. For outline of general cache algorithms (e.g. processor, disk, database, web),In a
computer operating system that uses paging for virtual memory management, page replacement
algorithms decide which memory pages to page out (swap out, write to disk) when a page of
memory needs to be allocated.
Paging happens when a page fault occurs and a free page cannot be used to satisfy the allocation,
either because there are none, or because the number of free pages is lower than some threshold.
When the page that was selected for replacement and paged out is referenced again it has to be
paged in (read in from disk), and this involves waiting for I/O completion.
5
FIFO page Replacement algorithm.
Optimal page Replacement algorithm.
LRU page Replacement algorithm
The simple First-In, First-Out (FIFO) algorithm is also applicable to page replacement. All pages
in main memory are kept in a list where the newest page is in head and the oldest in tail. When a
page needs to be evicted, the oldest page is selected and the page is inserted to the head of the list
and the page at the tail is swapped out. Another implementation is using a ring (usually referred
to as clock): Every time a page has to be replaced, the page the pointer points at is swapped out
and at the same place the new page is swapped in. After this, the pointer moves to the next page.
The FIFO algorithm's performance is rather bad .The result on scan data is as follows
Implementation
6
1.Start the process
4. Check the need of replacement from old page to new page in memory
Advantages of FIFO:
1. FIFO method saves money and time in calculating the exact cost of the inventory being
sold because the cost will depend upon the most former cash flows of purchases to be
used first.
2. It is a simple concept which is easy to understand. Even a layman can grab the idea with
little explanation. The managers with little to no accounting information would be able
to understand it easily.
7
Disadvantages of FIFO:
1. One of the biggest disadvantage of FIFO approach of valuation for inventory/stock is that
in the times of inflation it results in higher profits, due to which higher “Tax Liabilities”
incur. It can result in increased cash out flows in relation to tax charges.
2. FIFO may not be a suitable measure in times of “hyper inflation”. In such times there
exist no reasonable pattern of inflation and prices of goods could inflate drastically. And
in such cases the matching of most prior purchases with most recent sales would not be
appropriate and may pump up profits to present a distorted picture.
Implementation of FIFO in c
#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++)
{
8
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;
}
Manual output:-
5 3 1 2 4
Page 1 5 5 5 2 2
Page 2 3 3 3 4
Page 3 1 1 1
Output
9
2. Optimal page Replacement algorithm
10
in an operating system that uses paging for memory management, a page replacement algorithm
is needed to decide which page needs to be replaced when new page comes in.
Page Fault – A page fault happens when a running program accesses a memory page that is
mapped into the virtual address space, but not loaded in physical memory.
Since actual physical memory is much smaller than virtual memory, page faults happen. In case
of page fault, Operating System might have to replace one of the existing pages with the newly
needed page. Different page replacement algorithms suggest different ways to decide which page
to replace. The target for all algorithms is to reduce the number of page faults.
Implementation
4. Check the need of replacement from old page to new page in memory
Advantages of optimal:
1) It is less complex and easy to implement.
11
2) A page is replaced with minimum fuss.
disavantages of optimal:
1) Not all operating systems can implement this algorithm.
2) Error detection is harder.
Implementation code in c
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k,
pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
12
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
13
break;
}
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
frames[pos] = pages[i];
faults++;
}
printf("\n");
14
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
Manual Output:-
5 3 1 2 4
Page 1 5 5 5 2 2
Page 2 3 3 3 4
Page 3 1 1 1
Output:-
15
3.LRU page Replacement algorithm:-
16
Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that
are heavily used in previous instructions are likely to be used heavily in next instructions. And
the page that are used very less are likely to be used less in future. Whenever a page fault occurs,
the page that is least recently used is removed from the memory frames. Page fault occurs when
a referenced page in not found in the memory frames.
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 —> 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
Implementation:
17
1 .Start the process
4. Check the need of replacement from old page to new page in memory
Implementation Code in c
#include<stdio.h>
18
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
19
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
20
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
return 0;
}
Manual Output:-
5 3 1 2 4
Page 1 5 5 5 2 2
Page 2 3 3 3 4
Page 3 1 1 1
Output
21
Conclusion
Thus we have developed the Page Replacement Algorithm by using c programming Language.
22
We have successfully implementing page replacement algorithm FCFS,Optimal and LRU. We
have created a separate director and pages for FIFO ,Optical ,LRU. The dominant force in the
OS market is the indirect network effect, as the related markets of the systems hardware,
peripherals, and applications software are all factors in the success of any particular OS. Another
crucial force is the path-dependence of the personal computer industry as well as user lock-in.
Finally, we applied our framework to the market, and in particular, to Microsoft and Apple, and
analyzed the strategies that determined their success.
References
23
2. OSY Programming Black Book by DT Editorial Services.
3. OSY Programming by Dr. RajendraKawale.
4. Learn c in One Day and Learn It Well by Jamie Chan.
5. Beginning Programming with OSY with Dummies by Barry A Burd.
24
a) Great help with studies and in making a career choice.
b) Improves academic performance and interest.
c) To Support Self Directed Learning.
d) To help students develop teamwork and problem-solving skills.
e) To enhance the skills to ‘communicate effectively and skillfully at workplace’.
3. Proposed Methodology:
The work will be distributed among 3 students involved in the group. To complete the
Project “Page Replacement Algorithm”, qualitative method will be used in which data
collection, analysis and interpretation is required. The data will be collected from
different sources such as Internet, reference books etc. The analysis and interpretation
will be done by observing the collected data & programming work. Finally the data will
be represented with interpretation in the form of application or collected data.
4. Action Plan:
25
Sr. Name of
Planned Planned
No Details of Activity Responsible Team
Start date Finish date
. Members
Discussion and Finalization of
1
the Project Title
2 Preparation of Abstracts
3 Literature Review
4 Collection of Data
Discussion and Outline of
5
Content
Rough Writing of the Projects
6
Contents
Editing and Proof Reading of
7
the Contents
Final Completion of the
8
Project
Seminar Presentation, viva-
9 vice, Assessment and
Submission of Report
5. Resources Required:
Sr.
Name of
no Specifications Qty. Remarks
Resource/material
.
1 for 1
1 Computers Java
student
1 for 1
2 Open learning Sources Internet
student
26