Shortest Job First Algorithm: Step1:Get the number of process. Step2:Get the id and service time for each process. Step3:Initially the waiting time of first short process as 0 and total time of first short is process the service time of that process. Step4:Calculate the total time and waiting time of remaining process. Step5:Waiting time of one process is the total time of the previous process. Step6:Total time of process is calculated by adding the waiting time and service time of each process. Step7:Total waiting time calculated by adding the waiting time of each process. Step8:Total turn around time calculated by adding all total time of each process. Step9:calculate average waiting time by dividing the total waiting time by total number of process. Step10:Calculate average turn around time by dividing the total waiting time by total number of process. Step11:Display the result.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
//Step1:Get the number of process
Scanner in=new Scanner(System.in);
int num_process;
System.out.print("(Enter the number of process: ");
num_process=in.nextInt();
//Step2:Get the id and service time for each process.
String []process_id=new String[num_process];
int [] service_time=new int[num_process];
for(int i=0;i<num_process;i++){
System.out.print("Enter process id for process "+(i+1)+": ");
process_id[i]=in.next();
System.out.print("Enter service time for process "+(i+1)+": ");
service_time[i]=in.nextInt();
}
int waiting_time[]=new int[num_process];
int [] total_time=new int[num_process];
int waiting_tm=0;
for(int i=0;i<num_process;i++){
waiting_time[i]=waiting_tm;
waiting_tm=waiting_tm+service_time[i];
}
System.out.println("Process ID\tService\tWaiting\tTotal");
for(int i=0;i<num_process;i++){
System.out.println(process_id[i]+"\t"+service_time[i]+"\t"+waiting_time[i]);
}
}
}
Comments
Leave a comment