FIFO page replacement scheduling algorithm Program Code in C and C++
C++ Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <bits/stdc++.h> using namespace std; const int N=100005; int n; int frame_size; int pages[N]; int mark[N]; void fifo_page_replacement(void) { queue<int> Q; int page_faults=0; for(int i=0; i<n; i++) { if(mark[pages[i]]==true) { cout<<"Reference to page "<<pages[i]<<" did not cause a page fault\n"; } 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<<"Reference to page "<<pages[i]<<" caused a page fault\n"; } } cout<<"\nTotal Page Faults: "<<page_faults; return; } int main() { cout<<"Number of Frames: "; cin>>frame_size; 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(); return 0; } /** 3 20 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 */ |
C Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#include<stdio.h> #include<conio.h> int fr[3]; void main() { void display(); int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2}; int flag1=0,flag2=0,pf=0,frsize=3,top=0; clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<12;i++) { if(fr[i]==page[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<frsize;i++) { if(fr[i]==-1) { fr[i]=page[j]; flag2=1; break; } } } if(flag2==0) { fr[top]=page[j]; top++; pf++; if(top>=frsize) top=0; } display(); } printf("Number of page faults : %d ",pf); getch(); } void display() { int i; printf("\n"); for(i=0;i<3;i++) printf("%d\t",fr[i]); } /* OUTPUT : 2 -1 -1 2 3 -1 2 3 -1 2 3 1 5 3 1 5 2 1 5 2 4 5 2 4 3 2 4 3 2 4 3 5 4 3 5 2 Number of page faults : 6 */ |