Scheduling Algorithms in Operating Systems - Part 4 - Priority Scheduling Algorithm
Published: 2021-12-07 12:29:43

---
title: Scheduling Algorithms in Operating Systems - Part 4 - Priority Scheduling Algorithm
published: true
description: 
tags: 
cover_image: https://cdn-images-1.medium.com/max/2000/1*YFMjCPU7Ttl0xb1lmiUkVg.jpeg
---
#### You can skip the intro if you have been through the [**Part - 1**](https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-5g7a) or [**Part - 2**](https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-2-shortest-job-first-sjf-4if3) or [**Part - 3**](https://dev.to/iamdigitalanna/scheduling-algorithms-in-operating-systems-part-3-round-robin-algorithm-9j9) of the series

Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.

For this purpose, there are many scheduling algorithms like the **First Come First Serve** (FCFS) algorithm, **Shortest Job First** (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.

 1. **Arrival Time**: The time at which the process arrives in the processor for execution.

 2. **Waiting Time**: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :
>  *Waiting time = Completion Time- Arrival Time- Service Time*

3. **Service Time/ Burst time**: The time required by the process to complete its execution.

4. **Turnaround Time**: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula:
>  *Turnaround Time = Service Time + Waiting Time*

Now, let us have a look at the Priority Scheduling algorithm :

## Priority Scheduling Algorithm:

In this algorithm, every process has a specific priority and is executed according to the given priority.

 1. **Non-Preemptive Priority Scheduling Algorithm:**

In this algorithm, the process of highest priority is completely executed first and then the process with the next highest priority is executed.

Let's take an example:

![](https://cdn-images-1.medium.com/max/2710/0*Z4KmHicp52OpPNnF.png)

In the above example, we have 5 processes A, B,C, D,E

Processes A and B enter the processor first. Out of those two processes, process B has a higher priority and hence, process B will be executed first.

Meanwhile, the other processes are added to the waiting queue.

![](https://cdn-images-1.medium.com/max/2510/0*WJHklQMTnjHGLOIl.png)

Now, after B completes its execution, the waiting queue is checked. Process D is the process in the waiting queue with the highest priority. Hence, it is executed next.

![](https://cdn-images-1.medium.com/max/2510/0*4obnRfg4OjchHTUp.png)

Similarly, now from the queue, process E has the highest priority and is therefore executed.

![](https://cdn-images-1.medium.com/max/2510/0*pNiwuuxW0c4zuW71.png)

Again, considering the priority, process C is executed….

![](https://cdn-images-1.medium.com/max/2810/0*5WC3E8h2uqcSCve6.png)

followed by the process A.

![](https://cdn-images-1.medium.com/max/2810/0*neo2qI9EiRXfgtHn.png)

Now, let us calculate the average waiting time and turnaround time.

![](https://cdn-images-1.medium.com/max/2886/0*rXptcfjgAH8bTqnu.png)

Advantages:

The processes of higher priority get preference and are made to execute earlier.

Disadvantages:

The processes with lower priority may suffer starvation.

2. **Preemptive Scheduling Algorithm:**

In this algorithm, if a process with a priority higher than that of the process being executed enters, the processor is preempted and that process starts executing.

For example,

![](https://cdn-images-1.medium.com/max/2710/0*Ojk0VZELOhuNG4zo.png)

Here at 0ms processes A and B enter the system. B being the process with the highest priority starts executing first….

![](https://cdn-images-1.medium.com/max/2510/0*QfEOhxNKiC3d4G1j.png)

..but as process D enters the processor since D has a higher priority, it starts executing next and B is added to the queue.

![](https://cdn-images-1.medium.com/max/2510/0*6aJWWIwh0f09Y62I.png)

Now after D completes executing, (D gets completely executed as no other process with higher priority enters in between) B being the process with the highest priority starts executing next.

![](https://cdn-images-1.medium.com/max/2510/0*rsTStM2SvsZ2RlE7.png)

Next, the process E starts executing

![](https://cdn-images-1.medium.com/max/2510/0*erHuzfX9XK7ap6zh.png)

Then, process C starts executing

![](https://cdn-images-1.medium.com/max/2510/0*xL5iloZYCQurrkel.png)

And finally, process A starts executing

![](https://cdn-images-1.medium.com/max/2734/0*jUjEsCl_6dkEm_6C.png)

Now, let us calculate the average waiting time and turnaround time

![](https://cdn-images-1.medium.com/max/2710/0*jBD8Cpx3d22t5WjJ.png)

The advantage of this algorithm is that the processes with higher priority do not have to wait, hence they are not starved.


In the next post, let us have a look at the Highest Response Ratio Next Algorithm.


Hope this helps you!

Have a nice day 😊!!