PSYCHOCODES

First Come First Serve(FCFS) Program in Java

A processor handles multiple processes and assigns them to CPU on the basis of a particular algorithm which is known as a scheduling algorithm.

In this tutorial, we are going to see a popular scheduling algorithm known as FIRST COME FIRST SERVE algorithm but before studying the algorithm let's take a look at some important terms which we are going to use in this tutorial.

  • Bursts/Service time: When a process gets executed it gets a slot of time during which it gets executed that slot is known as Burst.
  • Arrival time:  Arrival time is nothing but the time at which the process will arrive for execution(it is not in seconds or minutes).Arrival time just gives the idea about which process is going to be executed first and which process going to be executed last.
  • Waiting time:  It is the time in which other processes are waiting for execution.
  • Turnaround Time: It is the total time taken between the submission of the process and the complete output of the process.
  • Completion time: It is the total time taken by the process to complete its execution.

FCFS have a simple working rule the process which comes first will be executed first and then the second and the third, this algorithm is easy to understand and implement.

Formulas for calculating Waiting time total time and turnaround time

turnaroundTime = completionTime - arrivalTime
waitingTime = turnaroundTime - bursts

Java Program for FCFS

import java.util.Scanner;

class Process{
    int wait;
    int submission;
    int bursts;
	int turnAround;
	int completionTime = 0;
    Process(int sub,int bur){
        submission = sub;
        bursts = bur;
    }

}
class Processmain{
    public static void main(String[] args){
        int wait = 0,x=0;
		Scanner s = new Scanner(System.in);
		System.out.println("Enter the number of processes:");
		int n = s.nextInt();
        Process[] myProcess = new Process[n];
		for(int i=0;i<n;i++){
			System.out.println("Enter Arrival time and bursts: ");
			int sub  = s.nextInt();
			int bur = s.nextInt();
			myProcess[i] = new Process(sub,bur);
		}
        for(int i=0;i<myProcess.length;i++){
            x = x+myProcess[i].bursts;
			myProcess[i].completionTime = x;
			myProcess[i].turnAround = myProcess[i].completionTime - myProcess[i].submission;
			myProcess[i].wait = myProcess[i].turnAround - myProcess[i].bursts;
			System.out.println("Process "+i+":");
			System.out.println("Turnaround\tCompletion\twaiting");
			System.out.println(myProcess[i].turnAround+"\t\t\t"+myProcess[i].completionTime+"\t\t\t"+myProcess[i].wait);
        }
    }
}

The output of the above code:

FCFS program in Java

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Share your thoughts