C++ Program code for Round Robin

This is the solution for Round Robin scheduling algorithm using C++ Object Oriented Approach
#include <iostream>
#include <windows.h>
#include<conio.h>
using namespace std;

int time[20];
int quantum;
int timeLength;
int pt[20];
int ptLength;
int check;
class round
{
public :
    round()
    {
        lastTime = 0;
        state = true;
        first = true;

    }
    void askAC()
    {
        cout << "Enter Arrival : " ;
        cin >> arrivalTime ;
        cout <<"and Cpu Burst : ";
        cin >> cpuBurstTime;
        deburst = cpuBurstTime;
        WT = -arrivalTime;
    }


    int getArrival()
    {
        return arrivalTime;
    }


    int getCpu()
    {
        return cpuBurstTime;
    }

    void setLastTime(int n)
    {
        lastTime = n;
    }
    void setWt(int n,int a = 0 )
    {
        WT += n - lastTime - a;
    }

    int getWt()
    {
        return WT;
    }
    int deDeburst(int n)
    {
        int a = deburst;
        if(deburst>=quantum){
                deburst -= n;
        return quantum;
        }
        else if(deburst>0&&deburst<quantum)
        {
            deburst -= n;
            return a;
        }
        else
            return 0;
    }
    bool checkState()
    {
        return state;
    }

    void disableState()
    {
        state = false;
    }
    void enableCheck()
    {
        check += 1;
    }
    int getTat()
    {
        TAT = WT + cpuBurstTime;
        return TAT;
    }
private:
    int TAT;
    int WT;
    bool state;
    int arrivalTime;
    int cpuBurstTime;
    int lastTime;
    bool first;
    int deburst;



};

int main()
{
    int noProcess ;
    round p[100];
    cout << "Enter Quantum: ";
    cin >> quantum;
    cout << "Enter number of Process: ";
    cin >> noProcess;

    for(int i = 0; i < noProcess; i++)
    {
        p[i].askAC();
    }

    time[timeLength++] = 0;
    while(check!=noProcess)
    {
            for(int i = 0; i < noProcess ; i++)
            {

                if(p[i].checkState())
                {

                    if(p[i].getArrival()>time[timeLength-1] ){
                        time[timeLength] = p[i].getArrival();
                        timeLength++;
                        pt[ptLength++] = 333;
                    }

                    int a;

                    a = p[i].deDeburst(quantum);

                    if(a!=0){
                        p[i].setWt(time[timeLength-1]);

                        time[timeLength] = time[timeLength-1] + a;
                        timeLength++;

                        pt[ptLength++] = i+1;

                        p[i].setLastTime(time[timeLength-1]);

                    }
                    else
                    {
                        p[i].disableState();
                        p[i].enableCheck();


                    }


                }


            }


    }
    for(int i = 0; i < timeLength; i++)
    {
        cout <<  time[i] <<"\t" ;
    }
    cout << endl;
    for(int i = 0; i < ptLength; i++)
    {
        if(pt[i] == 333)
            cout << "  idle\t";
        else
        cout <<  "   p[" << pt[i] << "]\t";
    }
    cout << endl<< endl;

    for(int i =0 ;i<5 ;i++)
    {
        cout <<  "waiting time p[" << i+1 << "] :" << p[i].getWt() << endl;
        cout <<  "TAT p[" << i+1 << "] :" << p[i].getTat() << endl;
    }
    return 0;
}

No comments:

Post a Comment