What is CPU Scheduling?
CPU Scheduling is the process by which the CPU is assigned to different processes in a multi-
programming environment. When multiple processes are in the ready queue, the operating system
must choose one to execute next. The part of the OS that makes this decision is called the CPU
scheduler.
The goal of CPU scheduling is to maximize CPU utilization and system throughput while
minimizing waiting time, turnaround time, and response time.
Types of CPU Scheduling Algorithms
There are several types, but two important ones are explained below:
1. First-Come, First-Served (FCFS) Scheduling
Definition:
FCFS is the simplest CPU scheduling algorithm. The process that arrives first is served first, like
a queue in a supermarket.
Characteristics:
Non-preemptive: Once a process starts executing, it runs till completion.
Based on arrival time.
Advantages:
Simple and easy to implement.
Fair (in the sense that jobs are processed in order of arrival).
Disadvantages:
Can lead to long waiting times, especially if a long process comes before a short one (called
the convoy effect).
Poor average waiting time.
Example:
Process Arrival Time Burst Time
P1 0 5
P2 1 3
P3 2 8
Execution Order: P1 → P2 → P3
Waiting Times:
P1 = 0,
P2 = 5 - 1 = 4,
P3 = 5 + 3 - 2 = 6
Average Waiting Time = (0 + 4 + 6)/3 = 3.33 ms
2. Shortest Job First (SJF) Scheduling
Definition:
In SJF, the process with the smallest CPU burst time is selected next.
Types:
Non-preemptive SJF: Once a process starts, it completes.
Preemptive SJF (also called Shortest Remaining Time First - SRTF): A new process can
preempt if it has a shorter burst time.
Advantages:
Optimal in terms of average waiting time for non-preemptive version.
Efficient use of CPU.
Disadvantages:
Difficult to know the burst time in advance.
Can lead to starvation of longer processes.
Example (Non-preemptive SJF):
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 2
P4 3 1
Execution Order: P1 → P4 → P3 → P2
(based on smallest burst time among available processes)
Waiting Times:
P1 = 0,
P4 = 8 - 3 = 5,
P3 = 8 + 1 - 2 = 7,
P2 = 8 + 1 + 2 - 1 = 10
Average Waiting Time = (0 + 5 + 7 + 10)/4 = 5.5 ms