First Come First Serve Algorithm: Step1: Create the number of process. Step2: Get the ID and Service time for each process. Step3: Initially, Waiting time of first process is zero and Total time for the first process is the starting time of that process. Step4: Calculate the Total time and Processing time for the remaining processes. Step5: Waiting time of one process is the Total time of the previous process. Step6: Total time of process is calculated by adding Waiting time and Service time. Step7: Total waiting time is calculated by adding the waiting time for lack process. Step8: Total turn around time is 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 time by the 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