# Java Program for First Come First Serve (FCFS) Scheduling Algorithm

In this article we are going to learn about first come first serve (fcfs) scheduling in Java with program example. FCFS strategy will be helpful in many situations especially in tie breaking situations. Here we will see FCFS strategy with scheduling problem.

### First Come First Serve (FCFS) Scheduling

First come First serve means whatever the job came first we should process that job first regardless other properties. This situation we can map with our real time scenario. When we are in queue for movie tickets, whoever the person first entered into queue will get the ticket first. Second person will be severed second only. Same strategy will be applied on scheduling also.

In our context we are talking about job scheduling problem. As we know one processor may loaded with many jobs. Scheduler will do the scheduling job. If scheduler takes FCFS strategy then whichever the process arrived first that job will be scheduled on processor to be processed. Once we allocate processor to the job we can’t stop that processing until job is completed. This is called non-preemptive scheduling. If we are able to stop then it is called preemptive scheduling. Best scheduling algorithms will minimize the average waiting time, turnaround time.

#### Example:

Arrival time: The time when process came for scheduling.

Burst time: Time needed to execute the job.

If we apply FCFS scheduling on these jobs then P0 came first. So first we will schedule P0. After completion of P0 we will see for next job. P0 will take 9ms till then P1,P2 both jobs had come but we will schedule P1 because it arrived earlier than P2. After completion of P1 we will schedule P2.

Gantt Chart:

Waiting Times:

 Process Waiting time = first scheduled time – arrival time (ms) P0 0-0 = 0 P1 9-1= 8 P2 13-2 =11

Average waiting time is (0+8+11)/3 = 6.33ms

Turnaround Times:

 Process Turnaround Time = execution time + waiting time P0 0+9=9 P1 8+4=12 P2 11+9=20

Average turnaround time = (9+12+20)/3 = 13.66ms

## Java Program for First Come First Serve (FCFS) Scheduling Algorithm

```import java.util.*;

public class FCFS {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter no of process: ");
int n = sc.nextInt();
int pid[] = new int[n];   // process ids
int ar[] = new int[n];     // arrival times
int bt[] = new int[n];     // burst or execution times
int ct[] = new int[n];     // completion times
int ta[] = new int[n];     // turn around times
int wt[] = new int[n];     // waiting times
int temp;
float avgwt=0,avgta=0;

for(int i = 0; i < n; i++)
{
System.out.println("enter process " + (i+1) + " arrival time: ");
ar[i] = sc.nextInt();
System.out.println("enter process " + (i+1) + " brust time: ");
bt[i] = sc.nextInt();
pid[i] = i+1;
}

//sorting according to arrival times
for(int i = 0 ; i <n; i++)
{
for(int  j=0;  j < n-(i+1) ; j++)
{
if( ar[j] > ar[j+1] )
{
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
}
}
}

// finding completion times
for(int  i = 0 ; i < n; i++)
{
if( i == 0)
{
ct[i] = ar[i] + bt[i];
}
else
{
if( ar[i] > ct[i-1])
{
ct[i] = ar[i] + bt[i];
}
else
ct[i] = ct[i-1] + bt[i];
}
ta[i] = ct[i] - ar[i] ;          // turnaround time= completion time- arrival time
wt[i] = ta[i] - bt[i] ;          // waiting time= turnaround time- burst time
avgwt += wt[i] ;               // total waiting time
avgta += ta[i] ;               // total turnaround time
}

System.out.println("\npid  arrival  brust  complete turn waiting");
for(int  i = 0 ; i< n;  i++)
{
System.out.println(pid[i] + "  \t " + ar[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + ta[i] + "\t"  + wt[i] ) ;
}
sc.close();
System.out.println("\naverage waiting time: "+ (avgwt/n));     // printing average waiting time.
System.out.println("average turnaround time:"+(avgta/n));    // printing average turnaround time.
}
}```

Output

1. Simple to implement
2. Simple to understand
3. First come first serve will be used in tie breaking situations
4. Fairness in treating