A41 Os Exp 9
A41 Os Exp 9
Experiment No.09
Aim:
Write a program in C demonstrate the concept of page replacement policies for handling
page faults using FIFO.
Outcome:
After successful completion of this experiment students will be able to,
Implement and analyze concepts of virtual memory.
Theory:
Virtual Memory:
Virtual Memory is a storage mechanism which offers user an illusion of having a very
big main memory. It is done by treating a part of secondary memory as the main memory.
In Virtual memory, the user can store processes with a bigger size than the available main
memory.
Therefore, instead of loading one long process in the main memory, the OS loads the
various parts of more than one process in the main memory. Virtual memory is mostly
implemented with demand paging and demand segmentation.
Virtual memory has become quite common these days. It is used whenever some pages
require to be loaded in the main memory for the execution, and the memory is not
available for those many pages.
So, in that case, instead of preventing pages from entering in the main memory, the OS
searches for the RAM space that are minimum used in the recent times or that are not
referenced into the secondary memory to make the space for the new pages in the main
memory.
Demand Paging:
Demand paging is a technique used in virtual memory systems where the pages are
brought in the main memory only when required or demanded by the CPU. Hence, it is
also named as lazy swapper because the swapping of pages is done only when required
by the CPU.
1. Now, if the CPU wants to access page P2 of a process P, first it will search the page
in the page table.
2. As the page table does not contain this page so it will be a trap or page fault . As
soon as the trap is generated and context switching happens and the control goes to
the operating system.
3. The OS system will put the process in a waiting/ blocked state. The OS system will
now search that page in the backing store or secondary memory.
4. The OS will then read the page from the backing store and load it to the main
memory.
5. Next, the OS system will update the page table entry accordingly.
6. Finally, the control is taken back from the OS and the execution of the process is
resumed.
7. Hence whenever a page fault occurs these steps are followed by the operating system
and the required page is brought into memory.
Advantages
It increases the degree of multiprogramming as many processes can be present in the
main memory at the same time.
There is a more efficient use of memory as processes having size more than the size of
the main memory can also be executed using this mechanism because we are not loading
the whole page at a time
Disadvantages
The amount of processor overhead and the number of tables used for handling the page
faults is greater than in simple page management techniques.
FIFO:
FIFO algorithm replaces the oldest (First) page which has been present for the longest
time in the main memory.
When a new page comes in from secondary memory to main memory, It selects the front
of the queue which is the oldest page present, and removes it.
Working:
• The OS maintains a list of all pages which are residing in the memory
• When a new page brings from the secondary memory
• The new page requests the main memory
• On a page fault, the head of the list i.e the oldest will be removed
• The new page will be added at the tail of the list.
Example:
Calculate number of page faults and page hits for the page replacement policy – FIFO for
given reference string 6, 0, 5, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 5, 2, 0, 5, 6, 0, 5. Assume number
of frames = 3
Ref.
6 0 5 2 0 3 0 4 2 3 0 3 2 5 2 0 5 6 0 5
String
Frame 1 6 6 6 2 2 2 2 4 4 4 0 0 0 0 0 0 0 6 6 6
Frame 2 0 0 0 0 3 3 3 2 2 2 2 2 5 5 5 5 5 0 0
Frame 3 5 5 5 5 0 0 0 3 3 3 3 3 2 2 2 2 2 5
Page Fault
(F) / Page F F F F H F F F F F F H H F F H H F F F
Hit (H)
Advantages:
• Simple to understand and implement
• Does not cause more overhead
CODE:
#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 sequence: ");
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;
for(i=1;i<=n;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++;
}
}
printf("\nPage hits are %d",(n-count));
printf("\nPage faults are %d",count);
return 0;
}
Conclusion:
Hence, we conclude that the FIFO Page Replacement Algorithm helps in managing memory
by replacing the oldest page when a new page needs space. In this experiment, we counted
both page faults (when a page is not found in memory) and page hits (when a page is already
in memory). We observed that FIFO is simple but may not always be the most efficient, as it
does not consider how frequently a page is used.