This is the Preemptive version of Priority scheduling. In this type of scheduling the processes which are present in ready queue are compared and the process with highest priority is selected for execution.
The difference between Non-Preemptive Priority Scheduling and Preemptive Priority Scheduling is that in non-preemptive if a process comes inside CPU then it can't be preempted and it will execute completely. However, in case of Preemptive Priority Scheduling a process which is executing inside CPU can be preempted if a higher priority process comes in the ready queue.
Numerical:
Schedule the processes given in the table below on the basis of Preemptive Priority Scheduling. Also draw the gantt chart, waiting time and turn around time for each process.
We will have to schedule the process for 1ms upto 5 ms because last arrival time for a process is 5ms which is P6.
We will take another table T to store remaining time for each process.
1. At 0 arrival time we have process P4 which will execute for 1ms. Now, remaining time for P4 is 0ms so it won't be added to table T.
2. At arrival time 1 we have process P1 which will execute for 1ms. Now, remaining time for P1 is 3ms so it will be added to table T.
3. At arrival time 2 we have process P2. Compare priority of P2 and P1. Clearly, P1 has higher priority so P1 will be scheduled again for 1ms.Now, remaining time for P1 is 2ms. P2 will also be added to table T with remaining time 5ms.
4. At arrival time 3 we have process P3. Compare priority of P2, P1, P3. Clearly, P3 has higher priority so P3 will be scheduled for 1ms.Now, remaining time for P3 is 5ms. P3 will also be added to table T with remaining time 5ms.
5. At arrival time 4 we have process P5. Compare priority of P2, P1, P3, P5. Clearly, P5 has higher priority so P5 will be scheduled for 1ms.Now, remaining time for P5 is 1ms. P5 will also be added to table T with remaining time 1ms.
6. At arrival time 5 we have process P6. Compare priority of P2, P1, P3, P5, P6. Clearly, P6 has higher priority so P6 will be scheduled.Since no process have arrived after P6 thus P6 will execute for complete 3ms.
7. P6 has completed execution.
8. Remaining process in table T are P2, P1, P3, P5. Clearly, P5 has highest priority so P5 will be scheduled for its remaining time which is 1ms.
9. P5 has completed execution.
10. Remaining process in table T are P2, P1, P3. Clearly, P3 has highest priority so P3 will be scheduled for its remaining time which is 5ms.
11. P3 has also completed execution.
12. Remaining process in table T are P2, P1. Clearly, P1 has highest priority so P1 will be scheduled for its remaining time which is 2ms.
13. P1 has also completed execution.
14. Remaining process in table T are P2 so P2 will be executed for 5ms.
15. P2 has also completed execution.
Gantt Chart
Waiting Time for each process
Waiting time is computed by the formula starting execution time - arrival time of a process.
Process Starting execution time - arrival time Waiting time
P1 15 - 4 11
P2 19 - 5 14
P3 11 - 6 5
P4 1 - 1 0
P5 5 - 2 3
P6 3 - 3 0
Turn around Time for each Process
Turn around time is computed by the formula completion time - arrival time of a process.
Process Completion time - arrival time Waiting time
P1 16 - 1 15
P2 21 - 2 19
P3 14 - 3 11
P4 1 - 0 1
P5 9 - 4 5
P6 8 - 5 3
The output might not be clear but you guys can confirm by running it on your pc.
If you are interested then you can follow me on :