First Come First Serve (FCFS) Scheduling

Learn via video courses
Topics Covered

Overview

When we run a program, we create a particular instance of the program called a process. There might be a condition where more than one process is created at a given time and the CPU has to serve all the processes. There are various process scheduling algorithms that decide which process has to be executed at a given time by considering various factors. FCFS or First come first serve is one such algorithm that schedules the processes.

What is First Come First Serve Scheduling?

The First come first serve scheduling algorithm is non-preemptive in nature i.e, if a process is already running, then it is not interrupted by another process until the currently running process is executed completely.

Buying a movie ticket from the ticket counter is a perfect real-life example of a first come first serve (FCFS) algorithm. The person who comes first and stands in the queue gets to buy the ticket first. Similarly in the FCFS scheduling algorithm, the process that arrives first gets executed first.

How does FCFS Scheduling Work?

Let us consider an example where 4 processes with different burst times arrive at different times.

ProcessBurst TimeArrival time
P162
P281
P330
P444

Let us see how the above processes are handled using the First come first serve scheduling algorithm.

  • At time t=0
ProcessBurst TimeArrival time
P162
P281
P330 (please add this row in different color)
P444

Queue

queue in FCFS scheduling

Gantt Chart

gantt chart in FCFS scheduling

From the above chart, we can see that at time 0, the process P3 arrives. Once the process P3 arrives, the process is served as there is no other process that is being executed.

  • At time t=1

ProcessBurst TimeArrival time
P162
P281 (please add this row in different color but same color as the one used in previous image)
P330
P444

The process P2 arrives at time t=1, but the program P3 is still executing. The process P2 will be kept in a queue which will be executed after the existing process finishes its execution. Queue queue 2 in FCFS scheduling Gantt Chart gantt chart 2 in FCFS scheduling

  • At time t=2
ProcessBurst TimeArrival time
P162 (please add this row in different color but the same color as the one used in the previous image)
P281
P330
P444

At time t=2, Process P1 arrives and waits in the queue as there is another process being executed.

Queue queue 3 in FCFS scheduling

Gantt Chart gantt chart 3 in FCFS scheduling

  • At time t=3

At time t=3, Process P3 finished execution after 3 seconds. In the queue, the process P2 is waiting for the execution Therefore process P2 begins its execution.

Queue queue 4 in FCFS scheduling Gantt Chart gantt chart 4 in FCFS scheduling

  • At time t=5
ProcessBurst TimeArrival time
P162
P281
P330
P444 (please add this row in different color but the same color as the one used in the previous image)

At time t=5, Process P4 arrives and waits in the queue.

Queue queue 5 in FCFS scheduling Gantt Chart gantt chart 5 in FCFS scheduling

  • At time t=21

In the end, at time t=21 we get a Gantt chart as shown below.

Gantt Chart

gantt chart 6 in FCFS scheduling

Characteristics of FCFS Algorithm

  • The first come first serve is a simple scheduling algorithm
  • The process that arrives first would be served first based on a first come first serve basis.
  • This method is easy to understand and implement.

Implementation of FCFS Scheduling Using a Programming Language

C++ code

Output

Explanation In the above code, we created 4 processes with the burst time of 6, 8, 3, and 4. We make a call to the avgTime function, The avgTime function calls the waitingTime and turnAroundTime functions which return the waiting time and turn around time respectively.

Example of FCFS

Billing counters in a supermarket is a real-life example of the FCFS algorithm. The first person to come to the billing counter will be served first, later the next person, and so on. The second customer will only be served if the first person's complete billing is done. Similarly in the FCFS scheduling algorithm, the process that arrives first will be completely served and later the next process waiting will be served.

Calculating Average Waiting Time

Turn around time is the amount of time taken to fulfill the request by the process. It can be calculated by taking the difference between the completion time and the arriving time.

Waiting time is the time spent by a process in the queue waiting to get executed. difference between the turn around time and the burst time. The sum of all the waiting times divided by the number of the process gives the average waiting time.

Now let us find the average waiting time for the previous example we considered.

First, let us find the Turn Around Time (T.A.T). Turn Around Time = Completion Time – Arrival Time.

ProcessBurst TimeArrival timeCompletion TimeTurn Around Time
P1621715
P2811110
P33033
P4442117

Next, let us find the waiting time. Waiting Time = Turn Around Time – Burst Time.

ProcessBurst Time (ms)Arrival Time (ms)Completion Time (ms)Turn Around Time (ms)Waiting Time (ms)
P1623159
P28111109
P3301733
P444211713

The average waiting time is calculated by taking the sum of all the waitings and dividing it by the number of processes.

9+9+3+134\frac{9+9+3+13}{4} = 8.5 ms

Therefore the average waiting is 8.5 ms.

Advantages of FCFS

  • The First come first serve process scheduling algorithm is one of the simple and easy processes scheduling algorithms.
  • The process that arrives first will be executed first.

Disadvantages of FCFS

  • The non-preemptive nature of the algorithm makes other small processes wait until the current program completes.
  • Short processes have to wait for a long time until the bigger process arrives before it.
  • The waiting time is usually high.
  • This scheduling algorithm is not ideal for time-sharing systems.

Conclusion

  • FCFS stands for First come first serve.
  • It is the simplest process scheduling algorithm.
  • In FCFS, the process that arrives first will be served first.
  • Billing counters in the supermarket are real-life examples of the FCFS algorithm.
  • Because of the simple nature of the FCFS algorithm, small processes suffer starvation when a big process arrives first.