C++: Round Robin CPU Scheduling Algorithm

Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices (also known as time quanta) are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). Round-robin scheduling is simple, easy to implement, and starvation-free. Round-robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in computer networks. It is an operating system concept.

The name of the algorithm comes from the round-robin principle known from other fields, where each person takes an equal share of something in turn.

To schedule processes fairly, a round-robin scheduler generally employs time-sharing, giving each job a time slot or quantum (its allowance of CPU time), and interrupting the job if it is not completed by then. The job is resumed next time a time slot is assigned to that process. If the process terminates or changes its state to waiting during its attributed time quantum, the scheduler selects the first process in the ready queue to execute. In the absence of time-sharing, or if the quanta were large relative to the sizes of the jobs, a process that produced large jobs would be favoured over other processes.

Round-robin algorithm is a pre-emptive algorithm as the scheduler forces the process out of the CPU once the time quota expires.[Wikipedia]

Here we have used some features of C++11. Algorithm Library will help to understand them.

This program also uses the concept of Inheritance.

Data members and member functions which are common to all scheduling algorithm are declared in scheduling.h and member functions are declared in scheduling.cpp . virtual keyword is used before calcEndTime() because each scheduling algorithm has different algorithm to calculate end time.

Turnaround time and waiting time are calculated using these formulas.

turnAroundTime = endTime - arrivalTime
waitingTime = turnAroundTime - burstTime

Here is the C++ implementation.

You can compile with this command in Linux g++ -std=c++11 shortestjobfirst.cpp scheduling.cpp

Round Robin


Other CPU Scheduling Algorithms 
C++: Shortest Job First Preemptive CPU Scheduling Algorithm 
C++: Priority Preemptive CPU Scheduling Algorithm