๐ง What is Preemptive Scheduling?
Preemptive Scheduling is a CPU scheduling method where the operating system forcibly takes the CPU away from the currently running process and gives it to another higher-priority or waiting process.
So, even if a process hasnโt finished โ the OS can say:
๐ โYouโre done for now. Someone else needs the CPU.โ
๐ When Does Preemption Happen?
-
A higher-priority process becomes ready
-
The running process exceeds its time slice (quantum)
-
A process I/O completes, and it rejoins the ready queue
-
System interrupts or signals occur
๐ Example:
Round Robin (with time quantum = 100 ms)
-
P1 runs for 100 ms โฑ๏ธ
-
OS preempts it and gives CPU to P2
-
Even if P1 isnโt done โ itโs forced to yield
๐ฉ Key Features
| Feature | Description |
|---|---|
| โฑ๏ธ Time-limited execution | Each process runs only for a short time quantum |
| โก Responsive | Good for interactive and real-time systems |
| โ๏ธ Fair | Prevents CPU hogging |
| ๐ Context Switch | Frequent โ leads to more overhead |
โ Advantages of Preemptive Scheduling
| โ Benefit | ๐ Why It Matters |
|---|---|
| ๐จ Faster responsiveness | For real-time and UI apps (e.g., mouse clicks, keypresses) |
| ๐ Fair CPU usage | Long-running processes canโt block others |
| ๐ Higher throughput | Shorter jobs donโt get stuck behind long ones |
| ๐ง Efficient priority handling | High-priority tasks preempt low ones immediately |
โ Disadvantages
| โ Issue | ๐ Explanation |
|---|---|
| ๐ High context switch overhead | Frequent task switching uses CPU time |
| ๐งต Race conditions | If threads are preempted mid-update |
| โ ๏ธ Starvation risk | Low-priority processes may be delayed indefinitely |
| ๐ฅ Complex | Requires fine-tuned time management + interrupt handling |
๐ Common Preemptive Scheduling Algorithms
| Algorithm | How Itโs Preemptive |
|---|---|
| Round Robin | Preempts on time slice expiry |
| Shortest Remaining Time First (SRTF) | Preempts if a shorter job arrives |
| Priority Scheduling (Preemptive) | Preempts when higher-priority process arrives |
| Multilevel Feedback Queue (MLFQ) | Dynamically adjusts priorities and can preempt |
๐ง Interview-Ready Definition:
Preemptive Scheduling allows the OS to forcibly remove the CPU from a running process to assign it to another, usually based on priority or time-slicing. It ensures fairness, responsiveness, and better resource utilization in multi-user, real-time systems โ but comes at the cost of complexity and overhead.
๐ฅ๏ธ Real-World Analogy
Youโre working in an office (CPU), and your boss (OS) says,
โYouโre doing great, but this VIP just walked in โ give them your chair for now.โ
Thatโs preemption โ you get up, and they get to work.