0% found this document useful (0 votes)
7 views4 pages

3rd (FCFS)

The document outlines a Java program that implements the First Come First Serve (FCFS) CPU scheduling algorithm. It calculates and displays the total waiting time, average waiting time, total turnaround time, average turnaround time, and generates a Gantt Chart based on the arrival and burst times of processes. The program sorts the processes by arrival time and computes the necessary metrics accordingly.

Uploaded by

malikirfan0502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

3rd (FCFS)

The document outlines a Java program that implements the First Come First Serve (FCFS) CPU scheduling algorithm. It calculates and displays the total waiting time, average waiting time, total turnaround time, average turnaround time, and generates a Gantt Chart based on the arrival and burst times of processes. The program sorts the processes by arrival time and computes the necessary metrics accordingly.

Uploaded by

malikirfan0502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical-03

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;

public Process(int arrival_time, int burst_time) {


this.arrival_time = arrival_time;
this.burst_time = burst_time;
this.start_time = 0;
this.finish_time = 0;
this.wait_time = 0;
this.turnaround_time = 0;
}
}

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();
List<Process> processes = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.println("Enter arrival time and burst time of process " + (i+1)
+ ": ");
int arrival_time = sc.nextInt();
int burst_time = sc.nextInt();
processes.add(new Process(arrival_time, burst_time));
}

Collections.sort(processes, Comparator.comparing(p -> p.arrival_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();

double avg_wait_time = (double)total_wait_time / n;


double avg_turnaround_time = (double)total_turnaround_time / n;

System.out.println("Total waiting time: " + total_wait_time);


System.out.println("Average waiting time: " + avg_wait_time);
System.out.println("Total turnaround time: " + total_turnaround_time);
System.out.println("Average turnaround time: " + avg_turnaround_time);
}
}
Output-

You might also like