IMPORTANT : Please use Process also do not use Shortest Remain time for the firs
ID: 3721785 • Letter: I
Question
IMPORTANT : Please use Process also do not use Shortest Remain time for the first job. MOST IMPORTANTLY the data need to be entered manually via a prompt in the console similar to the image below and CANNOT BE HARDCODED. I'm a beginner in JAVA: The Process Scheduler relies on a scheduling algorithm, based on a specific scheduling policy, to allocate the CPU in the best way to move jobs through the system efficiently. Write a Java program that will simulate the following process scheduling algorithm: 3. Shortest Remaining Time (SRT) Your Java program should produce tables to presents computed results for each scheduling algorithm as shown in the following sample data for the validation your program’s computed results for each scheduling algorithm.
C) Timeline for Shortest Remaining Time (SRT) AIB 10 14 20 Arrival CPU Start Finish Time Time Turnaround Time = Finish Time Arrival Time Job Time Cycle T 20 20-0- 20 14 14 1 13 5 15-2=3 10 110-3-7 7 7-4 3 5 4 2 Average Tumaround Time (20+ 13 37+3)/5 46/5 9.2Explanation / Answer
package com.src;
import java.io.*;
public class SRTF {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Please enter the number of Processes: ");
n = Integer.parseInt(br.readLine());
int proc[][] = new int[n + 1][4];//proc[][0] is the AT array,[][1] - RT,[][2] - WT,[][3] - TT
for(int i = 1; i <= n; i++)
{
System.out.println("Please enter the Arrival Time for Process " + (char)(i + 64) + ": ");
proc[i][0] = Integer.parseInt(br.readLine());
System.out.println("Please enter the Burst Time for Process " + (char)(i + 64) + ": ");
proc[i][1] = Integer.parseInt(br.readLine());
}
System.out.println();
//Calculation of Total Time and Initialization of Time Chart array
int total_time = 0;
for(int i = 1; i <= n; i++)
{
total_time += proc[i][1];
}
int time_chart[] = new int[total_time];
for(int i = 0; i < total_time; i++)
{
//Selection of shortest process which has arrived
int sel_proc = 0;
int min = 99999;
for(int j = 1; j <= n; j++)
{
if(proc[j][0] <= i)//Condition to check if Process has arrived
{
if(proc[j][1] < min && proc[j][1] != 0)
{
min = proc[j][1];
sel_proc = j;
}
}
}
//Assign selected process to current time in the Chart
time_chart[i] = sel_proc;
//Decrement Remaining Time of selected process by 1 since it has been assigned the CPU for 1 unit of time
proc[sel_proc][1]--;
//WT and TT Calculation
for(int j = 1; j <= n; j++)
{
if(proc[j][0] <= i)
{
if(proc[j][1] != 0)
{
proc[j][3]++;//If process has arrived and it has not already completed execution its TT is incremented by 1
if(j != sel_proc)//If the process has not been currently assigned the CPU and has arrived its WT is incremented by 1
proc[j][2]++;
}
else if(j == sel_proc)//This is a special case in which the process has been assigned CPU and has completed its execution
proc[j][3]++;
}
}
//Printing the Time Chart
if(i != 0)
{
if(sel_proc != time_chart[i - 1])
//If the CPU has been assigned to a different Process we need to print the current value of time and the name of
//the new Process
{
System.out.print("--" + (char)(i + 64) + "--" + sel_proc);
}
}
else//If the current time is 0 i.e the printing has just started we need to print the name of the First selected Process
System.out.print(i + "--P" + sel_proc);
if(i == total_time - 1)//All the process names have been printed now we have to print the time at which execution ends
System.out.print("--" + ((char)(i + 1 + 64)));
}
System.out.println();
System.out.println();
//Printing the WT and TT for each Process
System.out.println("P WT TT ");
for(int i = 1; i <= n; i++)
{
System.out.printf("%s %2dms %2dms",(char)(i + 64),proc[i][2],proc[i][3]);
System.out.println();
}
System.out.println();
//Printing the average WT & TT
float WT = 0,TT = 0;
for(int i = 1; i <= n; i++)
{
WT += proc[i][2];
TT += proc[i][3];
}
WT /= n;
TT /= n;
System.out.println("The Average WT is: " + WT + "ms");
System.out.println("The Average TT is: " + TT + "ms");
}
}
//output:
Please enter the number of Processes:
5
Please enter the Arrival Time for Process A:
0
Please enter the Burst Time for Process A:
7
Please enter the Arrival Time for Process B:
1
Please enter the Burst Time for Process B:
5
Please enter the Arrival Time for Process C:
2
Please enter the Burst Time for Process C:
3
Please enter the Arrival Time for Process D:
3
Please enter the Burst Time for Process D:
3
Please enter the Arrival Time for Process E:
4
Please enter the Burst Time for Process E:
2
0--P1--A--2--B--3--E--5--G--4--J--2--N--1--T
P WT TT
A 13ms 20ms
B 8ms 13ms
C 0ms 3ms
D 4ms 7ms
E 1ms 3ms
The Average WT is: 5.2ms
The Average TT is: 9.2ms