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:

fcfs java 1

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:

Java FCFS Scheduling 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

Output

fcfs java 2

Advantages:

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

Disadvantages:

  1. Convoy effect: If long process came first then all small processes should wait in the queue. It will increase the average waiting time. This effect is convoy effect.
  2. It is non-preemptive.

Comment below if you have queries related to above fcfs scheduling program in Java.

4 comments

  • Ken Hu

    The first thing comes to my mind is how do I know the burst time of each job ? Although it is another topic, but it seems no way to implement the FCFS scheduling without knowing this.

    Reply
  • saeed

    Thanks

    Reply
  • sepehr

    thank you very neat !
    could you also provide algorithms for Round Robin and HRRN please ?

    Reply
  • bhadwasur

    nhi chl rha hai bhai tera program ., bandi ke saamne bezti krwa di khamakha ki

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *