Adding average response time calculation and throughout put of Round robin sched
ID: 3751780 • Letter: A
Question
Adding average response time calculation and throughout put of Round robin scheduler algorithm
import java.awt.image.BufferStrategy;
import java.util.Random;
import java.util.Scanner;
import java.util.Scanner.*;
public class RoundRobin {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int quantum, sum = 0;
float average_waitingtime = 0;
float average_turnaroundtime = 0;
System.out.println(" ------------------------------------------");
System.out.println(" ** Round robin CPU Scheduling Algorithm **");
System.out.println(" ------------------------------------------");
System.out.println("Enter number of process:");
int n = sc.nextInt();
int burst_time[] = new int[n];
int waiting_time[] = new int[n];
int turnaround_time[] = new int[n];
int array[] = new int[n];
// System.out.println(" Please Enter brust Time:");
// for (int i = 0; i < n; i++) {
// System.out.println(" Please Enter brust Time for process: " + (i + 1));
// burst_time[i] = sc.nextInt();
// }
Random rand = new Random();
for(int i = 0; i< n ; i++) {
burst_time[i] = rand.nextInt(25);
}
System.out.println(" Please Enter Time quantum:");
quantum = sc.nextInt();
for (int i = 0; i < n; i++)
array[i] = burst_time[i];
for (int i = 0; i < n; i++)
waiting_time[i] = 0;
do {
for (int i = 0; i < n; i++) {
if (burst_time[i] > quantum) {
burst_time[i] -= quantum;
for (int j = 0; j < n; j++) {
if ((j != i) && (burst_time[j] != 0))
waiting_time[j] += quantum;
}
} else {
for (int j = 0; j < n; j++) {
if ((j != i) && (burst_time[j] != 0))
waiting_time[j] += burst_time[i];
}
burst_time[i] = 0;
}
}
sum = 0;
for (int k = 0; k < n; k++)
sum = sum + burst_time[k];
} while (sum != 0);
for (int i = 0; i < n; i++)
turnaround_time[i] = waiting_time[i] + array[i];
System.out.println(" process BurstTime WaitingTime TurnAroundTime");
for (int i = 0; i < n; i++) {
System.out.println(
"process" + (i + 1) + " " + array[i] + " " + waiting_time[i] + " " + turnaround_time[i]);
}
for (int j = 0; j < n; j++) {
average_waitingtime += waiting_time[j];
}
for (int j = 0; j < n; j++) {
average_turnaroundtime += turnaround_time[j];
}
System.out.println(" The average waiting time is:" + (average_waitingtime / n));
System.out.println(" The Average turn around time is: " + (average_turnaroundtime / n));
}
}
Explanation / Answer
package OS;
import java.awt.image.BufferStrategy;
import java.util.Random;
import java.util.Scanner;
import java.util.Scanner.*;
// Class RoundRobin definition
public class RoundRobin
{
// main method definition
public static void main(String args[])
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// To store time quantum and sum
int quantum, sum = 0;
// To store average waiting time
float average_waitingtime = 0;
// To store average turn around time
float average_turnaroundtime = 0;
// Displays heading
System.out.println(" ------------------------------------------");
System.out.println(" ** Round robin CPU Scheduling Algorithm **");
System.out.println(" ------------------------------------------");
System.out.println("Enter number of process:");
// Accepts number of processes
int n = sc.nextInt();
// Dynamically allocates memory to burst time array of size n (number of process)
int burst_time[] = new int[n];
// Dynamically allocates memory to waiting time array of size n (number of process)
int waiting_time[] = new int[n];
// Dynamically allocates memory to turn around time array of size n (number of process)
int turnaround_time[] = new int[n];
// Temporary array to stoer burst time
int array[] = new int[n];
// System.out.println(" Please Enter brust Time:");
// for (int i = 0; i < n; i++) {
// System.out.println(" Please Enter brust Time for process: " + (i + 1));
// burst_time[i] = sc.nextInt();
// }
// Random class object created
Random rand = new Random();
// Loops till number of process to generate random burst time
for(int i = 0; i< n ; i++)
burst_time[i] = rand.nextInt(25);
// Accepts time quantum
System.out.println(" Please Enter Time quantum:");
quantum = sc.nextInt();
// Loops till number of process to store duplicate copy of burst time
for (int i = 0; i < n; i++)
array[i] = burst_time[i];
// Loops till number of process to assign 0 to all the process waiting time
for (int i = 0; i < n; i++)
waiting_time[i] = 0;
// Loops till sum is not equals to zero
do
{
// Loops till number of process
for (int i = 0; i < n; i++)
{
// Checks if burst time of current process is greater than the time quantum
if (burst_time[i] > quantum)
{
// Subtracts the time quantum from the burst time for the current process
burst_time[i] -= quantum;
// Loops till number of process
for (int j = 0; j < n; j++)
{
// Checks if process j is not equals to process i
// and burst time of the process j is not zero
if ((j != i) && (burst_time[j] != 0))
// Adds the time quantum to the earlier waiting time of the process j
waiting_time[j] += quantum;
}// End of inner for loop
}// End of if condition
// Otherwise burst time is less than the time quantum
else
{
// Loops till number of process
for (int j = 0; j < n; j++)
{
// Checks if current process is not equals to earlier process
// and burst time of the current process is not zero
if ((j != i) && (burst_time[j] != 0))
// Adds the burst time of process i to the earlier waiting time of the process j
waiting_time[j] += burst_time[i];
}// End of for loop
// Assigns burst time of process i to zero
burst_time[i] = 0;
}// End of else
}// End of outer for loop
// Re initializes the sum to zero
sum = 0;
// Loops till number of process
for (int k = 0; k < n; k++)
// Adds the previous sum with each process burst time
sum = sum + burst_time[k];
} while (sum != 0); // End of do - while loop
// Loops till number of process
for (int i = 0; i < n; i++)
// Calculates the turn around time by adding waiting time of each process by burst time of each process
turnaround_time[i] = waiting_time[i] + array[i];
// Displays heading
System.out.println(" process BurstTime WaitingTime TurnAroundTime");
// Loops till number of process
for (int i = 0; i < n; i++)
// Displays each process id, burst time, waiting time and turn around time
System.out.println("process" + (i + 1) + " " + array[i] + " " + waiting_time[i] + " " + turnaround_time[i]);
// Loops till number of process
for (int j = 0; j < n; j++)
// Calculates total waiting time for each process
average_waitingtime += waiting_time[j];
// Loops till number of process
for (int j = 0; j < n; j++)
// Calculates total turn around time for each process
average_turnaroundtime += turnaround_time[j];
// Creates an array to store time required for to each process
int required_time[] = new int[n];
// To store total time required
int totalRequiredTime = 0;
// To store total time quantum
// Initializes to -quantum because first response time for first process is 0
int totalResponseTime = -quantum;
// Loops till number of process
for (int j = 0; j < n; j++)
{
// Calculates required time by subtracting waiting time of each from turn around time of each process
required_time[j] = turnaround_time[j] - waiting_time[j];
// Calculates total required time
totalRequiredTime += required_time[j];
// Calculates total response time
totalResponseTime += quantum;
}// End of for loop
// Displays information
System.out.println(" The average waiting time is:" + (average_waitingtime / n));
System.out.println(" The Average turn around time is: " + (average_turnaroundtime / n));
System.out.println(" The Average response time is: " + (totalResponseTime / n));
System.out.println(" The Average throughout put is: " + (totalRequiredTime / n));
}// End of main method
}// End of class
Sample Output:
------------------------------------------
** Round robin CPU Scheduling Algorithm **
------------------------------------------
Enter number of process:
8
Please Enter Time quantum:
3
process BurstTime WaitingTime TurnAroundTime
process1 22 72 94
process2 20 72 92
process3 9 40 49
process4 19 74 93
process5 3 12 15
process6 12 57 69
process7 8 49 57
process8 1 21 22
The average waiting time is:49.625
The Average turn around time is: 61.375
The Average response time is: 2
The Average throughout put is: 11
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.