This tutorial covers the concepts of Round Robin Scheduling.
Round Robin Scheduling is the preemptive scheduling algorithm.
We assign a fixed time to all processes for execution, this time is called time quantum.
All processes can execute only until their time quantum and then leave the CPU and give a chance to other processes to complete their execution according to time quantum.
Context switching mechanisms store the states of preempted processes.
The formula of Round robin Turn Around Time = Completion Time – Arrival Time
The formula of Round robin Waiting Time(W.T): Time Difference between the turnaround and the burst time.
The formula of Round robin Waiting Time = Turn Around Time – Burst Time
Examples of Round-robin scheduling
Let’s see a round-robin scheduling example with arrival time, burst time, waiting time, and time quantum.
Time quantum = 2
Animation and Video of Round Robin Scheduling
Advantage of Round-robin Scheduling
There are many advantages of Round-robin Scheduling. Some of the advantages are mentioned below;
- Round-robin Scheduling avoids starvation or convoy effect.
- In Round-robin Scheduling , all the jobs get a fair and efficient allocation of CPU.
- Round-robin Scheduling don’t care about priority of the processes.
- Round-robin Scheduling gives the best performance in terms of average response time.
- We can assume the worst case response time for the process, If we know the total number of processes on the running queue.
- Round-robin Scheduling is easily implementable on the system because it does not depend upon burst time.
- Round-robin Scheduling promotes Context switching to save the states of the preempted processes.
Disadvantages of Round-robin Scheduling
- There are many disadvantages of Round-robin Scheduling. Some of the disadvantages are mentioned below;
- Round-robin Scheduling utilize more time on context switching which is not good in some cases.
- The processor output will be reduced in Round-robin Scheduling, If slicing time of OS is low.
- If the time quantum is low, then it increases the context switching time in Round-robin Scheduling .
- Round-robin Scheduling can leads to decreases the comprehension
- In Round-robin Scheduling , it’s a difficult task to decide a correct time quantum to increase the efficiency and speed.
- In Round-robin Scheduling , We can’t set priorities for the processes.
- Performance of Round-robin Scheduling heavily depends on time quantum given to the processes.
Round robin scheduling program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<stdio.h> int main() { int T4Tutorials_counter,j,n,time,Remaining,flag=0,time_quantum; int wait_time=0,turnaround_time=0,Arrival_Time[10],Burst_Time[10],rt[10]; printf("Enter Total Process:\t "); scanf("%d",&n); Remaining=n; for(T4Tutorials_counter=0;T4Tutorials_counter<n;T4Tutorials_counter++) { printf("Enter Arrival Time and Burst Time for Process Process Number %d :",T4Tutorials_counter+1); scanf("%d",&Arrival_Time[T4Tutorials_counter]); scanf("%d",&Burst_Time[T4Tutorials_counter]); rt[T4Tutorials_counter]=Burst_Time[T4Tutorials_counter]; } printf("Enter Time Quantum:\t"); scanf("%d",&time_quantum); printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n"); for(time=0,T4Tutorials_counter=0;Remaining!=0;) { if(rt[T4Tutorials_counter]<=time_quantum && rt[T4Tutorials_counter]>0) { time+=rt[T4Tutorials_counter]; rt[T4Tutorials_counter]=0; flag=1; } else if(rt[T4Tutorials_counter]>0) { rt[T4Tutorials_counter]-=time_quantum; time+=time_quantum; } if(rt[T4Tutorials_counter]==0 && flag==1) { Remaining--; printf("P[%d]\t|\t%d\t|\t%d\n",T4Tutorials_counter+1,time-Arrival_Time[T4Tutorials_counter],time-Arrival_Time[T4Tutorials_counter]-Burst_Time[T4Tutorials_counter]); wait_time+=time-Arrival_Time[T4Tutorials_counter]-Burst_Time[T4Tutorials_counter]; turnaround_time+=time-Arrival_Time[T4Tutorials_counter]; flag=0; } if(T4Tutorials_counter==n-1) T4Tutorials_counter=0; else if(Arrival_Time[T4Tutorials_counter+1]<=time) T4Tutorials_counter++; else T4Tutorials_counter=0; } printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); return 0; } |
Output
Round robin scheduling program in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <iostream> #include <vector> using namespace std; int main() { int T4Tutorials_counter, j, n, time, Remaining, flag =
0, time_quantum; int wait_time = 0, turnaround_time = 0; cout << "Enter Total Process:\t "; cin >> n; Remaining = n; vector<int> Arrival_Time(n), Burst_Time(n), rt(n); for (T4Tutorials_counter = 0; T4Tutorials_counter < n; T4Tutorials_counter++) { cout << "Enter Arrival Time and Burst Time for Process Process Number " << T4Tutorials_counter + 1 << ": "; cin >> Arrival_Time[T4Tutorials_counter]; cin >> Burst_Time[T4Tutorials_counter]; rt[T4Tutorials_counter] = Burst_Time[T4Tutorials_counter]; } cout << "Enter Time Quantum:\t"; cin >> time_quantum; cout << "\n\nProcess\t|Turnaround Time|Waiting Time\n\n"; for (time = 0, T4Tutorials_counter = 0; Remaining != 0;) { if (rt[T4Tutorials_counter] <= time_quantum && rt[T4Tutorials_counter] > 0) { time += rt[T4Tutorials_counter]; rt[T4Tutorials_counter] = 0; flag = 1; } else if (rt[T4Tutorials_counter] > 0) { rt[T4Tutorials_counter] -= time_quantum; time += time_quantum; } if (rt[T4Tutorials_counter] == 0 && flag == 1) { Remaining--; cout << "P[" << T4Tutorials_counter + 1 << "]\t|\t" << time - Arrival_Time[T4Tutorials_counter] << "\t|\t" << time - Arrival_Time[T4Tutorials_counter] - Burst_Time[
T4Tutorials_counter] << endl; wait_time += time - Arrival_Time[T4Tutorials_counter] - Burst_Time[T4Tutorials_counter]; turnaround_time += time - Arrival_Time[T4Tutorials_counter]; flag = 0; } if (T4Tutorials_counter == n - 1) T4Tutorials_counter = 0; else if (Arrival_Time[T4Tutorials_counter + 1] <= time) T4Tutorials_counter++; else T4Tutorials_counter = 0; } cout << "\nAverage Waiting Time= " << (float)wait_time / n << endl; cout << "Avg Turnaround Time = " << (float)turnaround_time / n << endl; return 0; } |
Round Robin Implementation in Java
Let us see the Round Robin Implementation in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import java.util.*; class ROUND_ROBIN_SCHEDULING { Scanner sc=new Scanner(System.in); int[] bur, wai,ta, extra_variable,; int t=0; int flag=0; int total_size,q; int b=0; ROUND_ROBIN_SCHEDULING(int total_size) { this.total_size=total_size; ta=new int[total_size]; extra_variable=new int[total_size]; bur=new int[total_size]; wai=new int[total_size]; } void get() { for(int i=0;i<total_size;i++) { System.out.print("Enter burst time of P"+(i+1)+":"); bur[i]=extra_variable[i]=sc.nextInt(); } System.out.print("Enter quantum time:"); q=sc.nextInt(); } void round() { do{ flag=0; for(int i=0;i<total_size;i++) { if(extra_variable[i]>=q) { System.out.print("P"+(i+1)+"\t"); for(int j=0;j<total_size;j++) { if(j==i) extra_variable[i]=extra_variable[i]-q; else if(extra_variable[j]>0) wai[j]+=q; } } else if(extra_variable[i]>0) { System.out.print("P"+(i+1)+"\t"); for(int j=0;j<total_size;j++) { if(j==i) extra_variable[i]=0; else if(extra_variable[j]>0) wai[j]+=extra_variable[i]; } } } for(int i=0;i<total_size;i++) if(extra_variable[i]>0) flag=1; }while(flag==1); for(int i=0;i<total_size;i++) ta[i]=wai[i]+bur[i]; } void display() { System.out.println("\nProcess\tBurst\tWaiting\tTurnaround"); for(int i=0;i<total_size;i++) { System.out.println("P"+(i+1)+"\t"+bur[i]+"\t"+wai[i]+"\t"+ta[i]); b+=wai[i]; t+=ta[i]; } System.out.println("Average waiting time is?"+(b/total_size)); System.out.println("Average Turnaround time is?"+(t/total_size)); } } class KROUND_ROBIN_SCHEDULING { public static void main(String arg[]) { Scanner s=new Scanner(System.in); System.out.print("Please Enter the total no of process:"); int n=s.nextInt(); ROUND_ROBIN_SCHEDULING obj = new ROUND_ROBIN_SCHEDULING(n); obj.get(); obj.round(); obj.display(); } } |
Exercise on Round Robin Process Scheduling algorithm with Solution