Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

operating system

What is preemptive priority scheduling algorithm?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In preemptive priority scheduling algorithm, every time a process with higher priority arrives in the waiting queue, the CPU cycle is shifted to the process with the highest priority. This is preemptive because a process that’s already being executed can be stopped to execute a process with higher priority.

Example

The following example will help you understand preemptive priority scheduling better. Following are all the processes and their arrival and burst times. We must also note their priority numbers, as they are the key to preemptive priority scheduling. The priority for P1, P2, P3, and P4, respectively, is 0, 2, 3, 1.

Time = 0

At time=0time=0, P1 arrives in the waiting queue. As it is the only process in the waiting queue, the CPU cycle is assigned to P1 until another process arrives.

P1

Time = 1

At time=1time=1, the process P2 arrives in the waiting queue. Now, the comparison is between the priority of P1 and P2. The processes have priorities 0 and 2, respectively. Hence, P2 is assigned the CPU cycle until another process arrives in the waiting queue. The process P1 is moved back to the waiting queue.

P1P2

Time = 2

At time=2time=2, the processes P3 and P4 arrive in the waiting queue. Now, the comparison is between the priority of all 4 processes. Their priorities are 0, 2, 3, and 1, respectively. Hence, P3 is assigned the CPU cycle until another process arrives or it terminates. The processes P1, P2, and P4 are kept in the waiting queue.

P1P2P3

Time = 4

At time=4time=4, the process P3 is done executing, as its burst time is over. Then, the comparison of priority is between P1, P2, and P4. Their priorities are 0, 2, and 1 respectively. Hence, P2 is assigned the CPU cycle until some other process arrives or it terminates. The processes P1 and P4 are moved to the waiting queue.

P1P2P3P3P2

Time = 6

At time=6time=6, process P2 has completed its execution. Now, the comparison of priorities is between P1 and P4. Their priorities are 0 and 1, respectively. Hence, process P4 is assigned the CPU cycle until another process arrives or it terminates.

P1P2P3P3P2P2P4

Time = 7

At time=7time=7, the process P4 is done executing. As no other processes have arrived and none are left in the waiting queue, the CPU cycle is assigned to P1.

P1P2P3P3P2P2P4P1

Advantages and disadvantages

Following are the advantages and disadvantages of a preemptive priority scheduling algorithm:

Advantages

Disadvantages

Good way to ensure processes with higher priorities are handled first

Processes with lower priority may be starved

Good when the resources are limited and priorities for each process are defined beforehand

Difficult to objectively decide which processes are given higher priority


Low priority processes will be lost if the computer crashes

RELATED TAGS

operating system

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring