Skip to content

CPU Scheduling Algorithms

Introduction

CPU scheduling is essential for maximizing CPU efficiency and ensuring smooth process execution. Different scheduling algorithms are used based on system requirements. The four major algorithms discussed here are:

  1. First Come, First Served (FCFS)
  2. Shortest Job First (SJF)
  3. Round Robin (RR)
  4. Multilevel Queue Scheduling (MLQ)

1. First Come, First Served (FCFS) Scheduling

Definition

  • The process that arrives first in the queue is executed first.
  • Works like a queue system (FIFO – First In, First Out).

How It Works

  1. The CPU is allocated to processes in the order of their arrival.
  2. Once a process gets the CPU, it runs to completion (Non-Preemptive).

Example

ProcessArrival TimeBurst TimeCompletion TimeTurnaround Time (TAT)Waiting Time (WT)
P10 ms5 ms5 ms5 ms0 ms
P21 ms3 ms8 ms7 ms4 ms
P32 ms8 ms16 ms14 ms6 ms

Advantages

✔ Simple and easy to implement
✔ Works well for batch systems

Disadvantages

✖ Poor response time for short processes
✖ Convoy Effect: Long processes make short ones wait


2. Shortest Job First (SJF) Scheduling

Definition

  • The process with the smallest burst time is executed first.
  • Can be Non-Preemptive or Preemptive (Shortest Remaining Time First – SRTF).

How It Works

  1. The scheduler selects the shortest job available in the queue.
  2. If Preemptive, a newly arrived shorter job preempts the running process.

Example (Non-Preemptive SJF)

ProcessArrival TimeBurst TimeCompletion TimeTurnaround Time (TAT)Waiting Time (WT)
P10 ms7 ms7 ms7 ms0 ms
P21 ms4 ms11 ms10 ms6 ms
P32 ms1 ms3 ms1 ms0 ms
P43 ms2 ms5 ms2 ms0 ms

Advantages

✔ Minimizes average waiting and turnaround time
✔ Efficient for batch processing

Disadvantages

✖ Starvation: Long processes may never execute
✖ Difficult to estimate burst time


3. Round Robin (RR) Scheduling

Definition

  • Each process gets a fixed time quantum (time slice) before switching to the next process.
  • Preemptive scheduling ensuring fair CPU distribution.

How It Works

  1. A fixed time quantum (e.g., 4ms) is assigned.
  2. The CPU cycles through processes in a round-robin fashion.
  3. If a process is not completed within its quantum, it goes to the end of the queue.

Example (Time Quantum = 4ms)

ProcessArrival TimeBurst TimeOrder of ExecutionCompletion TimeTurnaround TimeWaiting Time
P10 ms10 msP1(4) → P2(4) → P3(4) → P1(4) → P1(2)20 ms20 ms10 ms
P21 ms6 msP2(4) → P3(4) → P1(4) → P1(4) → P2(2)12 ms11 ms5 ms
P32 ms8 msP3(4) → P1(4) → P1(4) → P3(4)16 ms14 ms6 ms

Advantages

✔ Prevents starvation (each process gets CPU time)
✔ Fair for interactive systems

Disadvantages

✖ High Context Switching Overhead
✖ Performance depends on quantum size:

  • Too small → Too many switches, wasting CPU time
  • Too large → Becomes similar to FCFS

4. Multilevel Queue (MLQ) Scheduling

Definition

  • Processes are divided into multiple queues based on priority or type (foreground/background).
  • Each queue has its own scheduling algorithm.

How It Works

  1. The system divides processes into separate queues, such as:
    • Foreground Queue (Interactive, uses Round Robin)
    • Background Queue (Batch jobs, uses FCFS)
  2. CPU allocates time to each queue based on priority.

Example Queue Structure

Queue TypeScheduling Algorithm
System ProcessesHighest Priority
Interactive ProcessesRound Robin
Background (Batch Jobs)FCFS

Advantages

✔ Good for multi-user and multi-tasking systems
✔ Can handle different process types efficiently

Disadvantages

✖ Difficult to implement
✖ Priority inversion (low-priority process may get delayed indefinitely)


Comparison of Scheduling Algorithms

CriteriaFCFSSJFRound RobinMultilevel Queue
TypeNon-PreemptivePreemptive / Non-PreemptivePreemptivePreemptive & Non-Preemptive
EfficiencyLow (convoy effect)High (low waiting time)Medium (fair but overhead)High (multi-tasking)
StarvationNoYes (for long jobs)NoYes (lower priority may starve)
Response TimeHighMediumLow (good for interactive)Varies
Best ForBatch SystemsShort processesTime-sharing SystemsMulti-tasking Systems

Conclusion

Each scheduling algorithm serves a different purpose:

  • FCFS is simple but inefficient.
  • SJF minimizes waiting time but causes starvation.
  • Round Robin is fair but has context-switching overhead.
  • Multilevel Queue is ideal for multi-user systems but complex to implement.