Monday 3 November 2014

C++ program to implement FIFO page replacement algorithm

C++ program to implement First In First Out page replacement algorithm using circular queue.


C++ program to implement First In First Out page replacement algorithm using array

#include<iostream>
using namespace std;
class fifo_alg
{
    char ref[100];
    int frame,fault,front,rear;
    char *cir_que;
public:
    fifo_alg()
    {
        front=rear=-1;
        fault=0;
    }
    void getdata();
    void page_fault();
};
void fifo_alg::getdata()
{
    cout<<"Enter Page reference string : ";
    cin.getline(ref,50);
    cout<<"Enter no. of frames : ";
    cin>>frame;
}
void fifo_alg::page_fault()
{
    cir_que=new char[frame];
    int flag=0;
    for(int i=0;ref[i]!=0;i++)
    {
        if(ref[i]==' ')
            continue;
        if(front==-1)
        {
            cir_que[0]=ref[i];
            front=rear=0;
            fault++;
        }
        else
        {
            for(int y=front%frame;y!=rear;y=(y+1)%frame)
                if(cir_que[y]==ref[i])
                {
                        flag=1;
                        break;
                }
                if(cir_que[rear]==ref[i])
                        flag=1;
            if(flag==0)
            {
                if((rear+1)%frame==front)
                    front=(front+1)%frame;
                rear=(rear+1)%frame;
                cir_que[rear]=ref[i];
                fault++;
            }
            flag=0;
        }
    }
    cout<<"Number of page faults : "<<fault;
}
int main()
{
    fifo_alg page;
    page.getdata();
    page.page_fault();
    return 0;
}




C++ program to implement First In First Out page replacement algorithm using Linked list

#include<iostream>
using namespace std;
struct node
{
    char data;
    node *next;
};
class fifo_alg
{
    char ref[100];
    int frame,count,fault;
    node *front,*rear;
public:
    fifo_alg()
    {
        front=rear=NULL;
        fault=count=0;
    }
    void getdata();
    void page_fault();
};
void fifo_alg::getdata()
{
    cout<<"Enter Page reference string : ";
    cin.getline(ref,50);
    cout<<"Enter no. of frames : ";
    cin>>frame;
}
void fifo_alg::page_fault()
{
    int flag=0;
    for(int i=0;ref[i]!=0;i++)
    {
        if(ref[i]==' ')
            continue;
        if(front==NULL)
        {
            front=rear=new node;
            front->data=ref[i];
            front->next=NULL;
            fault=count=1;
        }
        else
        {
            node *temp=front;
            while(temp!=NULL)
            {
                if(temp->data==ref[i])
                {
                        flag=1;
                        break;
                }
                temp=temp->next;
            }
            if(flag==0)
            {
                if(count==frame)
                {
                    rear=rear->next=front;
                    front=front->next;
                    rear->data=ref[i];
                    rear->next=NULL;
                }
                else
                {
                    rear=rear->next=new node;
                    rear->data=ref[i];
                    rear->next=NULL;
                    count++;
                }
                fault++;
            }
            flag=0;
        }
    }
    cout<<"Number of page faults : "<<fault;
}
int main()
{
    fifo_alg page;
    page.getdata();
    page.page_fault();
    return 0;
}



C++ program to implement First In First Out page replacement algorithm with graphical representation

Following program uses BGI graphics and compiled it using TurboC/C++ 3.0 compiler.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
class fifo_alg
{
    char ref[100];
    int frame,fault,front,rear,x,y;
    char *cir_que;
public:
    fifo_alg()
    {
front=rear=-1;
fault=0;
x=5,y=50;
    }
    void getdata();
    void insert(char ch);
    void chart();
    void page_fault();
};
void fifo_alg::getdata()
{
    cout<<"Enter Page reference string : ";
    cin.getline(ref,50);
    cout<<"Enter no. of frames : ";
    cin>>frame;
}
void fifo_alg::chart()
{
   char d[2];
   int f=front,r=rear,flag=1;
   for(int i=0;i<frame;i++)
   {
      if(f==r&&flag==0)
      {
d[0]=0;
if(f==frame)
  f=0;
      }
      else
      {
 if(f==r)
    flag=0;
 d[0]=cir_que[f];
 f=(f+1)%frame;
 d[1]=0;
      }
      rectangle(x,y+i*20,x+20,y+(i+1)*20);
      outtextxy(x+10,y+i*20+10,d);
   }
   x+=20;
}
void fifo_alg::insert(char ch)
{
    if(front==-1)
    {
cir_que[0]=ch;
front=0,rear=0;
    }
    else
    {
rear=(rear+1)%frame;
cir_que[rear]=ch;
    }
    fault++;
}
void fifo_alg::page_fault()
{
    cir_que=new char[frame];
    int flag=0;
    for(int i=0;ref[i]!=0;i++)
    {
if(ref[i]>57||ref[i]<48)
   continue;
if(front==-1)
   insert(ref[i]);
else
{
   if(cir_que[rear]==ref[i])
flag=1;
   else
   {
for(int y=front;y!=rear;y=(y+1)%frame)
  if(cir_que[y]==ref[i])
  {
flag=1;
break;
  }
   }
   if(flag==0)
   {
if((rear+1)%frame==front)
   front=(front+1)%frame;
insert(ref[i]);
   }
}
if(flag==0)
  chart();
flag=0;
    }
    cout<<"Number of page faults : "<<fault;
}
int main()
{
    fifo_alg page;
    page.getdata();
    int gdriver = DETECT,gmode;
    initgraph(&gdriver, &gmode, "");
    page.page_fault();
    getch();
    return 0;
}


OUTPUT




3 comments:

  1. great effort....wonderfull..!!!!

    ReplyDelete
  2. great effort....wonderfull..!!!!

    ReplyDelete
  3. In "C++ program to implement First In First Out page replacement algorithm with graphical representation" there is an error in #include how to i fix it? Please help thanks.

    ReplyDelete