Tuesday, 25 September 2012

Multilevel feedback using queue & array


//Here in this programe i am using two queue
#include<iostream.h>
#include<conio.h>
int front=-1,rear=-1,front1=-1,rear1=-1;
#define MAX 29
int a[MAX];
void en(int x)//for insertion to thequeue
{
   
     if(rear==MAX-1)
     return;
     rear=rear+1;
     a[rear]=x;
     if(front==-1)
     front=0;
     };
    void en1(int x)
{
   
     if(rear1==MAX-1)
     return;
     rear1=rear1+1;
     a[rear1]=x;
     if(front1==-1)
     front1=0;
     };
     int de()//for deletion from the queue
     {int e;//for returning the index of the element
         e=front;
         if(front==rear)
      {
                     front=-1;
       rear=-1;}
       else
       front=front+1;
       return a[e];
       };
        int de1()
     {int e;
         e=front1;
         if(front1==rear1)
      {
                     front1=-1;
       rear1=-1;}
       else
       front1=front1+1;
       return a[e];
       };
     
main()
{
      int p[15],b[15],n,b1[15],pn,t[15],w[15],q,q1;
      cout<<"Enter n";//for giving the no. of the process
      cin>>n;
      cout<<"entr  p& b";
      for(int i=0;i<n;i++)//for entering the process no. & burst time of each process
      {cin>>p[i]>>b[i];
      b1[i]=b[i];
      t[i]=0;
      w[i]=0;
      en(i);}

      for(int i=1;i<=3;i++)
      {if(i%2!=0)
          {    while(front!=-1)
             {
                   pn=de();
                   if(i==1)
                   { q=8;
                 
                   if(b[pn]>q)
                  {b[pn]=b[pn]-q;
                   q1=q;}
                    else
                   { q1=b[pn];
                     b[pn]=0;
                     }}
                     else
                   { q1=b[pn];
                     b[pn]=0;
                     }
                   for(int i=0;i<n;i++)//for calculation of waitng time of the processes while one process is running
                   {
                           if(i!=pn & b[i]>0)
                           w[i]=w[i]+q1;
                           }
                     if(b[pn]>0)
                      en1(pn);
                      }//endin of the 1st while statement
        }//endin of the 1st if statement                
      else
                while(front1!=-1)
              {  pn=de1();
                   q=16;
                   if(b[pn]>q)
                  {b[pn]=b[pn]-q;
                   q1=q;}
                  else
                   { q1=b[pn];
                     b[pn]=0;
                     }
                  for(int i=0;i<n;i++ )//for calculation of waiting time when one process executing
                   {
                           if(i!=pn & b[i]>0)
                           w[i]=w[i]+q1;
                           }
                     if(b[pn]>0)
                      en(pn);
              }                        
           } //ending of else statement        
         for(int i=0;i<n;i++)//for the calculation of turn around time of each process
         t[i]=w[i]+b1[i];
         cout<<"Process\n";
         for(int i=0;i<n;i++)//for displaying result
         cout<<p[i]<<"\t"<<b1[i]<<"\t"<<w[i]<<"\t"<<t[i]<<"\n";
            getch();
            } //end of main            

1 comment: