CPU Scheduling Algorithms: FCFS, SJF, SRTN, Round Robin & Priority
Suppose two jobs arrive together and need five and one CPU units. Serving the long job first makes the short job wait five units. Shortest-job-first cuts that wait if burst lengths are known, while round robin limits how long one job keeps the processor per turn.
1. Overview of Classical CPU Scheduling Algorithms
In DAVV BCA exams, Unit 2 always includes an 8-mark theory + Gantt chart numerical question on these five core CPU scheduling algorithms:
FCFS uses arrival order and shortest-job policies favour short work. Priority uses a rank, while round robin rotates the CPU after a time slice.
- 1. FCFS — First-Come, First-Served (Non-Preemptive FIFO Queue)
- 2. SJF — Shortest Job First (Non-Preemptive — Picks smallest Burst Time)
- 3. SRTN / SRTF — Shortest Remaining Time Next (Preemptive version of SJF)
- 4. Round Robin (RR) — Preemptive Time-Sharing using a fixed Time Quantum (q)
- 5. Priority Scheduling — Picks process with highest Priority number (Preemptive or Non-Preemptive)
View diagram · Figure 2.6: The 5 Core CPU Scheduling Algorithms
2. First-Come, First-Served (FCFS) Scheduling
- Mode: Strictly Non-Preemptive.
- How It Works: The simplest scheduling algorithm. Whichever process requests the CPU first is allocated the CPU first (just like a queue at a railway ticket counter). Implemented using a standard FIFO (First-In, First-Out) queue.
- Major Drawback — The Convoy Effect:
If one huge CPU-bound process (e.g., Burst Time =
100 ms) arrives first, followed by 5 tiny processes (Burst Time =1 mseach), all 5 tiny processes get stuck waiting behind the giant process! This causes the average waiting time to skyrocket and is called the Convoy Effect.
Solved Numerical Example (FCFS):
Suppose 3 processes arrive at time AT = 0 in the order P1, P2, P3:
| Process | Arrival Time (AT) | Burst Time (BT) | Completion Time (CT) | Turnaround Time (TAT = CT - AT) | Waiting Time (WT = TAT - BT) |
|---|---|---|---|---|---|
| P1 | 0 | 24 ms | 24 | 24 - 0 = 24 | 24 - 24 = 0 |
| P2 | 0 | 3 ms | 27 | 27 - 0 = 27 | 27 - 3 = 24 |
| P3 | 0 | 3 ms | 30 | 30 - 0 = 30 | 30 - 3 = 27 |
View diagram · First come, first served CPU timeline
- Average Waiting Time: (0 + 24 + 27) / 3 = 17.0 ms
- Average Turnaround Time: (24 + 27 + 30) / 3 = 27.0 ms
3. Shortest Job First (SJF — Non-Preemptive)
- Mode: Non-Preemptive.
- How It Works: Among all available processes in the Ready Queue, the CPU is assigned to the process that has the smallest (shortest) next CPU Burst Time. If two processes have the exact same burst time, FCFS is used to break the tie.
- Key Advantage: SJF is mathematically proven to be Optimal—it gives the minimum possible Average Waiting Time for a given set of processes!
- Disadvantage: In practice, it is difficult to know the exact future CPU burst length of a process in advance, and long processes may suffer from Starvation if short jobs keep arriving.
Solved Numerical Example (Non-Preemptive SJF):
Using the exact same 3 processes (P1 = 24 ms, P2 = 3 ms, P3 = 3 ms) arriving at AT = 0, SJF runs the shortest jobs (P2, P3) first!
View diagram · Shortest job first CPU timeline
- Waiting Times: P2 = 0 ms, P3 = 3 ms, P1 = 6 ms.
- Average Waiting Time: (0 + 3 + 6) / 3 = 3.0 ms (Dropped from 17 ms in FCFS down to just 3 ms!)
4. Shortest Remaining Time Next (SRTN / SRTF)
- Mode: Preemptive version of SJF!
- How It Works: At any moment, the CPU executes the process with the shortest remaining burst time.
- Whenever a new process arrives in the Ready Queue, the scheduler compares its Burst Time with the remaining time of the currently running process. If the newly arrived process requires less time than what is left of the current process, the OS immediately preempts (interrupts) the current process and gives the CPU to the new shorter process!
Solved Numerical Example (SRTN):
| Process | Arrival Time (AT) | Burst Time (BT) | Completion Time (CT) | TAT = CT - AT | WT = TAT - BT |
|---|---|---|---|---|---|
| P1 | 0 | 8 ms | 17 | 17 - 0 = 17 | 17 - 8 = 9 |
| P2 | 1 | 4 ms | 5 | 5 - 1 = 4 | 4 - 4 = 0 |
| P3 | 2 | 9 ms | 26 | 26 - 2 = 24 | 24 - 9 = 15 |
| P4 | 3 | 5 ms | 10 | 10 - 3 = 7 | 7 - 5 = 2 |
- Step-by-Step Execution & Gantt Chart:
- At t=0: Only P1 is present (BT=8). P1 starts running.
- At t=1: P2 arrives with BT=4, while P1 still has 7 ms remaining! Since 4 < 7, P1 is preempted and P2 runs from 1 to 5.
- At t=5: P2 finishes. Remaining times: P1=7, P3=9, P4=5. Shortest is P4 (5 ms), so P4 runs from 5 to 10, then P1 (10 to 17), then P3 (17 to 26).
View diagram · Shortest remaining time CPU timeline
- Average Waiting Time: (9 + 0 + 15 + 2 / 4) = 6.5 ms
5. Round Robin (RR) Scheduling
- Mode: Preemptive (Designed specifically for Time-Sharing Systems).
- How It Works:
- A small fixed unit of time called a Time Quantum (q) or Time Slice (typically
10 msto100 ms) is defined. - The Ready Queue is treated as a circular FIFO queue.
- The scheduler gives the first process in the queue the CPU for at most 1 Time Quantum. If the process’s burst time is still not finished when the timer goes off, the process is preempted and placed at the tail (back) of the Ready Queue, and the next process gets its turn!
- A small fixed unit of time called a Time Quantum (q) or Time Slice (typically
- Effect of Time Quantum Size:
- If Time Quantum is too large (∞): Round Robin degenerates into simple FCFS.
- If Time Quantum is too small (1 ms): Causes excessive Context Switch Overhead, slowing down the CPU.
Solved Numerical Example (Round Robin with Time Quantum q = 4 ms):
Processes arriving at AT = 0: P1 (BT = 10 ms), P2 (BT = 4 ms), P3 (BT = 5 ms).
View diagram · Round robin CPU timeline
- Completion Times: P2 = 8, P3 = 17, P1 = 19.
- Waiting Times (CT - BT):
- P1 = 19 - 10 = 9 ms
- P2 = 8 - 4 = 4 ms
- P3 = 17 - 5 = 12 ms
- Average Waiting Time: (9 + 4 + 12) / 3 = 8.33 ms
6. Priority Scheduling (Starvation & Aging)
- Mode: Can be either Preemptive or Non-Preemptive.
- How It Works: A Priority Number (an integer) is associated with each process, and the CPU is allocated to the process with the Highest Priority (e.g., Priority
1= Highest). Equal-priority processes are scheduled in FCFS order. - Major Problem — Starvation (Indefinite Blocking): In a heavily loaded system where high-priority processes keep arriving continuously, a low-priority process may wait in the Ready Queue forever and never get the CPU! This is called Starvation.
- The Solution — Aging:
Aging is a technique that gradually increases the priority of processes that have been waiting in the system for a long time (e.g., increasing a waiting process’s priority by
+1every 15 minutes). Eventually, even the lowest-priority process reaches top priority and gets executed!
💡 Exam Preparation Tips for DAVV BCA
- Always draw a Gantt Chart in every CPU scheduling answer—even in a theory question! Label the time at every change of process.
- Memorize these two pairs of Problem → Solution for 2-mark/viva questions:
- FCFS Problem: Convoy Effect (short processes stuck behind a huge process).
- Priority/SJF Problem: Starvation → Solved by Aging!