Priority Scheduling in Operating System
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
- Preemptive Priority Scheduling
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:
- At Time 0: Only P1 has arrived. P1 starts execution as it is the only available process, and it will continue executing till t = 4 because it is a non-preemptive approach.
- At Time 4: P1 finishes execution. Both P2 and P3 have arrived. Since P2 has the highest priority (Priority 1), it is selected next.
- At Time 6: P2 finishes execution. The only remaining process is P3, so it starts execution.
- At Time 12: P3 finishes 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 |
---|
- Average Turnaround Time = 6.33
- Average Waiting Time = 2.33
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:
- At Time 0: All processes arrive at the same time. P3 has the highest priority (Priority 3), so it starts execution.
- At Time 6: P3 completes execution. Among the remaining processes, P1 (Priority 2) has a higher priority than P2, so P1 starts execution.
- At Time 13: P1 completes execution. The only remaining process is P2 (Priority 1), so it starts execution.
- At Time 17: P2 completes execution. All processes are now finished.
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 |
---|
- Average Turnaround Time = 12
- Average Waiting Time = 6.33
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:
- At Time 0: Only P1 has arrived, so it starts execution.
- At Time 1: P2 arrives with a higher priority (Priority 3) than P1. P1 is preempted, and P2 starts execution.
- At Time 5: P2 completes execution. Both P1 and P3 are available. P1 has the higher priority (Priority 2), so it starts execution.
- At Time 10: P1 completes execution. P3 resumes execution to finish its remaining burst time.
- At Time 15: P3 completes execution. All processes are now finished.
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 |
---|
- Average Turnaround Time = 9
- Average Waiting Time = 4
Similar Reads
List scheduling in Operating System Prerequisite - CPU Scheduling List Scheduling also known as Priority List Based Scheduling is a scheduling technique in which an ordered list of processes are made by assigning them some priorities. So, basically what happens is, a list of processes that are ready to be executed at a given point is
3 min read
Process Schedulers in Operating System A process is the instance of a computer program in execution. Scheduling is important in operating systems with multiprogramming as multiple processes might be eligible for running at a time.One of the key responsibilities of an Operating System (OS) is to decide which programs will execute on the C
7 min read
I/O scheduling in Operating Systems Input/Output (I/O) operations are how a computer communicates with external devices such as hard drives, keyboards, printers, and network interfaces. These operations involve transferring data into and out of the system whether itâs reading a file, saving a document, printing, or sending data over a
6 min read
CPU Scheduling in Operating Systems CPU scheduling is a process used by the operating system to decide which task or process gets to use the CPU at a particular time. This is important because a CPU can only handle one task at a time, but there are usually many tasks that need to be processed. The following are different purposes of a
8 min read
Multiple-Processor Scheduling in Operating System In multiple-processor scheduling multiple CPUs are available and hence Load Sharing becomes possible. However multiple processor scheduling is more complex as compared to single processor scheduling. In multiple processor scheduling, there are cases when the processors are identical i.e. HOMOGENEOUS
8 min read
Multi Processing Operating System The operating system functions like a manager of all the available resources. Therefore operating system is defined as an interface between the system and the user. There are various types of operating systems such as Batch Operating Systems, Multi-programming Operating Systems, distributed operatin
4 min read
Two-level scheduling in Operating Systems Two-level scheduling is an efficient scheduling method that uses two schedulers to perform process scheduling. Let us understand it by an example : Suppose a system has 50 running processes all with equal priority and the system's memory can only hold 10 processes simultaneously. Thus, 40 processes
2 min read
Priority Inversion in Operating Systems Let us first put 'priority inversion' in the context of the Big Picture i.e. where does this come from.In Operating System, one of the important concepts is Task Scheduling. There are several Scheduling methods such as First Come First Serve, Round Robin, Priority-based scheduling, etc. Each schedul
6 min read
Precedence Graph in Operating System Prerequisite - Process Synchronization Precedence Graph is a directed acyclic graph which is used to show the execution level of several processes in operating system. It consists of nodes and edges. Nodes represent the processes and the edges represent the flow of execution. Properties of Precedenc
2 min read
Long Term Scheduler in Operating System Pre-requisites: Process Schedulers in Operating System A long-term scheduler, also known as a job scheduler, is an operating system component that determines which processes should be admitted to the system and when. It is used in batch processing systems and operates at a high level. The long-term
3 min read