Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
After lots of research online and studying various source codes. I thought of trying myself to come up with a way to write the round robin code.
I tried but i get errors in certain part of my output. I am unable to generate the Gaint chart and also please can anyone explain me where i made mistake in calculating my waiting time algorithm.

Can anyone explain me specially how can i get the waiting time of each process and kindly correct my algorithm .Please i have a sincere request. I am a self learner and there is basically no one other than online articles or books to help me understand a code. So, if you correct my mistake, please can you explain me along with the correction. I really want to get the concept because i dont want to do the same mistake again. Thanks.


here is my code:-

C++
#include<iostream>

using namespace std;

int main()
{
	int k,j,q,i,n,ts,temp;
     int aw;                      float awt;
     int bt[10],wt[10],te[10],rt[10],at[10];j=0; //te array stores the number of times a process comes to CPU before completion

	 //bt is my burst time store array
	 //wt is my waiting time array
	 //te keeps a count of the number of times a process enters the CPU before completion
	 //at is the arrival time


	cout<<"Enter number of processes"<<endl;
	 cin>>n;

	 

	 for(i=0;i<n;i++)
	 {
		 
	 cout<<"Enter burst time"<<endl; 

		 cin>>bt[i];

		 cout<<"Enter arrival times"<<endl;
		 cin>>at[i];

		 te[i] = 0; wt[i] = 0;
	 }

	 for(i=0;i<n;i++)
	 {
		 for(j = i+1;j<n;j++)
		 {
			 if(at[j]<at[i])
			 {
				 temp = at[i];
			     at[i] = at[j];
				 at[j] = temp;
             if(at[j] ==at[i])
				 temp = bt[i];
			     bt[i] = bt[j];
				 bt[j] = temp;
			 }
		 }
	 }


	 cout<<"Enter time slice"<<endl;
	 cin>>ts;

	 cout<<"process:"<<endl;

	 
	    for(i=0;i<n;i++)
	 {
		 cout<<"\t"<<i+1<<endl;
	 } 

	 
	 
	cout<<"Burst time:"<<endl;
	for(i=0;i<n;i++)
	{
		cout<<"  "<<bt[i]<<endl;
		rt[i] = bt[i];
	}

	cout<<"arrival time:"<<endl;
	for(i=0;i<n;i++)
	{
		cout<<"  "<<at[i]<<endl;
	}

	cout<<"Gaint chart"<<endl;

	while (j<=n)
	{
		
		j++;

		for(i = 0;i<n;i++)
		{
			if(rt[i] ==0) continue;
			if(rt[i]>=ts)
			{
				cout<<"\t"<<q<<i+1<<endl;
				q = q + ts;
				rt[i] = rt[i] - ts;
				te[i] = te[i] + 1;
			}

			else
			{
				cout<<"  "<<q<<i+1<<endl;
				wt[i] = q-te[i]*ts;
				q = q +rt[i];
				rt[i] = rt[i] - rt[i];
			}
		}
	}

	awt = 0;


	cout<<"Process Waiting Time"<<endl;
	for(i =0;i<n;i++)
	{
		wt[i] = wt[i] - at[i];
		cout<<"  "<<i+1<<endl;
			cout<<wt[i]<<endl;
		awt = awt + wt[i];
	}
	aw = awt;
	cout<<"Total waiting time"<<aw<<endl;
	cout<<"Average waiting time "<<awt/n<<endl;
	
	return 0;


}
Posted
Updated 19-May-18 4:40am
v3

I understand that you want for the fun of it (e.g. to learn something ;-)) *simulate* simple Round Robin arbitration.
Under that assumption, there are several issues to discuss:
- you mix all aspects of the problem into a flat and unstructured pile of nested ifs and fors
- you do not use data structures (classes) and functions to connect things that belong together
- there is no separation between the algorithm, the input data and the statistics visible

Let's start with statig the prpoblem first. If you cannot say what you want in prose or pseudo code, you do not need to start coding - it would not lead to a fruitful end.

