Problem Definition:Write a JAVA Program to implement Round Robin CPU Scheduling
Algorithm
Problem Description :
Round robin is a pre-emptive algorithm
The CPU is shifted to the next process after fixed interval time, which is called time
quantum/time slice.
The process that is preempted is added to the end of the queue.
Round robin is a hybrid model which is clock-driven
Time slice should be minimum, which is assigned for a specific task that needs to be
processed. However, it may differ OS to OS.
It is a real time algorithm which responds to the event within a specific time limit.
Round robin is one of the oldest, fairest, and easiest algorithm.
Widely used scheduling method in traditional OS.
Let us now cover an example for the same:
In the above diagram, arrival time is not mentioned so it is taken as 0 for all processes.
Note: If arrival time is not given for any problem statement then it is taken as 0 for all
processes; if it is given then the problem can be solved accordingly.
Explanation
The value of time quantum in the above example is 5.Let us now calculate the Turn
around time and waiting time for the above example :
Turn Around Time
Waiting Time
Processes Burst Time Turn Around Time =
Waiting Time = Turn Around
Completion Time – Arrival
Time – Burst Time
Time
P1 21 32-0=32 32-21=11
P2 3 8-0=8 8-3=5
P3 6 21-0=21 21-6=15
P4 2 15-0=15 15-2=13
Average waiting time is calculated by adding the waiting time of all processes and then
dividing them by no.of processes.
average waiting time = waiting time of all processes/ no.of processes
average waiting time=11+5+15+13/4 = 44/4= 11ms
import java.util.*;
public class RoundRobin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, tq, time = 0, total_wt = 0, total_tat = 0;
System.out.print("Enter the number of processes: ");
n = sc.nextInt();
int[] bt = new int[n];
int[] wt = new int[n];
int[] tat = new int[n];
int[] rem_bt = new int[n];
System.out.print("Enter the burst time of each process: ");
for (int i = 0; i < n; i++) {
bt[i] = sc.nextInt();
rem_bt[i] = bt[i];
System.out.print("Enter the time quantum: ");
tq = sc.nextInt();
while (true) {
boolean done = true;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false;
if (rem_bt[i] > tq) {
time += tq;
rem_bt[i] -= tq;
} else {
time += rem_bt[i];
wt[i] = time - bt[i];
rem_bt[i] = 0;
if (done) break;
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total_wt += wt[i];
total_tat += tat[i];
System.out.println("Process\tBurst Time\tWaiting Time\tTurnaround Time");
for (int i = 0; i < n; i++) {
System.out.println((i+1) + "\t" + bt[i] + "\t\t" + wt[i] + "\t\t" + tat[i]);
System.out.println("Average waiting time: " + (float)total_wt / n);
System.out.println("Average turnaround time: " + (float)total_tat / n);
/* Input/Output :
D:\>javac RoundRobin.java
D:\>java RoundRobin
Enter the number of processes: 5
Enter the burst time of each process: 10 1 2 1 5
Enter the time quantum: 1
Process Burst Time Waiting Time Turnaround Time
1 10 9 19
2 1 1 2
3 2 5 7
4 1 3 4
5 5 9 14
Average waiting time: 5.4
Average turnaround time: 9.2 */