In: Computer Science
Write a JAVA program that implements the following disk-scheduling algorithms:
Answer:
Program:
import java.util.*;
public class Fcfs
{
public static void main(String args[])
{
Scanner sc = new
Scanner(System.in);
System.out.print("Enter the number
of processes:");
int n = sc.nextInt();
// array for process ids
int p_id[] = new int[n];
// array for arrival times
int arr_t[] = new int[n];
// array for burst times
int burst_t[] = new int[n];
// array for completion times
int comp_t[] = new int[n];
// array for turn around
times
int tat[] = new int[n];
// array for waiting times
int wait_t[] = new int[n];
int temp;
float avg_wt=0,avg_tat=0;
for(int i = 0; i < n; i++)
{
System.out.print("Enter the process" + (i+1) + "'s arrival time:
");
arr_t[i] =
sc.nextInt();
System.out.print("Enter the process" + (i+1) + "'s brust time:
");
burst_t[i] =
sc.nextInt();
p_id[i] =
i+1;
}
//Performs sorting according to the
process arrival times
for(int i = 0 ; i <n; i++)
{
for(int j=0; j
< n-(i+1) ; j++)
{
if( arr_t[j] > arr_t[j+1] )
{
temp = arr_t[j];
arr_t[j] = arr_t[j+1];
arr_t[j+1] = temp;
temp = burst_t[j];
burst_t[j] =
burst_t[j+1];
burst_t[j+1] = temp;
temp = p_id[j];
p_id[j] = p_id[j+1];
p_id[j+1] = temp;
}
}
}
// Calculating the completion
times
for(int i = 0 ; i < n;
i++)
{
if( i ==
0)
{
comp_t[i] = arr_t[i] + burst_t[i];
}
else
{
if( arr_t[i] > comp_t[i-1])
{
comp_t[i] = arr_t[i] +
burst_t[i];
}
else
comp_t[i] = comp_t[i-1] +
burst_t[i];
}
// turnaround
time= completion time- arrival time
tat[i] =
comp_t[i] - arr_t[i] ;
// waiting time=
turnaround time- burst time
wait_t[i] =
tat[i] - burst_t[i] ;
// total waiting
time
avg_wt +=
wait_t[i] ;
// total
turnaround time
avg_tat +=
tat[i] ;
}
System.out.println("\nPID
ARRIVAL_TIME BURST_TIME COMPLETION TURN_AROUND_TIME
WAITING");
for(int i = 0 ; i< n; i++)
{
System.out.println(p_id[i] + " \t " + arr_t[i] + " \t " +
burst_t[i] + " \t " + comp_t[i] + " \t " + tat[i] + " \t " +
wait_t[i] ) ;
}
sc.close();
// printing average waiting
time
System.out.println("\nAverage
Waiting Time: "+ (avg_wt/n));
// printing average turnaround
time
System.out.println("Average
Turnaround Time:"+(avg_tat/n));
}
}
Program Screenshot(s):
Output: