Saturday, June 29, 2013

CPU Scheduling Algorithm : Shortest Remaining Time [Version 2] C++


Hi guys, after the the first version of C implementation of CPU Scheduling Algorithm Shortest Remaining Time by my friend, here I present another version of it in C++, which is also a much shorter program than previous one..


#include<iostream.h>
#include<conio.h>
class pro
{
public:
    int id;
    int burstTime;
    float arrivalTime;
    int roundat;
};
class sch
{
public:
    int nop,rqsize,btsum,g[100],gt[100],tt[100];
    pro min;
    pro max;
    void getdata(pro p[100]);
    void putdata(pro p[100]);
    void round(pro p[100]);
    void srt(pro p[100],pro readyq[100],int minid);
    void gantt(pro p[100],int gt[100]);
};
void sch::getdata(pro p[100])
{
       cout<<"\nEnter No. of Processes=\t";
       cin>>nop;
       for(int i=1;i<=nop;i++)
       {
        cout<<"\nEnter burst time for P"<<i<<" =\t";
        cin>>p[i].burstTime;
        btsum=btsum+p[i].burstTime;
        cout<<"\nArrival Time for P"<<i<<" =\t";
        cin>>p[i].arrivalTime;
        p[i].id=i;
       }
 }

void sch::putdata(pro p[100])
{
       cout<<"Process ID\tBurst Time\tArrival Time\n";
       cout<<"----------\t----------\t------------\n";
       for(int i=1;i<=nop;i++)
        cout<<"P"<<p[i].id<<"\t\t"<<p[i].burstTime<<"\t\t"<<p[i].arrivalTime<<"\n";
       cout<<"--------------------------------------------\n";
}
void sch::round(pro p[100])
{
    for(int i=1;i<=nop;i++)
    {
        if(int(p[i].arrivalTime)<p[i].arrivalTime)
        p[i].roundat=int(p[i].arrivalTime)+1;
        else
        {
        if(int(p[i].arrivalTime)==p[i].arrivalTime)
        p[i].roundat=int(p[i].arrivalTime);
        }
    }
}
void sch::srt(pro p[100],pro readyq[100],int minid)
{
    int pos,flag=0,k=0;
    readyq[rqsize]=p[minid];
    pos=1;
    for(int time=0/*min.roundat*/;time<=btsum;time++)
    {
    for(int i=1;i<=nop;i++)
    {
    if(p[i].roundat==time)
    {
    readyq[rqsize]=p[i];
    rqsize=rqsize+1;
    flag=1;
    }
    }
    for(i=1;i<rqsize-1;i++)
    {
    for(int j=i+1;j<rqsize;j++)
    {
    if(readyq[i].burstTime>readyq[j].burstTime)
    {
    pro temp=readyq[i];
    readyq[i]=readyq[j];
    readyq[j]=temp;
    }
    }
    }
    for(i=1;i<rqsize;i++)
    {
    if(readyq[i].burstTime!=0)
    {
    pos=i;
    break;
    }
    }
    readyq[pos].burstTime--;
    gt[time]=readyq[pos].id;
    }
    gantt(p,gt);
}
void sch::gantt(pro p[100],int gt[100])
{
    int c=0,k=0,i;
    float sum1=0.0,sum2=0.0;
    float waitingTime[100],turnaroundTime[100];
    int finishTime[100];
    int prev,current;
    prev=gt[0];
    current=gt[0];
    for(i=0;i<btsum;i++)
    {
    if(current!=prev)
    {
    g[k]=prev;
    tt[k]=c;
    k++;
    }
    c++;
    prev=gt[i];
    current=gt[i+1];
    }
    g[k]=prev;
    tt[k]=c;
    cout<<"\n----------------GANTT CHART-----------------\n\n";
    for(i=0;i<=k;i++)
    cout<<"|--P"<<g[i]<<"---";
    cout<<"|\n0";
    for(i=0;i<=k;i++)
    cout<<"\t"<<tt[i];
    int pr,flag,temppro[100],j,size=0;
    for(i=k;i>=0;i--)
    {
    pr=g[i];
    flag=0;
    for(j=0;j<size;j++)
    {
    if(pr==temppro[j])
    {
    flag=1;
    break;
    }}
    if(flag==0)
    {
    temppro[size]=g[i];
    finishTime[size]=tt[i];
    size++;
    }
    }
    cout<<"\n";
    for (i=0;i<nop;i++)
    {
        if(p[temppro[i]].arrivalTime!=p[temppro[i]].roundat)
        waitingTime[i]=float(finishTime[i]-p[temppro[i]].burstTime-p[temppro[i]].roundat-p[temppro[i]].arrivalTime+min.roundat);
        else
        waitingTime[i]=float(finishTime[i]-p[temppro[i]].burstTime-p[temppro[i]].roundat);
        sum1=sum1+waitingTime[i];
        turnaroundTime[i]=float(waitingTime[i]+p[temppro[i]].burstTime);
        sum2=sum2+turnaroundTime[i];
    }
    float waitingTimeAVG=float(sum1)/nop;
    float turnaroundTimeAVG=float(sum2)/nop;
    cout<<"\nAverage Waiting Time=\t"<<waitingTimeAVG;
    cout<<"\nAverage Turn Around Time=\t"<<turnaroundTimeAVG;
 }
void main()
{
    clrscr();
    sch s;
    s.rqsize=1;
    pro p[100],readyq[100],current;
    s.btsum=0;
    s.getdata(p);
    cout<<"\n............PROCESSES INFORMATION..........\n";
    s.round(p);
    s.putdata(p);
    s.min=s.max=p[1];
    for(int i=2;i<=s.nop;i++)
    {
        if(s.min.roundat>p[i].roundat)
        s.min=p[i];
        if(s.max.roundat<p[i].roundat)
        s.max=p[i];
    }
    cout<<"\n";
    for(i=1;i<=s.nop;i++)
    p[i].roundat=p[i].roundat-s.min.roundat;
    s.srt(p,readyq,s.min.id);
    getch();
}

Output