Priority scheduling in Operating Systems - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using priority scheduling in operating systems, it's important to understand how the time to schedule processes grows as more processes arrive.
We want to know how the scheduling time changes when the number of processes increases.
Analyze the time complexity of the following priority scheduling code snippet.
while process_list is not empty:
find process with highest priority
run that process
remove it from process_list
This code selects and runs the highest priority process repeatedly until all are done.
Look at what repeats in this scheduling approach.
- Primary operation: Finding the highest priority process in the list.
- How many times: Once for each process, so as many times as there are processes.
As the number of processes increases, the time to find the highest priority process grows because it checks all remaining processes each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 checks |
| 100 | About 5,050 checks |
| 1000 | About 500,500 checks |
Pattern observation: The number of operations grows much faster than the number of processes, roughly like the square of the input size.
Time Complexity: O(n²)
This means that if you double the number of processes, the scheduling time roughly quadruples.
[X] Wrong: "Finding the highest priority process each time only takes the same time regardless of how many processes are left."
[OK] Correct: Actually, each search looks through all remaining processes, so as the list shrinks, it still takes time proportional to how many are left, adding up to a lot overall.
Understanding how scheduling time grows helps you explain how operating systems handle many tasks efficiently, a useful skill in system design discussions.
What if we used a priority queue data structure instead of searching the list each time? How would the time complexity change?
Practice
Solution
Step 1: Understand priority scheduling basics
Priority scheduling chooses processes based on their assigned importance or priority level.Step 2: Compare with other scheduling criteria
Unlike first-come-first-served or shortest job first, priority scheduling uses priority, not arrival time or size.Final Answer:
The importance level assigned to each process -> Option BQuick Check:
Priority scheduling = importance level [OK]
- Confusing priority with arrival order
- Thinking process size affects scheduling
- Assuming time already run decides priority
Solution
Step 1: Define preemptive scheduling
Preemptive scheduling allows interruption of a running process if a more important one arrives.Step 2: Match with priority scheduling
In priority scheduling, preemptive means higher priority processes can interrupt lower priority ones.Final Answer:
A system where a running process can be interrupted if a higher priority process arrives -> Option DQuick Check:
Preemptive priority = interrupt for higher priority [OK]
- Confusing preemptive with non-preemptive
- Thinking processes always run to completion
- Ignoring priority in scheduling decisions
Solution
Step 1: Identify priority order
Priority 1 is highest, so P2 (priority 1) runs first, then P1 (2), then P3 (3).Step 2: Apply non-preemptive scheduling
Since all arrive together, processes run fully in priority order without interruption.Final Answer:
P2, P1, P3 -> Option CQuick Check:
Lower number = higher priority, run in that order [OK]
- Mixing priority numbers with arrival order
- Assuming preemption changes order here
- Confusing priority 1 as lowest priority
Solution
Step 1: Understand non-preemptive behavior
In non-preemptive priority scheduling, once a process starts, it runs to completion even if a higher priority process arrives later.Step 2: Explain why lower priority runs first
If a low priority process starts first, it will finish before the higher priority process can run.Final Answer:
The system is using non-preemptive scheduling and a low priority process started first -> Option AQuick Check:
Non-preemptive lets running process finish first [OK]
- Assuming priority numbers are reversed
- Ignoring scheduling type (preemptive vs non-preemptive)
- Thinking arrival time is always ignored
Solution
Step 1: Track process arrivals and priorities
At 0s: P1(2) starts. At 1s: P2(1) arrives and preempts P1 since higher priority (lower number). At 2s: P3(3) arrives, lower priority than P2(1), so P2 continues. At 3s: P4(1) arrives, same priority as running P2.Step 2: Determine which process runs at 3s
In preemptive priority scheduling, preemption occurs only if a strictly higher priority process arrives. Since P4 has the same priority (1) as P2, P2 is not preempted and continues running at time 3s.Final Answer:
P2 -> Option AQuick Check:
Preempt only for higher priority; same priority continues [OK]
- Thinking same priority causes preemption
- Assuming new arrivals always preempt
- Confusing tie-breaker rules (usually FCFS for same priority)
