Priority (Preemptive) Algorithm
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.
Characteristics
- Priority scheduling is one of the most common scheduling algorithms in batch systems.
- Each process is assigned a priority. Process with highest priority is to be executed first and so on.
- Processes with same priority are executed on first come first served basis.
- Priority can be decided based on memory requirements, time requirements or any other resource requirement.
Advantages
- It is one of the simplest and easiest form of CPU Scheduling Algorithm.
- Process having high priority doesn't have to wait for a long time.
- It is used in the places where time varies eventually along with resources.
Disadvantages
- In this type of algorithm, process having highest priority compared to currently executing process have to wait till
execution ends
- Some low priority processes can be in waiting state for an indefinite time.
- On system crash, low priority processes shall not be scheduled.
- Resource Utilization in parallel is not possible here.
Example
Process |
Priority |
Arrival Time |
Burst Time |
P1 |
10(L) |
0 |
5 |
P2 |
20 |
1 |
4 |
P3 |
30 |
2 |
2 |
P4 |
40(H) |
4 |
10 |
Working
- Since at T = 0 only P1 has arrived, it will get executed first for 1 time unit despite having the lowest priority
- At T = 1 P2 has arrived and it has higher priority than P1,So P2 will go in the running queue and P1 will be premepted to ready queue.
- At T = 2 P3 has arrived and it has higher priority than P2,So P3 will go in the running queue and P2 will be premepted to ready queue.
- At T = 3 no new process will come, so P3 will run for another time unit and it will be terminated
- At T=4 P4 will come in ready queue and it has highest priority and it will run for 1 time unit and get terminated.
- At T =5 all processes are arrived, so P2 will come in running queue from ready queue and it will run for 3 time unit and get terminated.
- At T=8 only P1 process is there in ready queue and it will run for 4 time unit and get terminated.
- At T=12 all process will be terminated.
Gantt Chart
P1 |
P2 |
P3 |
P4 |
P2 |
P1 |
0 - 1 |
1 - 2 |
2 - 4 |
4 - 5 |
5 - 8 |
8 - 12 |
Final Table
Process |
Priority |
Arrival Time |
Burst Time |
Completion Time |
Turn Around Time |
Waiting Time |
Response Time |
P1 |
10(L) |
0 |
5 |
12 |
12 |
7 |
0 |
P2 |
20 |
1 |
4 |
8 |
7 |
3 |
0 |
P3 |
30 |
2 |
2 |
4 |
2 |
0 |
0 |
P4 |
40(H) |
4 |
1 |
5 |
1 |
0 |
0 |