3rd (FCFS)
3rd (FCFS)
Aim- List of processes / jobs along with their arrival times & CPU burst times is given.
Write a program to print the total waiting time, average waiting time, total turnaround time,
average turnaround time & Gantt Chart using First Come First Serve (FCFS) CPU
scheduling policy.
First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to
their arrival time. The job which comes first in the ready queue will get the CPU first. The
lesser the arrival time of the job, the sooner will the job get the CPU. FCFS scheduling may
cause the problem of starvation if the burst time of the first process is the longest among all
the jobs.
Code-
import java.util.*;
class Process {
int arrival_time;
int burst_time;
int start_time;
int finish_time;
int wait_time;
int turnaround_time;
int current_time = 0;
int total_wait_time = 0;
int total_turnaround_time = 0;
System.out.println("Gantt Chart:");
System.out.println("0");
for (int i = 0; i < n; i++) {
Process process = processes.get(i);
if (current_time < process.arrival_time) {
current_time = process.arrival_time;
System.out.print(" - " + current_time);
}
else {
System.out.print(" - ");
}
process.start_time = current_time;
current_time += process.burst_time;
process.finish_time = current_time;
process.turnaround_time = process.finish_time - process.arrival_time;
process.wait_time = process.turnaround_time - process.burst_time;
total_wait_time += process.wait_time;
total_turnaround_time += process.turnaround_time;
System.out.print("P" + (i+1) + " " + process.finish_time);
}
System.out.println();