Help adding calculation of average response time and throughout put(run time of
ID: 3751676 • Letter: H
Question
Help adding calculation of average response time and throughout put(run time of the program) in this Round Robin algorithm program written in Java
import java.util.Random;
import java.util.Scanner;
public class RoundRobinAlgorithm {
public static void main(String[] args)
{
//create object to scanner class to read input from user
Scanner input = new Scanner(System.in);
//create object for random class to generate random numbers
Random r = new Random();
//variable declarations
int i, n, time, remain, flag = 0, ts;
int sum_wait = 0, sum_turnaround = 0;
int[] at = new int[10];
int[] bt = new int[10];
int[] rt = new int[10];
//prompt and read the number of processes
System.out.print("Enter no of Processes : ");
n = input.nextInt();
//set n to remind
remain = n;
//run a loop to read the arrival and burst time of all the processes
for (i = 0; i < n; i++)
{
//generate arrival time and burst time randomly
//to generate random number within a specific range use r.nextInt((maxValue - minValue) + 1) + minValue
at[i] = r.nextInt((25 - 1) + 1) + 1; // generates a number between 1 and 25
bt[i] = r.nextInt((15 - 10) + 1) + 10; // generates a number between 10 and 15
rt[i] = bt[i];
}
//prompt and read time slice
System.out.print("Enter time slice: ");
ts = input.nextInt();
//proceed with the computations and display the results
System.out.print(" Process |Turnaround time|waiting time ");
//run a loop to check which process has to be executed first
for (time = 0, i = 0; remain != 0;)
{
if (rt[i] <= ts && rt[i] > 0)
{
time += rt[i];
rt[i] = 0;
flag = 1;
}
else if (rt[i] > 0)
{
rt[i] -= ts;
time += ts;
}
if (rt[i] == 0 && flag == 1)
{
remain--;
System.out.printf("P[%d] | %d | %d ", i + 1, time - at[i], time - at[i] - bt[i]);
sum_wait += time - at[i] - bt[i];
sum_turnaround += time - at[i];
flag = 0;
}
if (i == n - 1)
i = 0;
else if (at[i + 1] <= time)
i++;
else
i = 0;
}
//display the average waiting time and average turn around times
System.out.printf(" Avg sum_wait = %f ", sum_wait * 1.0 / n);
System.out.printf("Avg sum_turnaround = %f", sum_turnaround * 1.0 / n);
input.close();
}
}
Explanation / Answer
Completion Time: Time at which process finishes its execution.
Pivot Time: Time Difference between fulfillment time and landing time. Pivot Time = Completion Time – Arrival Time
Holding up Time(W.T): Time Difference between pivot time and burst time.
Holding up Time = Turn Around Time – Burst Time
In this post, we have accepted landing times as 0, so pivot and fruition times are same.
The precarious part is to figure holding up times. When holding up times are processed, pivot times can be immediately registered.
Ventures to discover holding up times all things considered:
1-Create a cluster rem_bt[] to monitor remaining
burst time of procedures. This cluster is at first a
duplicate of bt[] (burst times cluster)
2-Create another exhibit wt[] to store holding up times
of procedures. Introduce this exhibit as 0.
3-Initialize time : t = 0
4-Keep navigating the all procedures while all procedures
are not done. Do following for i'th process in the event that it is
not done yet.
an If rem_bt[i] > quantum
(I) t = t + quantum
(ii) bt_rem[i] - = quantum;
c-Else/Last cycle for this procedure
(I) t = t + bt_rem[i];
(ii) wt[i] = t - bt[i]
(ii) bt_rem[i] = 0;/This procedure is finished
When we have holding up times, we can register pivot time tat[i] of a procedure as total of pausing and burst times, i.e., wt[i] + bt[i]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.