---
title: "Explain the Preemptive priority algorithm with an example in OS."  
description: "Explain the Preemptive priority algorithm with an example in OS."  
author: "Revati S Misra"  
published: 2023-03-31  
updated: 2023-03-31  
canonical: https://www.mindstick.com/forum/157664/explain-the-preemptive-priority-algorithm-with-an-example-in-os  
category: "Operating System"  
tags: ["windows", "operating system"]  
reading_time: 2 minutes  

---

# Explain the Preemptive priority algorithm with an example in OS.

What is the Preemptive priority [algorithm in operating](https://answers.mindstick.com/qa/96691/what-is-banker-s-algorithm-in-operating-system) [systems](https://www.mindstick.com/articles/85451/best-travel-systems-for-newborns-to-pick)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with an example.

## Replies

### Reply by Krishnapriya Rajeev

The preemptive priority [algorithm](https://www.mindstick.com/blog/119/implementing-cryptography-in-c-sharp-dot-net-by-using-sha1-algorithm) is a process scheduling algorithm used in [operating systems](https://www.mindstick.com/articles/332551/operating-systems-trends-and-innovations-in-2023) to determine which process should be executed first. In this algorithm, each process is assigned a *priority value*, which determines its relative importance in the system. The process with the highest priority gets executed first, and if two processes have the same priority, then the one that arrived first gets executed. There are two ways to assign priority values:

1. The lower the value, the higher the priority.
2. The higher the value, the higher the priority.

When a higher-priority process arrives while another process is executing, the currently executing process is preempted and put on hold, and the higher-priority process is enabled to execute. This continues until all processes have completed their execution.

Example:

Suppose there are seven processes in the system with the following priority values and burst times and here ***lower the value higher the priority***:

| **Process Id** | **Priority** | **Arrival Time** | **Burst time** |
| --- | --- | --- | --- |
| P1 | 2 | 0 | 1 |
| P2 | 6 | 1 | 7 |
| P3 | 3 | 2 | 3 |
| P4 | 5 | 3 | 6 |
| P5 | 4 | 4 | 5 |
| P6 | 10 | 5 | 15 |
| P7 | 9 | 6 | 8 |

The preemptive priority algorithm begins by selecting the process with the highest priority, which is P1 at arrival time 0. P1 will execute until it completes its execution or until a higher-priority process arrives. When P1 completes its execution, P2 arrives and becomes the process with the highest priority. While P2 is being executed, P3 arrives at the 2nd second and P3 has a lower value of priority which makes it a higher priority process and P2 gets preempted. P3 starts executing now. P6 gets executed at the very end as it is the least-priority process.

In this way, all the processes get executed and the *Gantt chart* is as follows:

```plaintext
	P1		P2		P3		P5		P4		P2		P7		P6
__________________________________________________________________
0		1		2		5		10		16		22		30		45
```


---

Original Source: https://www.mindstick.com/forum/157664/explain-the-preemptive-priority-algorithm-with-an-example-in-os

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
