SPOS - Round Robin Program

Round Robin Program

Code :

import java.util.Scanner;
public class RoundRobin {
 public static boolean allZeros(int[][] processes) {
 for (int[] data: processes) {
 if (data[1] != 0) {
 return false;
 }
 }
 return true;
 }
 public static float calcAvg(int[] arr) {
 int sum = 0;
 for (int j : arr) {
 sum += j;
 }
 return (float) sum/arr.length;
 }
 public static void main(String[] args) {
 Scanner myScanner = new Scanner(System.in);
 System.out.print("Enter total number of processes: ");
 int totalNumProcesses = myScanner.nextInt();
 System.out.print("Enter the time quantum: ");
 int timeQuantum = myScanner.nextInt();
 int[][] processes = new int[totalNumProcesses][2];
 int[] burstTimes = new int[totalNumProcesses];
 int[] waitingTimes = new int[totalNumProcesses];
 int[] turnAroundTimes = new int[totalNumProcesses];
 for (int i = 0; i < totalNumProcesses; i++) {
 System.out.print("Enter Process id: ");
 processes[i][0] = myScanner.nextInt();
 System.out.print("Enter Burst Time: ");
 processes[i][1] = myScanner.nextInt();
 burstTimes[i] = processes[i][1];
 }
 boolean isDone;
 int idx = 0;
 while(true) {
 int wt = 0;
 if (processes[idx][1] < timeQuantum) {
 wt = processes[idx][1];
 processes[idx][1] = 0;
 } else {
 wt = timeQuantum;
 processes[idx][1] -= wt;
 }
 for (int i = 0; i < processes.length; i++) {
 if (i != idx && processes[i][1] != 0) {
 waitingTimes[i] += wt;
 }
 }
 isDone = allZeros(processes);
 if (isDone) {
 break;
 }
 idx++;
 idx %= totalNumProcesses;
 if (processes[idx][1] == 0) {
 while(processes[idx][1] != 0) {
 idx++;
 idx%=totalNumProcesses;
 }
 }
 }
 for (int i = 0; i < processes.length; i++) {
 turnAroundTimes[i] = waitingTimes[i] + burstTimes[i];
 }
 float avgWaitingTime = calcAvg(waitingTimes);
 float avgTurnAroundTime = calcAvg(turnAroundTimes);
 System.out.format("%4s%4s%4s%4s\n", "pid", "bt", "wt", "tat");
 for (int i = 0; i < totalNumProcesses; i++) {
 System.out.format("%4d%4d%4d%4d\n", processes[i][0], burstTimes[i], waitingTimes[i],
turnAroundTimes[i]);
 }
 System.out.println("The average waiting time is: " + avgWaitingTime);
 System.out.println("The average turnaround time is: " + avgTurnAroundTime);
 }
}

Output :



Your review for this post?