Bird
Raised Fist0
Operating Systemsknowledge~6 mins

Priority scheduling in Operating Systems - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Imagine you have many tasks to do, but some are more important than others. You want to make sure the most important tasks get done first. Priority scheduling helps decide which task to run next based on how important it is.
Explanation
Assigning Priorities
Each task or process is given a priority value that shows how important it is. Higher priority means the task should run before others with lower priority. The system uses these values to order tasks.
Priorities determine the order in which tasks are chosen to run.
Preemptive vs Non-preemptive
In preemptive priority scheduling, a running task can be stopped if a new task with higher priority arrives. In non-preemptive, the current task runs until it finishes or waits, even if a higher priority task comes in.
Preemptive scheduling allows interruption for higher priority tasks, non-preemptive does not.
Handling Equal Priorities
When two or more tasks have the same priority, the system uses another method like first-come-first-served to decide which runs first. This ensures fairness among tasks with equal importance.
Equal priority tasks are scheduled using a secondary rule like arrival time.
Starvation Problem
Low priority tasks may wait a very long time if higher priority tasks keep coming. This is called starvation. Some systems use aging, which gradually increases the priority of waiting tasks to prevent this.
Starvation happens when low priority tasks wait indefinitely, but aging can fix it.
Real World Analogy

Imagine a hospital emergency room where patients are treated based on how serious their condition is. A patient with a severe injury is seen before someone with a minor cut, even if the minor cut patient arrived first.

Assigning Priorities → Doctors deciding which patient needs urgent care first
Preemptive vs Non-preemptive → Stopping treatment of a less serious patient to help a more critical one (preemptive) or finishing current treatment before helping others (non-preemptive)
Handling Equal Priorities → Treating patients with similar conditions in the order they arrived
Starvation Problem → A patient with a minor injury waiting too long because more serious cases keep arriving
Diagram
Diagram
┌───────────────┐
│ Task Queue    │
├───────────────┤
│ Task A (P=1)  │
│ Task B (P=3)  │
│ Task C (P=2)  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Scheduler     │
├───────────────┤
│ Picks highest │
│ priority task │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ CPU           │
│ Runs Task A   │
└───────────────┘
This diagram shows tasks with different priorities in a queue, the scheduler picking the highest priority task, and the CPU running it.
Key Facts
PriorityA value assigned to a task indicating its importance for scheduling.
Preemptive SchedulingAllows a running task to be interrupted if a higher priority task arrives.
Non-preemptive SchedulingThe current task runs to completion before switching, regardless of new tasks.
StarvationWhen low priority tasks wait indefinitely due to continuous arrival of higher priority tasks.
AgingA technique that gradually increases the priority of waiting tasks to prevent starvation.
Common Confusions
Believing that priority scheduling always guarantees fairness.
Believing that priority scheduling always guarantees fairness. Priority scheduling can cause starvation, meaning some tasks may never get CPU time unless aging or other methods are used.
Thinking preemptive and non-preemptive priority scheduling behave the same.
Thinking preemptive and non-preemptive priority scheduling behave the same. Preemptive scheduling can interrupt running tasks for higher priority ones, while non-preemptive waits for the current task to finish.
Summary
Priority scheduling runs tasks based on their importance, with higher priority tasks running first.
Preemptive scheduling can interrupt tasks, while non-preemptive waits for tasks to finish.
Starvation can occur for low priority tasks but can be prevented using aging.

Practice

(1/5)
1. What does priority scheduling in operating systems primarily use to decide which process runs first?
easy
A. The time each process has already run
B. The importance level assigned to each process
C. The size of the process in memory
D. The order in which processes arrive

Solution

  1. Step 1: Understand priority scheduling basics

    Priority scheduling chooses processes based on their assigned importance or priority level.
  2. 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.
  3. Final Answer:

    The importance level assigned to each process -> Option B
  4. Quick Check:

    Priority scheduling = importance level [OK]
Hint: Remember: priority means importance, not arrival time [OK]
Common Mistakes:
  • Confusing priority with arrival order
  • Thinking process size affects scheduling
  • Assuming time already run decides priority
2. Which of the following is the correct way to describe a preemptive priority scheduling system?
easy
A. A system that ignores priority and runs processes randomly
B. A system where processes run to completion without interruption
C. A system that schedules processes based on their arrival time only
D. A system where a running process can be interrupted if a higher priority process arrives

Solution

  1. Step 1: Define preemptive scheduling

    Preemptive scheduling allows interruption of a running process if a more important one arrives.
  2. Step 2: Match with priority scheduling

    In priority scheduling, preemptive means higher priority processes can interrupt lower priority ones.
  3. Final Answer:

    A system where a running process can be interrupted if a higher priority process arrives -> Option D
  4. Quick Check:

    Preemptive priority = interrupt for higher priority [OK]
Hint: Preemptive means interrupt if higher priority comes [OK]
Common Mistakes:
  • Confusing preemptive with non-preemptive
  • Thinking processes always run to completion
  • Ignoring priority in scheduling decisions
3. Consider three processes with priorities: P1=2, P2=1, P3=3 (1 is highest priority). If all arrive at the same time, which order will they be scheduled in a non-preemptive priority scheduling?
medium
A. P3, P1, P2
B. P1, P2, P3
C. P2, P1, P3
D. P1, P3, P2

Solution

  1. Step 1: Identify priority order

    Priority 1 is highest, so P2 (priority 1) runs first, then P1 (2), then P3 (3).
  2. Step 2: Apply non-preemptive scheduling

    Since all arrive together, processes run fully in priority order without interruption.
  3. Final Answer:

    P2, P1, P3 -> Option C
  4. Quick Check:

    Lower number = higher priority, run in that order [OK]
Hint: Lower priority number runs first in non-preemptive [OK]
Common Mistakes:
  • Mixing priority numbers with arrival order
  • Assuming preemption changes order here
  • Confusing priority 1 as lowest priority
4. A priority scheduling system is implemented but processes with lower priority sometimes run before higher priority ones. What is the most likely cause?
medium
A. The system is using non-preemptive scheduling and a low priority process started first
B. The priority numbers are assigned incorrectly with higher numbers meaning higher priority
C. The system is ignoring arrival times completely
D. The CPU is idle and no processes are running

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The system is using non-preemptive scheduling and a low priority process started first -> Option A
  4. Quick Check:

    Non-preemptive lets running process finish first [OK]
Hint: Non-preemptive means no interruption once started [OK]
Common Mistakes:
  • Assuming priority numbers are reversed
  • Ignoring scheduling type (preemptive vs non-preemptive)
  • Thinking arrival time is always ignored
5. You have four processes arriving at different times with priorities: P1(2, arrives at 0s), P2(1, arrives at 1s), P3(3, arrives at 2s), P4(1, arrives at 3s). Using preemptive priority scheduling, which process runs at time 3 seconds?
hard
A. P2
B. P4
C. P1
D. P3

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    P2 -> Option A
  4. Quick Check:

    Preempt only for higher priority; same priority continues [OK]
Hint: Preempt only if strictly higher priority [OK]
Common Mistakes:
  • Thinking same priority causes preemption
  • Assuming new arrivals always preempt
  • Confusing tie-breaker rules (usually FCFS for same priority)