0
0
Operating Systemsknowledge~6 mins

Priority scheduling in Operating Systems - Full Explanation

Choose your learning style9 modes available
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.