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.
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.
At , 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.
At , 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.
At , 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.
At , 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.
At , 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.
At , 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.
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 |
Free Resources