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:
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)
{
if (processes.size() > 0)
{
if (_current < 0)
{
_current = 0;
}
else
{
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); }
Have fun!
Andi