What is Round Robin Scheduling in OS?

Learn via video courses
Topics Covered

Overview

The Round-robin scheduling algorithm is a kind of preemptive First come, First Serve CPU Scheduling algorithm where each process in the ready state gets the CPU for a fixed time in a cyclic way (turn by turn). It is the oldest scheduling algorithm, which is mainly used for multitasking.

What is Round Robin Scheduling in OS?

The Round robin scheduling algorithm is one of the CPU scheduling algorithms in which every process gets a fixed amount of time quantum to execute the process.

In this algorithm, every process gets executed cyclically. This means that processes that have their burst time remaining after the expiration of the time quantum are sent back to the ready state and wait for their next turn to complete the execution until it terminates. This processing is done in FIFO order which suggests that processes are executed on a first-come, first-serve basis.

Note: The CPU time quantum is the time period defined in the system.

How does the Round Robin Algorithm Work?

  1. All the processes are added to the ready queue.
  2. At first, The burst time of every process is compared to the time quantum of the CPU.
  3. If the burst time of the process is less than or equal to the time quantum in the round-robin scheduling algorithm, the process is executed to its burst time.
  4. If the burst time of the process is greater than the time quantum, the process is executed up to the time quantum (TQ).
  5. When the time quantum expires, it checks if the process is executed completely or not.
  6. On completion, the process terminates. Otherwise, it goes back again to the ready state.

Consider the below flow diagram for a better understanding of Round Robin scheduling algorithm:

working of round robin scheduling in os

Characteristics of Round Robin Scheduling in OS

  • Round robin scheduling in os is clock-driven (Hybrid model).
  • It is a Preemptive type of CPU scheduling algorithm in OS.
  • The round-robin algorithm generally focuses on the Time Sharing technique.
  • Round robin Scheduling is the simplest and one of the oldest algorithms.
  • This algorithm is a real-time algorithm as it responds to an event within a specific time limit.
  • Round robin is a widely used algorithm in traditional OS.

Example of Round Robin Scheduling Algorithm

Consider the following 6 processes: P1, P2, P3, P4, P5, and P6 with their arrival time and burst time as given below:

Q. What are the average waiting and turnaround times for the round-robin scheduling algorithm (RR) with a time quantum of 4 units?

Process IDArrival TimeBurst Time
P105
P216
P323
P431
P545
P664

Ready Queue:

At first, In the ready queue, process P1 will be executed for a time slice of 4 units. Since there are no processes initially, Process P1, with a burst time of 5 units, will be the only process in the ready queue.

P1
5

Ready Queue:

Along with the execution of P1, four more processes, P2, P3, P4, and P5, arrive in the ready queue. P1 will be added to the ready queue due to the remaining 1 unit.

P2P3P4P5P1
63151

Ready Queue:

During the execution of P2, P6 arrived in the ready queue. Since P2 has not been completed, P2 will be added to the ready queue.

P3P4P5P1P6P2
315142

Ready Queue:

Similarly, P3 and P4 have been completed, but P5 has a remaining burst time of 1 unit. Hence it will be added back to the queue.

P1P6P2P5
1421

Ready Queue:

The next processes, P6 and P2, will be executed. Only P5 will be left with 1 unit of burst time.

P5
1

GANTT Chart:

The Gantt chart will look like this:

Example of round-robin scheduling algorithm

As we know,

Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time

ProcessesArrival Time(AT)Burst Time(BT)Turn Around Time(TAT)Waiting Time(WT)
P1051712
P2162216
P32396
P43198
P5452015
P6641511

Avg Waiting Time = (11+5+8+6+16+12)/6 = 68/6 = 11.33 units.

Implementation of Round Robin Algorithm in C++

Output:

Average Turn Around Time = 15.33 Average Waiting Time = 11.33

  • Code Explanation:
    • Initialize a FIFO queue to implement this algorithm and push the first process into the queue.
    • We will use an array to check whether the process is in the queue.
    • Keep track of the time using a variable - current_time
    • If the process gets CPU for the first time, record its start time as current_time.
    • Give the quantum unit of time to the process in the front of the queue and pop this process from the queue.
    • If the burst time of this process becomes 0, calculate the Completion Time, Turn Around Time, and Waiting Time for it.
    • If some process had arrived when this process was executing, insert them into the queue.
    • If the current process has burst time remaining, push the process into the queue again.
    • If the queue is empty, pick the first process from the list that is not completed.
    • Keep doing this till all processes are completed.

Advantages of Round Robin in OS

  1. This round-robin algorithm offers starvation-free execution of processes.
  2. Each process gets equal priority and fair allocation of CPU.
  3. Round Robin scheduling algorithm enables the Context switching method to save the states of preempted processes.
  4. It is easily implementable on the system because round-robin scheduling in os doesn’t depend upon burst time.

Disadvantages of Round Robin in OS

  1. The waiting and response times are higher due to the short time slot.
  2. Lower time quantum results in higher context switching.
  3. We cannot set any special priority for the processes.

Conclusion

  • The name of this algorithm comes from the round-robin principle, where every person gets an equal share of something turn by turn.
  • Every process gets executed cyclically, and the processing is done in FIFO order.
  • The execution of the Round Robin scheduling algorithm mainly depends on the value of the time quantum.
  • As the time quantum value decreases
    • The response time decreases.
    • And, The number of context switches increases.
  • As the time quantum value increases
    • The response time increases.
    • And, The number of context switches decreases.
  • We can conclude that a good scheduling algorithm for a real-time and time-sharing system must possess the following characteristics:
    • Minimum context switches.
    • Maximum CPU utilization.