Question: You are to complete the Laboratory Exercise 7.1: Analyzing the Round Robin Scheduling'' on page 283 of the textbook. Read the description there for a
You are to complete the Laboratory Exercise 7.1: Analyzing the Round Robin Scheduling'' on page 283 of the textbook. Read the description there for a detailed discussion. Note that you do not need to create the simulator environment as mentioned in the text, just simulate the round robin scheduler.
//---------------------------------------------------------------test.txt----------------------------------------------------------------------------------------------------- //test data
0 9.571929 1 25.650548 22 8.560047 23 26.134516 44 42.415887 105 20.118175 110 30.165473 145 20.137875 183 35.208735 188 23.002376 290 17.159267 320 27.688058 355 5.973565 370 14.135837 385 156.461809 397 26.506563 407 19.368534 454 37.669534 496 49.926830 514 9.579488 523 2.843379 533 15.042583 654 12.908772 689 84.755662 749 10.718434 849 57.787122 851 7.732450 860 100.359647 871 0.404646 942 111.339487 966 43.723954 983 56.291714 984 0.465955 992 10.417631 1020 82.211013 1118 28.728950 1180 26.885525 1244 26.570872 1258 31.370536 1263 28.010057 1272 22.249149 1278 56.369535 1331 18.321876 1335 28.288497 1337 18.391092 1361 28.304957 1404 27.819683 1437 15.494933 1455 32.007682 1495 34.221506 1516 18.497760 1522 31.399206 1551 3.511068 1560 7.167934 1579 11.980034 1595 5.763140 1605 1.402882 .... ........ //--------------------------current code-----------------------------------------------------------------
//Feel free to start over or fix current code. //Code won't display correctly, trouble making it read in doubles
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void findWaitingTime(int processes[], int n, float bt[], int wt[], int quantum)
{
float rem_bt[] = new float[n];
for (int i = 0 ; i < n ; i++)
{
rem_bt[i] = bt[i];
}
// Current Time
int t = 0;
while(true)
{
boolean done = true;
//all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false; // There is a pending process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = (int) (t + rem_bt[i]);
// Waiting time is current time minus time
// used by this process
wt[i] = (int) (t - bt[i]);
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == true)
break;
}
}
// Method to calculate turn around time
static void findTurnAroundTime(int processes[], int n,
float bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = (int) (bt[i] + wt[i]);
}
// Method to calculate average time
static void findavgTime(int processes[], int n, float bt[],
int quantum)
{
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
System.out.println("Processes " + " Burst time " +
" Waiting time " + " Turn around time");
// Calculate total waiting time and total turn
// around time
for (int i=0; i { total_wt = total_wt + wt[i]; total_tat = total_tat + tat[i]; System.out.println(" " + (i+1) + "\t\t" + bt[i] +"\t " + wt[i] +"\t\t " + tat[i]); } System.out.println("Average waiting time = " + (float)total_wt / (float)n); System.out.println("Average turn around time = " + (float)total_tat / (float)n); } // Driver Method public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(new File("test.txt")); ArrayList ArrayList ArrayList while (scanner.hasNext()) { String data = scanner.nextLine(); allData.add(data); //System.out.println(data); } int size = allData.size(); String[] arr1 = new String[size]; String[] arr2 = new String[size]; for(int i = 0; i < size; i++) { String[] temp = allData.get(i).split("\\s+"); //processData.add(temp[0]); //burstData.add(temp[1]); arr1[i] = temp[0]; arr2[i] = temp[1]; } int n = processData.size(); int[] processData1 = Arrays.stream(arr1).mapToInt(Integer::parseInt).toArray(); float[] burstData1 = new float[size]; for (int i = 0; i < size; ++i) { burstData1[i] = Float.parseFloat(arr2[i]); //System.out.println(burstData1[i]); } // process id's //int processData1[] = { 1, 2, 3}; //int n = processes.length; // Burst time of all processes //float burstData1[] = {10, 8, 12}; // Time quantum int quantum = 500; findavgTime(processData1, n, burstData1, quantum); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
