Scheduling Algorithms in OS
Contents
Introduction
Imagine managing a busy coffee shop: some customers demand urgent service (like espresso orders), while others wait patiently (like a large batch brew). Operating systems use scheduling algorithms to similarly manage processes. Let’s explore the two broad categories: preemptive and non-preemptive scheduling.
Preemptive Scheduling: The Interrupter
What It Does
- The OS can pause a running process to prioritize another.
- Processes are interrupted based on priority, time limits, or events.
- Ex: A traffic light interrupting a green signal for an ambulance (higher priority).
- Ex: A video call app is running, but the OS pauses it to handle a sudden mouse click.
Pros & Cons
- Flexibility: Critical tasks get immediate attention.
- Better for real-time systems (e.g., robotics).
- Overhead: Frequent switching slows performance.
Common Preemptive Algorithms
- Round Robin (RR)
- Shortest Remaining Time First (SRTF)
- Priority Preemptive Scheduling
- Multilevel Queue Scheduling
- Multilevel Feedback Queue
Non-Preemptive Scheduling: The Completer
What It Does
- The OS lets a process run to completion once it starts.
- No interruptions unless the process voluntarily gives up the CPU (e.g., waits for I/O).
- Ex: A buffet line: Once you start filling your plate, others wait until you’re done.
- Ex: A printer finishes a job completely before starting the next, even if a new request arrives.
Pros & Cons
- Simplicity: Less overhead for the OS.
- Predictable: Processes finish in a set order.
- Poor responsiveness: Long tasks block others.
Common Non-Preemptive Algorithms
- First-Come, First-Served (FCFS)
- Shortest Job First (SJF)
- Priority Non-Preemptive Scheduling
- Highest Response Ratio Next (HRRN)
Key Differences
Feature | Preemptive | Non-Preemptive |
---|---|---|
Interruption | Yes (OS forces) | No (process finishes/yields) |
Overhead | High | Low |
Responsiveness | High | Low |
Use Cases | Real-time systems, multitasking | Batch processing, simple tasks |
Why Does This Matter?
- Preemptive ensures fairness and responsiveness (e.g., your phone handling calls and apps).
- Non-Preemptive guarantees task completion (e.g., printing documents in order).
FAQ
Q: Can an algorithm be both preemptive and non-preemptive?
No! They’re mutually exclusive. However, some systems combine both (e.g., preemptive for high-priority tasks).
Q: Which is better?
Depends on the goal:
- Preemptive: For interactive systems (desktops, servers).
- Non-Preemptive: For stability (embedded systems, batch jobs).
Q: What’s “voluntary yielding” in non-preemptive?
A process may give up the CPU on its own (e.g., waiting for user input).