Round Robin
The simple round robin algorithm takes from a pool of N candidates the next one, gives it the needed resources while the others wait, and after 1/N time, goes to the next (going after the last candidate to the first again).

For processes on one CPU, that would be implemented in some kind of timer which does the context switch after each Nth time slice (each time slice is 1/N duration). In such a situation, there is no point in doing statistics since each process gets *always* its 1/N time slice after all (N-1) other candidates were served. A process may be in some waiting state, where it kind of gives up on his time slice, so, the next process would benefit in terms of being started earlier - but it may not become a larger time slice. Round Robin guarantees that there is *no* starving of any process: every process always gets executed at latest after every (N-1) time slice.

Simulation of the Round Robin
To simulate the Round Robin arbitation, you may model the processes by some class that is in two possible states: suspended or running (this process model is the simplest one for the Round Robin simulation).

Then you could define a Scheduler class that takes a vector of processes and a strategy to schedule (e.g. the Round Robin).

E.g. the followong skeleton may be a starting point:
C++
class Process {
public:
    Process(const string& name): _name(name) {}
    bool resume()  const { cout << "resuming " << name << endl; return true; }
    bool suspend() const { cout << "suspending " << name << endl; return true; }
private:
    const string _name;
};

class SchedulingStrategy {
public:
   virtual void next(const vector<Process*> &processes) = 0;
};

class RoundRobinStrategy: public SchedulingStrategy {
public:
   RoundRobinStrategy(): _current(-1) {}
   virtual void next(const vector<Process*> &processes)
   {
       // only active if there are processes
       if (processes.size() > 0)
       {
          if (_current < 0)
          {
             // special initial handling
             _current = 0;
          }
          else
          {
             // stady state handling
             processes[_current]->suspend();
             _current++;
             _current %= processes.size();
          }  
          processes[_current]->resume();
       }
   }
private:
   int _current;
};

class Scheduler {
public:
   Scheduler(SchedulingStrategy &strategy)
   : _strategy(strategy), processes()
   {}
   void addProcess(const string& name) { _processes.push_back(new Process(name)); }
   void run(int n)
   {
       while(--n > 0) _strategy.next(_processes);
   }
private:
   SchedulingStrategy &_strategy;
   const vector<Process*> _processes;
};

int main()
{
    RoundRobinStrategy rr;
    Scheduler scheduler(rr);
    scheduler.addProcess("A");
    scheduler.addProcess("B");
    scheduler.addProcess("C");
    scheduler.addProcess("D");
    scheduler.run(10); // won't call a final suspend in this simple implementation
}


Have fun!
Andi
 
Share this answer
 
Comments
Member 12977920 5-Feb-17 3:18am    
what is composition and aggregation in this code?
Andreas Gieriet 1-May-17 17:33pm    
Any suggestion?
Andi
C++

#include<iostream>
using namespace std; 
int main()
{  
int wtime[10],btime[10],rtime[10],num,quantum,total;
cout<<"Enter number of processes(MAX 10): "; 
cin>>num;
 
cout<<"Enter burst time";
for(int i=0;i<num;i++)
{  cout<<"\nP["<<i+1<<"]: "; cin>>btime[i];
   rtime[i] = btime[i];
   wtime[i]=0;
}
cout<<"\n\nEnter quantum: "; cin>>quantum;
int rp = num;
int i=0;
int time=0;
cout<<"0"; 
wtime[0]=0; 
while(rp!=0) { 
 if(rtime[i]>quantum)
 {
   rtime[i]=rtime[i]-quantum;
   cout<<" | P["<<i+1<<"] | ";
   time+=quantum;
   cout<<time;
   }
 else if(rtime[i]<=quantum && rtime[i]>0)
 {time+=rtime[i];
  rtime[i]=rtime[i]-rtime[i];
  cout<<" | P["<<i+1<<"] | ";
  rp--;
  cout<<time;
 }
 
i++;
if(i==num)
{
i=0;
}
}
system("pause"); 
return 0;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 19-May-18 11:26am    
Answered FOUR years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900