Scheduling 2 12
Scheduling 2 12
• Convoy effect: If P1 had a very large burst time, P2 and P3 would wait longer, leading to
4
inefficiency.
Shortest Job First (SJF)
Scheduling
Definition:
• SJF is a CPU scheduling algorithm where the process with the shortest burst time is executed first.
• It is an optimal algorithm for minimizing average waiting time, but it requires knowing burst time in
advance.
Types of SJF:
1️⃣ Non-Preemptive SJF:
• Once a process starts execution, it cannot be interrupted until it finishes.
• It follows a greedy approach by selecting the shortest available job.
• Risk: Long waiting times for longer processes if many short processes arrive.
2️⃣ Preemptive SJF (Shortest Remaining Time First - SRTF):
• If a new process arrives with a shorter burst time than the currently executing process, the CPU
switches to the new process.
• Ensures better response time but has higher context switching overhead.
Advantages:
● Minimizes waiting time and turnaround time efficiently.
● Preemptive SJF (SRTF) provides better responsiveness in a multi-user environment.
Disadvantages:
● Difficult to implement as it requires predicting burst time.
● Starvation problem: Long processes might never execute if short jobs keep arriving (especially in
5
SRTF).
Example of Non-Preemptive
SJF
Given Processes :
Process Arrival Time Burst Time
P1 0 7
P2 1 3
P3 2 5
Execution Process:
1️⃣ At t = 0, only P1 is available, so it should start execution.
2️⃣ At t = 1, P2 arrives, and its burst time (3) is shorter than P1’s (7). So, the CPU
switches to P2 when it gets free.
3️⃣ At t = 4, P3 arrives, and its burst time (5) is shorter than P1 (7), so it executes next.
4️⃣ Finally, P1 executes last.
Execution Order (Non-Preemptive SJF):
P2 → P3 → P1
Gantt Chart Representation:
| P2 | P3 | P1 |
1 4 9 16
6
Example of Preemptive SJF
(SRTF)
Given Processes :
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Execution Process (Shortest Remaining Time First - SRTF):
1️⃣ At t = 0, only P1 is available, so it starts execution.
2️⃣ At t = 1, P2 arrives (burst time = 4, shorter than P1’s remaining 7), so CPU switches
to P2.
3️⃣ At t = 3, P4 arrives (burst time = 5). Since P2 is still shorter, it continues.
4️⃣ At t = 5, P2 finishes, and the CPU switches to P4 (shortest remaining time).
5️⃣ At t = 8, P1 resumes execution since it's shorter than P3.
6️⃣ At t = 16, P1 finishes, and P3 executes last.
Final Execution Order:
P1 → P2 → P4 → P1 → P3
Gantt Chart Representation:
| P1 | P2 | P4 | P1 | P3 |
7
0 1 5 10 16 25
Multilevel Queue Scheduling
Definition: Multilevel Queue Scheduling is a CPU scheduling method where processes
are categorized into multiple queues based on their characteristics, such as priority,
process type, or resource needs. Each queue follows a specific scheduling algorithm.
Types of Queues:
1. Foreground Queue (Interactive Processes)
• Handles user applications, real-time tasks, and interactive processes.
• Uses Round Robin (RR) or Shortest Job Next (SJN) scheduling.
2. Background Queue (Batch Processes)
• Handles system processes, long-running tasks, and background jobs.
• Uses First-Come, First-Served (FCFS) or Priority Scheduling.
Scheduling Strategies:
● Fixed Priority Scheduling:
• Higher-priority queues execute first.
• Lower-priority queues only run when higher ones are empty.
• May lead to starvation for lower-priority processes.
● Time-Sharing Between Queues:
• CPU time is divided among queues.
• Prevents starvation by allocating fixed CPU time to lower-priority queues.
● Key Advantages:
• Efficient process categorization for better system performance.
• Different scheduling policies can be used for different types of tasks. 8
❖ FCFS is simple but may cause longer wait times due to the Convoy
Effect.
❖ SJF minimizes waiting time but can lead to starvation for longer
processes.