Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Ushdh ( 6 years ago )
#include <iostream>
using namespace std;
class RR_39
{
    public:
    int p[100],a[100],b[100],**o,**ready,**out,n,of, o_r,rf, rr,r,i_f,quantum,total_time,time,id,at,bt,ct,s;
    RR_39()
    {
        of = o_r = rf  =  rr = i_f =0;
        r =0;
        total_time=time=0;
        quantum=0;
        id=0;
        at=1;
        bt=2;
        ct=1;
        cout<<endl<<"Enter Number Of Process : "<<endl;
        cin>>n;
        cout<<endl<<"Enter Time Quantum : "<<endl;
        cin>>quantum;
        ready = new int *[3];
        out = new int *[3];
        o = new int *[2];
        for(int i=0;i<n;i++)
        {
            p[i]=0;
            a[i]=0;
            b[i]=0;
        }
        for(int i=0;i<n;i++)
        {
            cout<<endl<<"Process : "<<i<<endl;
            p[i]=i;
            cout<<"Enter AT : ";
            cin>>a[i];
            cout<<"Enter BT : ";
            cin>>b[i];
            total_time+=b[i];
        }
                arrange();
        cout<<endl<<"INPUT QUEUE"<<endl;
        dis_arr();
        s=total_time+1/quantum;
        for (int i = 0; i <=s; i++)
        {
            ready[i] = new int[3];
        }
        for (int i = 0; i <=s; i++)
        {
            out[i] = new int[3];
        }
        for (int i = 0; i <=s; i++)
        {
            o[i] = new int[2];
        }

        cout<<endl<<"Ready QUEUE"<<endl;
            prempt(time);
            dis_read();
                schedule();

    }
    void display()
    {
        cout<<endl<<"Process   at bt"<<endl;
        for (int i = 0; i < r; i++)
        {
            cout<< " |"<< " P"<<o[i][id];
        }
        cout<< " |"<< endl<<" 0   ";
        for (int i = 0; i < r; i++)
        {
             if(o[i][ct] <10)
                    cout<< " "<< o[i][ct]<< "   ";
                else
                    cout<< " "<< o[i][ct]<< "  ";
        }
    }

    void dis_arr()
    {
        cout<<endl<<"Process   at\tbt";
        for (int i = 0; i < n; i++)
        {
            cout<<endl<<"P"<<p[i]<<"  \t  "<<a[i]<<"\t"<<b[i];

        }
        cout<<endl;
    }
    void dis_read()
    {
        cout<<endl<<"Process   at\tbt";
        for (int i = rf; i < rr; i++)
        {
            cout<<endl<<"P"<<ready[i][id]<<"  \t  "<<ready[i][at]<<"\t"<<ready[i][bt];

        }
        cout<<endl;
    }
    void arrange()
    {
        cout<<endl<<"arranging"<<endl;
        int t1=0,t2=0,t3=0;
        for(int i=0;i<n;i++)
    	 {
    		 for(int j = i+1;j<n;j++)
    		 {
    			 if(a[j]<a[i])
    			 {
    			     t1 = p[i];
    			     p[i] = p[j];
    				 p[j] = t1;

    			     t2 = a[i];
    			     a[i] = a[j];
    				 a[j] = t2;

    			     t3 = b[i];
    			     b[i] = b[j];
    				 b[j] = t3;
    			 }
    			  if(a[j]==a[i])
    			 {
    			     if(b[j]<b[i])
        			 {
        			     t1 = p[i];
        			     p[i] = p[j];
        				 p[j] = t1;

        			     t2 = a[i];
        			     a[i] = a[j];
        				 a[j] = t2;

        			     t3 = b[i];
        			     b[i] = b[j];
        				 b[j] = t3;
        			 }
    			 }
        	}
    	 }
    }
    void prempt(int time)
    {
        // i_f is front of input queue
        for(int i=i_f;i<n;i++)
        {
            // inserting process which are arrived in ready queue
            if(a[i]<=time && b[i]>0)
            {
                ready[rr][id]=p[i];
                ready[rr][at]=a[i];
                ready[rr][bt]=b[i];
                //ready queue rear incremented
                rr=(rr+1)%s;
                //increment of input queue front
                i_f++;
                b[i]-=quantum;
            }
        }
    }
    void schedule()
    {

        cout<<endl<<"Scheduling"<<endl;
        while(time < total_time)
        {
            if(rf!= -1 && ready[rf][bt]>quantum)
            {
                time+=quantum;
                //decreasing the burst time to later re-insert in ready queue
                ready[rf][bt]-=quantum;
                // get all the process that are arrived using prempt function
                prempt(time);
                //re-insert current process in ready queue
                o[r][id]=ready[rr][id]=ready[rf][id];
                ready[rr][at]=ready[rf][at];
                ready[rr][bt]=ready[rf][bt];
                //ready queue rear incremented
                rr=(rr+1)%s;
                rf=(rf+1)%s;
                o[r][ct]=time;
                r++;
            }
            else if(rf!= -1 && ready[rf][bt]<=quantum)
            {
                //increment of time by current process's BT
                time+=ready[rf][bt];
                o[r][id]=out[++o_r][id] = ready[rf][id];
                o[r][ct]=out[o_r][ct]=time;
                //ready queue rear incremented
                r++;
                rf=(rf+1)%s;
            }
        }
        display();
    }

};
int main()
{
    RR_39 z;
    return 0;
}

 

Revise this Paste

Your Name: Code Language: