What is the Preemptive priority algorithm in operating systems? Explain with an example.
Explain the Preemptive priority algorithm with an example in OS.
491
31-Mar-2023
Updated on 31-Mar-2023
Krishnapriya Rajeev
31-Mar-2023The preemptive priority algorithm is a process scheduling algorithm used in operating systems 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:
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:
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: