Preemptive Priority CPU Scheduling Algorithm


Having the upper hand in computer operating systems, the CPU scheduling algorithm is a widely used method for scheduling processes. It is intended to make sure that the most important processes have priority access to the CPU so that system responsiveness and efficiency are maximized.

Each process in preemptive priority scheduling is given a priority value, which is often established by the nature and significance of the current job. When a higher-priority process becomes available, the one that is presently executing is preempted and the higher-priority process is executed in its place. The highest priority process is granted access to the CPU first.

Important and time-sensitive procedures are carried out swiftly and effectively thanks to this algorithm, whereas lower-priority processes might have to wait till higher-priority processes are finished. In real-time systems, when responsiveness and timeliness are crucial, it is a commonly utilized algorithm.

Preemptive Priority Scheduling Algorithm

A CPU scheduling mechanism called Preemptive Priority scheduling gives each process a priority rating depending on how important the job is. The work is completed more quickly the greater the priority.

  • The algorithm chooses the process with the highest priority and runs it until it completes or is stopped by a process with a higher priority.

  • In this procedure, the CPU interrupts the presently operating process and starts the new one if a new process with a higher priority arrives. Preemption is the term used to describe this procedure of stopping the present one.

  • In real-time systems, when a delay in completing a crucial activity might have catastrophic repercussions, preemptive priority scheduling assures that high-priority operations are carried out first.

The starving of low-priority jobs is one of the drawbacks of this approach, though. The low-priority jobs could starve if there are too many high-priority tasks using up all of the available time.

  • Several preemptive priority scheduling methods employ ageing to get around this issue. A process gets a chance to run eventually because its priority rises while it waits in the ready queue.

  • Preemptive Priority Scheduling, as a scheduling method, is helpful in systems where certain jobs are more crucial than others. It must be utilized cautiously to prevent starving low-priority jobs, though.

Priority Levels

In a computer system, the order in which tasks are carried out is determined by their priority levels. The significance, urgency, and resource needs of an activity often define its priority level. Tasks with higher priority levels are typically completed first, while those with lower priority levels are completed only in the absence of a higher priority job that is awaiting completion.

Tasks are given priority levels according to their relevance in preemptive priority scheduling. Higher priority activities are carried out first, while lower priority tasks are carried out only in the absence of higher priority tasks that need to be completed. Higher numbers indicate greater priority levels, which are often expressed as integers.

Implementation

Depending on the hardware used and the operating system being utilized, preemptive priority scheduling can be accomplished in several ways. Preemptive priority scheduling implementation typically entails the following steps −

Tasks are given priority levels based on their importance. Each task is given a priority level.

Perform the highest priority task first: The scheduler starts by carrying out the job with the highest priority level. The scheduler can make this decision using various scheduling techniques if there are numerous jobs with the same highest priority level.

Interrupt lower priority tasks: The scheduler interrupts the lower priority job and runs the higher priority task when a higher priority task becomes available.

Change task priority levels: Task priority levels can be dynamically adjusted in response to changes in their significance or resource needs.

Example 1

Consider the following processes −

Process

Arrival Time

Burst Time

Priority

       P1

           0

           4

      2

      P2

           1

           3

      1

      P3

            2

           1

      3

       P4

           3

            5

      4

Assume that these processes are scheduled using the Preemptive Priority CPU Scheduling Algorithm.

Prior to scheduling these processes, we must arrange them in priority order, starting with the one having the greatest priority −

Process

Arrival Time

Burst Time

Priority

       P1

          1

           3

        1

       P2

          0

          4

        2

      P3

          2

          1

       3

      P4

          3

          5

       4

We can see that P2 is allocated the CPU first since it has the highest priority. The CPU will go to P1, which has the next greatest priority, once P2 has run for one unit. Although P1 will have finished after 1 unit of time, P2 will still be running since it has 2 units of burst time left, which is less than P1's 4 units.

When P2 is finished, the CPU will be handed to P1. P3 will receive the CPU after P1, which has the next greatest priority, has run for three units. However, P1 will continue to run until it is finished since it will still have 1 unit of burst time left after 3 units of time, which is less than P3's 1 unit of burst time.

When P1 is finished, the CPU will be passed to P3. P4, which has the greatest priority among the remaining processes, will then be assigned the CPU once P3 has operated for one unit. P4 will proceed till it is finished.

The Gantt Chart can be prepared as −

Time

0

1

2

3

4

5

6

7

8

9

Process

P2

P2

P2

P1

P1

P1

P4

P4

P4

P4

The average waiting time of these processes is (0+3+1+6)/4 = 2.

Example 2

Consider the following processes −

Process

Arrival Time

Burst Time

Priority

P1

0

7

3

P2

1

5

2

P3

2

2

1

P4

3

4

4

Assume that these processes are scheduled using the Preemptive Priority CPU Scheduling Algorithm.

Prior to scheduling these processes, we must arrange them in priority order, starting with the one having the greatest priority −

Process

Arrival Time

Burst Time

Priority

P3

2

2

1

P2

1

5

2

P1

0

7

3

P4

3

4

4

We can see that P3 is allocated the CPU first since it has the highest priority. The CPU will go to P2, which has the second greatest priority, once P3 has run for 2 units. However, P3 will finish and P2 will keep running until it finishes since P3 has 0 units of burst time remaining after 2 units of time, which is less than P2's 5 units of burst time.

The CPU will be assigned to P1, which has the next highest priority, after P2 is finished. P1 will continue till it is finished. P4 will get the CPU at this point and run the program till it is finished.

The Gantt Chart can be prepared as −

Time

Process

0

P3

1

P3

2

P2

3

P2

4

P2

5

P2

6

P2

7

P1

8

P1

9

P1

10

P1

11

P1

12

P1

13

P4

14

P4

15

P4

16

P4

The average waiting time of these processes is (6+3+0+5)/4 = 3.5

Comparison With Other Scheduling Algorithms

In contrast to conventional scheduling algorithms, preemptive priority scheduling permits jobs with higher priority levels to interrupt processes with lower priority levels. In systems where the timely completion of some activities is essential, this feature makes sure that the higher priority tasks are carried out first.

The priority of tasks is not taken into account by other scheduling algorithms like round-robin scheduling and first-come, first-served scheduling. No matter how important a job is, round-robin scheduling executes each one for a certain period of time. Regardless of their significance, jobs are carried out according to the first-come, first-served scheduling method.

Conclusion

Task prioritization is a crucial part of operating systems, especially in multitasking systems where several activities are carried out at once. Operating systems employ the scheduling method known as preemptive priority scheduling to rank activities according to significance. Each job in this method is given a priority level, and the scheduler starts with the task with the highest level.

In contrast to conventional scheduling algorithms, preemptive priority scheduling permits jobs with higher priority levels to interrupt processes with lower priority levels. In systems where the timely completion of some activities is essential, this feature makes sure that the higher priority tasks are carried out first.

Several real-world applications, including real-time systems, multimedia systems, and operating system kernels, employ preemptive priority scheduling to ensure that tasks are completed on time. These systems may ensure that jobs with higher priority levels are done first by utilizing preemptive priority scheduling, which also allows the system to run effectively and efficiently.

Updated on: 19-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements