Header Ads

First In First Out (FIFO) page replacement algorithm

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.
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.




program in Python:
 pages,q=[],[]  
 n=int(input("Enter no of pages"))  
 capacity=int(input("Enter the capacity of main memory"))  
 for i in range(0,n):  
   pages.append(int(input()))  
 t,pf=0,0  
 print("req.page  main memory")  
 for i in range(0,n):  
   if(t<capacity):  
     if (pages[i] not in q):  
       q.append(pages[i])  
       pf=pf+1  
       t=t+1  
   else:  
     if (pages[i] not in q):  
       q.pop(0)  
       q.append(pages[i])  
       pf=pf+1  
   print(str(pages[i])+"    "+str(q))  
 print("Page Fault :: "+str(pf),"\n"+"Page Hit :: "+str(n-pf))  

No comments

Powered by Blogger.