First Come First Serve (FCFS) Scheduling
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.
Process | Burst Time | Arrival time |
---|---|---|
P1 | 6 | 2 |
P2 | 8 | 1 |
P3 | 3 | 0 |
P4 | 4 | 4 |
Let us see how the above processes are handled using the First come first serve scheduling algorithm.
- At time t=0
Process | Burst Time | Arrival time |
---|---|---|
P1 | 6 | 2 |
P2 | 8 | 1 |
P3 | 3 | 0 (please add this row in different color) |
P4 | 4 | 4 |
Queue
Gantt Chart
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
Process | Burst Time | Arrival time |
---|---|---|
P1 | 6 | 2 |
P2 | 8 | 1 (please add this row in different color but same color as the one used in previous image) |
P3 | 3 | 0 |
P4 | 4 | 4 |
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 Gantt Chart
- At time t=2
Process | Burst Time | Arrival time |
---|---|---|
P1 | 6 | 2 (please add this row in different color but the same color as the one used in the previous image) |
P2 | 8 | 1 |
P3 | 3 | 0 |
P4 | 4 | 4 |
At time t=2, Process P1 arrives and waits in the queue as there is another process being executed.
Queue
Gantt Chart
- 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 Gantt Chart
- At time t=5
Process | Burst Time | Arrival time |
---|---|---|
P1 | 6 | 2 |
P2 | 8 | 1 |
P3 | 3 | 0 |
P4 | 4 | 4 (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 Gantt Chart
- At time t=21
In the end, at time t=21 we get a Gantt chart as shown below.
Gantt Chart
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.
Process | Burst Time | Arrival time | Completion Time | Turn Around Time |
---|---|---|---|---|
P1 | 6 | 2 | 17 | 15 |
P2 | 8 | 1 | 11 | 10 |
P3 | 3 | 0 | 3 | 3 |
P4 | 4 | 4 | 21 | 17 |
Next, let us find the waiting time. Waiting Time = Turn Around Time – Burst Time.
Process | Burst Time (ms) | Arrival Time (ms) | Completion Time (ms) | Turn Around Time (ms) | Waiting Time (ms) |
---|---|---|---|---|---|
P1 | 6 | 2 | 3 | 15 | 9 |
P2 | 8 | 1 | 11 | 10 | 9 |
P3 | 3 | 0 | 17 | 3 | 3 |
P4 | 4 | 4 | 21 | 17 | 13 |
The average waiting time is calculated by taking the sum of all the waitings and dividing it by the number of processes.
= 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.