Priority Scheduling in Operating System (original) (raw)

Last Updated : 22 May, 2025

Priority scheduling is one of the most common scheduling algorithms used by the operating system to schedule processes based on their priority. Each process is assigned a priority value based on criteria such as memory requirements, time requirements, other resource needs, or the ratio of average I/O to average CPU burst time.

The process with the highest priority is selected for execution first. If there are multiple processes sharing the same priority, they are scheduled in the order they arrived, following a First-Come, First-Served approach. The chosen process is then executed, either until completion or until it is preempted, depending on whether the scheduling is preemptive or non-preemptive.

Priority Scheduling can be implemented in two ways:

Non-Preemptive Priority Scheduling

In Non-Preemptive Priority Scheduling, the CPU is not taken away from the running process. Even if a higher-priority process arrives, the currently running process will complete first.

Ex: A high-priority process must wait until the currently running process finishes.

Example of Non-Preemptive Priority Scheduling:

Consider the following table of arrival time and burst time for three processes P1, P2 and P3:

Note: Lower number represents higher priority.

Process Arrival Time Burst Time Priority
P1 0 4 2
P2 1 2 1
P3 2 6 3
Step-by-Step Execution:

Gantt Chart:

Now, lets calculate average waiting time and turn around time:

Process Arrival Time Burst Time Completion Time Turnaround Time (CT - AT) Waiting Time (TAT - BT)
P1 0 4 4 4 0
P2 1 2 6 5 3
P3 2 6 12 10 4

Code Implementation

**Preemptive Priority Scheduling

In **Preemptive Priority Scheduling, the CPU can be taken away from the currently running process if a new process with a higher priority arrives.

Ex: A low-priority process is running, and a high-priority process arrives; the CPU immediately switches to the high-priority process.

Example of Preemptive Priority Scheduling (Same Arrival Time)

Consider the following table of arrival time and burst time for three processes P1, P2 and P3:

Note: Higher number represents higher priority.

Process Arrival Time Burst Time Priority
P1 0 7 2
P2 0 4 1
P3 0 6 3
Step-by-Step Execution:

Gant Chart:

Now, lets calculate average waiting time and turn around time:

Process Arrival Time Burst Time Completion Time Turnaround Time (CT - AT) Waiting Time (TAT - BT)
P1 0 7 13 13 6
P2 0 4 17 17 13
P3 0 6 6 6 0

Example of Preemptive Priority Scheduling (Different Arrival Time)

Consider the following table of arrival time and burst time for three processes P1, P2 and P3:

Process Arrival Time Burst Time Priority
P1 0 6 2
P2 1 4 3
P3 2 5 1
Step-by-Step Execution:

Gant Chart:

Now, lets calculate average waiting time and turn around time:

Process Arrival Time Burst Time Completion Time Turnaround Time (CT - AT) Waiting Time (TAT - BT)
P1 0 6 10 10 4
P2 1 4 5 4 0
P3 2 5 15 13 8