Understanding CPU Scheduling: First-Come First-Served (FCFS) with Examples and Diagrams
In operating systems, CPU scheduling determines the order in which processes are executed on the CPU. One of the simplest scheduling algorithms is First-Come First-Served (FCFS), where processes are executed in the order they arrive.
In this blog post, we will:
Understand FCFS scheduling with an example.
Learn how to calculate waiting times.
Compute average and maximum waiting times.
Visualize the process execution using Mermaid Gantt charts.
Solve a real problem step-by-step.
1. What is FCFS Scheduling?
First-Come First-Served (FCFS) is a non-preemptive scheduling algorithm.
The process that arrives first gets the CPU first.
Once a process starts execution, it runs until completion.
Example Scenario
Consider the following processes:
Process
Arrival Time
CPU Burst Time
P1
0
5
P2
2
7
P3
4
3
P4
6
9
P5
8
1
P6
10
5
We will schedule them using FCFS.
2. Step-by-Step Execution Schedule
Since FCFS follows the arrival order, the CPU executes processes as follows:
P1 arrives at t=0 and runs for 5 units.
Start Time: 0
End Time: 5
P2 arrives at t=2 but must wait until P1 finishes (t=5).
Start Time: 5
End Time: 5 + 7 = 12
P3 arrives at t=4 but must wait until P2 finishes (t=12).
Start Time: 12
End Time: 12 + 3 = 15
P4 arrives at t=6 but must wait until P3 finishes (t=15).
Start Time: 15
End Time: 15 + 9 = 24
P5 arrives at t=8 but must wait until P4 finishes (t=24).
Start Time: 24
End Time: 24 + 1 = 25
P6 arrives at t=10 but must wait until P5 finishes (t=25).
Start Time: 25
End Time: 25 + 5 = 30
3. Calculating Waiting Times
The waiting time for a process is the time it spends waiting in the ready queue before execution.