Write a program to implement FIFO page replacement algorithm using C++

  The following c program will help you to implemen FIFO page replacement algorithm using C++

Page Reference Stream Length: 20
Page Reference Stream:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

Frame size  Page faults	Page Hits 
  4 	      10	10 
	

Table of Contents

C++ Program Code

   
 

/*;==========================================
; Title: Write a program to implement FIFO page replacement algorithm.
; Owned: codenaive littleboy8506@  <[email protected]> <[email protected]>
; Contribution:  Mansi bansal
; Programming Language: C++
; Date:   9 Feb 2021
;==========================================*/
#include <bits/stdc++.h> 
using namespace std; 
 
const int N=100005; 
 
void fifo_page_replacement(int frame_size, int n,int pages[]) 
{ 
 	int mark[N];     queue<int> Q; 
    int page_faults=0; 
 
    for(int i=0; i<n; i++) 
    { 
        if(mark[pages[i]]==true) 
        { 
        }         else 
        { 
            Q.push(pages[i]);             mark[pages[i]]=true; 
            if(Q.size()>frame_size) 
            { 
                int p= Q.front();                 mark[p]=false; 
                Q.pop(); 
            } 
            page_faults++; 
             
        } 
 
    }    
    cout<<"Frame size          Page faults          Page Hits\n"; 
    cout<<"     "<<frame_size<<"                     "<<page_faults<<"                "<<n-page_faults<<"\n"; 
 	return; 
} 
int main() 
{ 
 	int frame_size=4; int pages[N]; 
int n;    
    cout<<"Page Reference Stream Length: ";     cin>>n; 
 cout<<"Page Reference Stream:\n";     for(int i=0; i<n; i++)         cin>>pages[i]; 
  	fifo_page_replacement(frame_size,n,pages);  	return 0; 
} 
 

			 
						  
			 
						 					

Output

~/admin@codenaive/os $ g++ fifo.cpp -o fifo
~/admin@codenaive/os $ ./fifo 
Page Reference Stream Length: 20 
Page Reference Stream: 
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 
Frame size 				Page faults 			Page Hits 
	4 						10						 10 


						

Leave a Reply

Your email address will not be published. Required fields are marked *