C / C++ Code / Program For FIFO Page Replacement Algorithm (First In First Out)

Leave a Comment
/* The simplest page-replacement algorithm is a FIFO algorithm. The first-in, first-out (FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on the part of the operating system. The idea is obvious from the name – the operating system keeps track of all the pages in memory in a queue, with the most recent arrival at the back, and the oldest arrival in front. When a page needs to be replaced, the page at the front of the queue (the oldest page) is selected. While FIFO is cheap and intuitive, it performs poorly in practical application. Thus, it is rarely used in its unmodified form. This algorithm also experiences Bélády's anomaly.*/
/*The Following C++ Code Demonstrate The Use OF FIFO (First In First Out) Page Replacement Algorithm & the code has been tested in Microsoft Visual Studio 2010*/

   

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    using namespace std;
    
    int found(int x,int *l,int max)
    {
            for(int i=0;i<max;i++)
                    if(l[i]==x){return(i);}
            return(-1);
    }
    
    int main()
    {
            cout<<"\n\nEnter the maximum number of frames in the main memory:\t";
            int max;
            cin>>max;
            int *l=new int[max];
            for(int i=0;i<max;i++)l[i]=-1;
            int a,x;
            int k=0,c=0,res;
            cout<<"\n\nEnter the sequence of page requests(enter -1 to stop):\t";
            while(1)
            {
                    cin>>x;
                    if(x==-1) {cout<<"\n\n";break;}
                    else{
                  
                    if(k<max)
                    {
                            if((res=found(x,l,max))!=-1) {cout<<"\n\npage "<<x<<" already exists in frame "<<res<<" in MM";
                                    cout<<"\n\nNext page:\t";}
                            else
                            {
                                    cout<<"\n\npage "<<x<<" has been allocated a frame "<<k<<" in MM.";
                                    l[k++]=x;
                                    cout<<"\n\nNext page:\t";
                            }
                    }
                    else
                    {
                            if((res=found(x,l,max))!=-1) {cout<<"\n\npage "<<x<<" already exists in frame "<<res<<" in MM";
                                    cout<<"\n\nNext page:\t";}
                            else{
                          
                            cout<<"\n\npage fault has occured";
                            cout<<"\n\npage "<<x<<" has been allocated frame "<<c<<" in MM by replacing page "<<l[c];
                            l[c]=x;
                            c=(c+1)%max;
                            cout<<"\n\nNext page:\t";
                            }
                    }
                    }
                          
            }
          
            delete[] l;
            return(0);
    }


ScreenShot:
FIFO Page Replacement Algorithm Demonstration

FIFO Page Replacement Algorithm Demonstration

0 comments: