Detailed Algorithms (with Numerical Problems)
CPU scheduling determines which process gets the CPU next when multiple processes are in the ready queue. Scheduling algorithms aim to maximize CPU utilization, reduce waiting time, and improve system responsiveness.
Â
This section explores the five fundamental scheduling algorithms, explains their logic, and includes sample numerical problems to help students understand how these algorithms work in practice.
1. First-Come, First-Served (FCFS)
Definition: Processes are scheduled in the order they arrive. It’s a non-preemptive algorithm—once the CPU is allocated, the process runs to completion.
Â
Example:
Consider 3 processes with the following burst times:
Â
Process | Arrival Time | Burst Time |
---|---|---|
P1 | 0 | 5 |
P2 | 1 | 3 |
P3 | 2 | 8 |
Gantt Chart:
Â
Waiting Time (WT):
- P1: 0
- P2: 5 – 1 = 4
- P3: 8 – 2 = 6
Â
Average Waiting Time = (0 + 4 + 6) / 3 = 3.33 ms
2. Shortest Job First (SJF) — Non-Preemptive
Definition: The process with the shortest burst time is selected next. If arrival times differ, only processes that have already arrived are considered.
Â
Example:
Process | Arrival Time | Burst Time |
---|---|---|
P1 | 0 | 6 |
P2 | 1 | 4 |
P3 | 2 | 2 |
Gantt Chart:
Â
Waiting Time:
- P1: 0
- P3: 6 – 2 = 4
- P2: 8 – 1 = 7
Â
Average WT = (0 + 4 + 7) / 3 = 3.67 ms
3. Shortest Remaining Time First (SRTF) — Preemptive
Definition: Similar to SJF, but preemptive. A process can be interrupted if a new process arrives with a shorter burst time remaining.
Â
Example:
Process | Arrival Time | Burst Time |
---|---|---|
P1 | 0 | 8 |
P2 | 1 | 4 |
P3 | 2 | 2 |
Execution:
- At time 0: P1 starts
- At time 1: P2 arrives (shorter burst), preempts P1
- At time 2: P3 arrives (shortest), preempts P2
- Continue execution accordingly
Â
Gantt Chart:
Â
Calculate Waiting Time using Completion Time – Arrival Time – Burst Time
4. Round Robin (RR)
Definition: Each process is assigned a fixed time slice (quantum). If a process does not complete within its quantum, it’s added back to the queue.
Â
Example:
Process | Arrival Time | Burst Time |
---|---|---|
P1 | 0 | 5 |
P2 | 1 | 3 |
P3 | 2 | 1 |
Quantum = 2
Â
Execution Order:
Â
Waiting Time:
- P1: (6 – 0) + (9 – 6) = 9
- P2: (4 – 1) + (10 – 6) = 7
- P3: 4 – 2 = 2
Â
Average WT = (9 + 7 + 2) / 3 = 6 ms
5. Priority Scheduling (Preemptive or Non-preemptive)
Definition: Processes are scheduled based on priority. Lower number = higher priority.
Â
Process | Arrival Time | Burst Time | Priority |
---|---|---|---|
P1 | 0 | 4 | 2 |
P2 | 1 | 3 | 1 |
P3 | 2 | 2 | 3 |
Gantt Chart (Preemptive):
Â
Waiting Time:
- P1: (1-0) + (4-1) = 4
- P2: 1 – 1 = 0
- P3: 7 – 2 = 5
Â
Average WT = (4 + 0 + 5) / 3 = 3 ms
Summary
Algorithm | Preemptive | Starvation Possible | Best For |
---|---|---|---|
FCFS | No | Yes (long jobs early) | Simple batch processing |
SJF | No | Yes | Short predictable processes |
SRTF | Yes | Yes | Dynamic shortest tasks |
Round Robin | Yes | No | Time-sharing systems |
Priority | Optional | Yes (low-priority) | Real-time or critical systems |