IMPORTANT : Please use Process also do not use Shortest Remain time for the firs
ID: 3721783 • Letter: I
Question
IMPORTANT : Please use Process also do not use Shortest Remain time for the first job(only). 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.
Explanation / Answer
ANS.
Here is two class taken
1) Storing property of jobs => Process.java
2) Main class => CpuScheduling.java
public class Process {
int arivalTime,cpuCycle,waitingTime,complitionTime=-1,startTime,finishTime,turnaroundTime;
String name;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CpuScheduling {
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no of process : ");
int n=Integer.parseInt(br.readLine());
Process[] process=new Process[n];
for(int i=0;i<n;i++)
process[i]=new Process();
inputProcess(process,n);
srtf(process,n);
//calculate turnaround time=finish time - arival time
//also calculate sum of turnaround time
int sum=0;
for(int i=0;i<n;i++) {
process[i].turnaroundTime=process[i].finishTime - process[i].arivalTime;
sum+=process[i].turnaroundTime;
}
showProcess(process,n);
System.out.println(" Average Turnaround Time : "+(1.0*sum/n));
}
private static void srtf(Process[] process, int n) {
int timer=0;
boolean allJobComplete=false;
int remTime[]=new int[n];
for(int i=0;i<n;i++) {
remTime[i]=process[i].cpuCycle;
}
while(!allJobComplete) {
int job=-1;
int shortTime=9999;
//check whose remaining time is short
for(int i=0;i<n;i++) {
if(timer>=process[i].arivalTime && remTime[i]<shortTime && remTime[i]>0) {
shortTime=remTime[i];
job=i;
}
}
//job contain shortest time job
if(job!=-1) {
if(process[job].startTime==-1)
process[job].startTime=timer;
remTime[job]--;
timer++;
if(remTime[job]==0)
process[job].finishTime=timer;
System.out.print(process[job].name+" ");
}
else {
System.out.print(" - ");
timer++;
}
//check if all job complete
int flag=0;
for(int i=0;i<n;i++) {
if(remTime[i]!=0) {
flag=1;
break;
}
}
if(flag==0)
allJobComplete=true;
}
}
private static void showProcess(Process[] process, int n) {
System.out.print(" Job Arival Time Cpu Cycle Start Time Finished Time Turnaround Time");
for(int i=0;i<n;i++) {
System.out.print(" "+process[i].name+" "+process[i].arivalTime+" "+process[i].cpuCycle+" "+process[i].startTime+" "+process[i].finishTime+" "+process[i].turnaroundTime);
}
}
private static void inputProcess(Process[] process, int n) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<n;i++) {
System.out.println("Enter Job name : ");
process[i].name=br.readLine();
System.out.print("Enter arival time : ");
process[i].arivalTime=Integer.parseInt(br.readLine());
System.out.print("Enter cpu cycle : ");
process[i].cpuCycle=Integer.parseInt(br.readLine());
//initialize start time and finished time to -1
process[i].startTime=-1;
process[i].finishTime=-1;
}
}
}