Priority Scheduling Is A CPU Scheduling
Priority Scheduling Is A CPU Scheduling
P1 6 2
P2 10 1
P3 4 3
P4 6 2
Let us compute the average turnaround time and average waiting time
from the above chart.
Average Turnaround Time
= Sum of Turnaround Time of each Process/ Number of Processes
= ( TATP1 + TATP2 + TATP3 + TATP4) / 4
= ( 16 + 10 + 26 + 22) / 4 = 18.5 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= ( WTP1 + WTP2 + WTP3 + WTP4) / 4
= ( 10 + 0 + 22 + 16) / 4 = 10.5 ms
Example 2
In this example, we consider a situation when the processes arrive at
different times. Suppose we have set of four processes whose arrival
times, CPU burst times and priorities are as follows −
P1 0 6 2
P2 4 10 1
P3 4 4 2
P4 8 3 1
Let us draw the GANTT chart and find the average turnaround time and
average waiting time using non-preemptive priority scheduling,
considering lower priority value as higher priority.
GANTT Chart
At time 0ms, only P1 is there and so it is assigned to CPU. P1 completes
execution at 6ms and at that time P2 and P3 have arrived. P2 has higher
priority and hence assigned to CPU. P2 completes execution at time
10ms. By that time P4 has arrived having priority value 1 and so it is
assigned to CPU. Once P4 completes execution, P3 is assigned to CPU.
So the order of execution is P1, P2, P4, P3 as shown in the following
GANTT chart −
Let us calculate the turnaround time of each process and hence the
average.
Turnaround Time of a process = Completion Time Arrival Time
TATP1 = CTP1 - ATP1 = 6 - 0 = 6ms
TATP2 = CTP2 - ATP2 = 16 - 4 = 12ms
TATP3 = CTP3 - ATP3 = 23 - 4 = 19ms
TATP4 = CTP4 - ATP4 = 19 - 8 = 11ms
Average Turnaround Time
= Sum of Turnaround Time of each Process / Number of Processes
= ( 6 + 12+ 19 + 11) / 4 = 12 ms
The waiting time is given by the time that each process waits in the
ready queue. For a non-preemptive scheduling algorithm, waiting time
of each process can be simply calculated as −
Waiting Time of any process = Time of admission to CPU Arrival Time
WTP1 = 0 - 0 = 0ms
WTP2 = 6 - 4 = 2ms
WTP3 = 19 - 4 = 15ms
WTP4 = 16 - 8 = 8ms
Average Waiting Time
= Sum of Waiting Time of Each Process / Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= ( 0 + 2 +15 + 8) / 4 = 6.25 ms
Example of Preemptive Priority Scheduling
In preemptive priority scheduling, if a process arrives that has higher
priority than the executing process, then the higher priority process
pre-empts the lower priority process. Let us consider the following set
of processes whose arrival times, burst times and priorities are give in
the following table −
P1 0 8 3
P2 4 10 2
P3 4 3 3
P4 10 4 1
GANTT Chart
At time 0ms, P1 is the only process and so it starts executing. At time
4ms, P2 and P3 arrive. Since P2 has higher priority than P1, P2
preempts P1. At 10ms, P4 arrives which has higher priority than P2 and
so pre-empts P2. P4 completes execution at 14ms and leaves. The
processes in the system are P1, P2 and P3, among which P2 has highest
priority and so is assigned to CPU. At 18ms, P2 completes execution and
so P1 and P3 are the processes in the system. Since both processes are
of same priority, the scheduler selects P1 by FCFS method. When P1
completes execution, finally P3 executes. The following GANTT chart
shows the order of execution −
From the GANTT chart, we compute the average turnaround time and
the average waiting time.
Average Turnaround Time
= Sum of Turnaround Time of each Process/ Number of Processes
= (TATP1 + TATP2 + TATP3 + TATP4) / 4
= ((22-0) + (18-4) + (25-4) + (14-10)) / 4 = 15.25 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= (14 + 4 + 18 +0) / 4 = 7 ms
Advantages of Priority Scheduling
Implementation is simple since scheduler doesnt require doing
any prior calculations.
Once CPU defines the relative relevance (priorities) of the
processes, the order of execution is easily predictable.
Higher priority processes are almost served immediately.
Priority scheduling is particularly helpful in systems that have
variety of processes each with their own needs.
Disadvantages of Priority Scheduling
In static priority systems, lower priority processes may need to
wait indefinitely since the system is busy executing higher priority
processes. This results in stagnation.
Dynamic priority solves the stagnation problem. However, the
added logic of dynamically updating priority values according to
the system requires additional CPU cycles and thus increases load
on the system.
In non-preemptive priority scheduling, often a large process keeps
shorter processes waiting for long time.
In preemptive priority scheduling, a low priority process may be
repeatedly pre-empted by intermittent streams of high priority
processes requiring frequent context switches