Untitled
unknown
java
3 years ago
5.5 kB
17
Indexable
import java.util.*;
public class SRTF {
static void getSRTF(){
Scanner in = new Scanner(System.in);
int n;
System.out.print("Enter the # of Processes: ");
n = in.nextInt(); // get num of processes of user
int myProcess[][] = new int[n + 1][4]; // initialize array to store values
int Orig_Burst_t[] = new int[n + 1]; // this array is used to store the original value of the burst Time of the processes
// myProcess[][0] = Arrival time
// myProcess[][1] = Burst time
// myProcess[][2] = Wait time
// myProcess[][3] = Total time
for(int i = 1; i <= n; i++)
{
System.out.println("\n\t[P" + i +"]");
System.out.print("Arrival Time: ");
myProcess[i][0] = in.nextInt(); // get Arrival Time
System.out.print("Burst Time: ");
myProcess[i][1] = in.nextInt(); // get Burst Time
Orig_Burst_t[i] = myProcess[i][1]; // store the original burst time for output later
}
System.out.println();
//This part is the calculation of total time and the initialization of gantt chart array
int total_t = 0; // initialize total time to 0
for(int i = 1; i <= n; i++) {
total_t += myProcess[i][1]; // sums up all the burst times of all processes
}
int gantt_chart[] = new int[total_t]; // make gantt array who's size is the total time (total_t is the sum of all burst times)
for(int i = 0; i < total_t; i++)
{
// this part will pick the shortest process that has arrived
int select_process = 0;
int minimum = 999;
for(int j = 1; j <= n; j++)
{
if(myProcess[j][0] <= i) //this condition checks if Process (arrival time) has been called
{
if(myProcess[j][1] < minimum && myProcess[j][1] != 0) // if burst time < minimum and burst time != 0 then execute
{
minimum = myProcess[j][1]; // update the minumum to the selected burst time
select_process = j; // update the select_process to the number of j selected
}
}
}
gantt_chart[i] = select_process; // add the selected_press to the respected index it belongs in the gantt chart.
myProcess[select_process][1]--; // decrement the burst time of the selected process by 1 since it had used 1 unit of time
//Waiting Time and Total Time Calculations
for(int j = 1; j <= n; j++)
{
if(myProcess[j][0] <= i) //this condition checks if Process has been called
{
if(myProcess[j][1] != 0) // if burst time != 0; execute
{
myProcess[j][3]++; // increment TotalTime by 1 if the process has been called and execution is not yet complete
if(j != select_process) // if j != the selected process increment the Waiting Time by 1
myProcess[j][2]++;
}
else if(j == select_process)// if process has been assigned CPU and has completed its execution increment the total time by 1
myProcess[j][3]++;
}
}
//Printing the gantt chart
if(i != 0){
if(select_process != gantt_chart[i - 1]){ // We must print the current time and the name of the new Process if the CPU has been given to a different Process.
System.out.print(" | " + i + " | P" + select_process);
}
}
else { //If the current time is 0, print the first selected process
System.out.print(i + " | P" + select_process);
}
if(i == total_t - 1) // by this point all processes have been called and we need to print the last value of the time of execution
System.out.print(" | " + (i + 1));
}
System.out.println();
System.out.println();
//Printing the Waiting time and Total time for each Process
System.out.println("Process\t\t\t Arrival Time \t\t\t Burst Time \t\t\t Waiting Time \t\t\t Turnaround Time ");
for(int i = 1; i <= n; i++)
{
System.out.printf("%4d\t%16d\t%19d\t%22dms\t%19dms",i,myProcess[i][0],Orig_Burst_t[i],myProcess[i][2],myProcess[i][3]);
System.out.println();
}
System.out.println();
//Printing the average Waiting time & Total time
// initalize variables to store the total waiting time & total time
float print_waitingtime = 0;
float print_totaltime = 0;
for(int i = 1; i <= n; i++){ // add all occurrences of waiting time and total time inside the variable
print_waitingtime += myProcess[i][2];
print_totaltime += myProcess[i][3];
}
// divide by number of processes that was initialized by the user (n)
print_waitingtime /= n; // print_waitingtime = print_waitingtime / n
print_totaltime /= n; // print_totaltime = print_totaltime / n
System.out.println("The Average Waiting Time is: " + print_waitingtime + "ms");
System.out.println("The Average Total Time is: " + print_totaltime + "ms");
}
}
Editor is loading...