Q2Operating System
Question
Explain the various process scheduling algorithms with examples and compare them.
Answer
A definitive mathematical comparison of Process Scheduling algorithms (FCFS, SJF, SRTF, Round Robin), detailing their exact execution mechanics, wait time calculations, and the severe implications of Starvation and the Convoy Effect.
In a modern multiprogramming OS, the Ready Queue is constantly flooded with dozens of processes demanding CPU time. The Short-Term Scheduler is the mathematical engine that dictates exactly which process gets the CPU next. The algorithms are strictly classified into two paradigms: Non-Preemptive (once a process enters the CPU, it violently holds it until completion) and Preemptive (the OS can aggressively rip the CPU away from a running process).
1. First-Come, First-Served (FCFS) [Non-Preemptive]
The absolute simplest architecture. It uses a strict FIFO mathematical queue. - Example: P1 arrives at 0ms (Burst 24ms). P2 arrives at 1ms (Burst 3ms). P3 arrives at 2ms (Burst 3ms). - Execution: P1 executes from 0 to 24. P2 executes from 24 to 27. P3 executes from 27 to 30. - Flaw: P2 and P3 are tiny, but they are violently blocked for 24 milliseconds waiting for P1. This catastrophic bottleneck is mathematically known as the Convoy Effect, resulting in horrific Average Wait Times.
2. Shortest Job First (SJF) [Non-Preemptive]
The mathematically optimal algorithm for minimizing Average Wait Time. The Scheduler aggressively scans the queue and selects the process with the smallest exact Burst Time. - Example: P1(6ms), P2(8ms), P3(7ms), P4(3ms) all in queue. - Execution: P4(3) executes first, then P1(6), then P3(7), then P2(8). - Flaw: It mathematically guarantees Starvation. If a 100ms process is in the queue, and a continuous stream of 2ms processes arrives, the 100ms process will NEVER execute. Furthermore, it is practically impossible for the OS to accurately predict the exact future burst time of a process.
3. Shortest Remaining Time First (SRTF) [Preemptive SJF]
This is the highly aggressive, preemptive version of SJF. - Mechanism: If P1 (Burst 10ms) is executing, and after 2ms (Remaining 8ms) a new process P2 arrives with a Burst Time of 4ms, the OS mathematically compares them. Since , the OS violently interrupts P1, yanks it out of the CPU, and injects P2. This ensures the absolute lowest wait times but generates massive Context Switch overhead.
4. Round Robin (RR) [Preemptive]
The absolute standard for Time-Sharing desktop OS architectures. - Mechanism: The OS mathematically defines a strict Time Quantum (e.g., ms). The queue is treated as a massive circular buffer. P1 gets exactly 4ms. If P1 is not done, the OS violently triggers a hardware timer interrupt, pre-empts P1, throws it to the back of the queue, and gives P2 exactly 4ms. - Mathematical Tuning: The entire performance of RR hinges on the Quantum . If is astronomically large, RR mathematically devolves into FCFS. If is microscopically small (e.g., 1 microsecond), the CPU spends 99% of its time executing Context Switches (saving and loading registers) and 1% actually executing code, crashing system throughput.