A priority is associated with each process, and the CPU is allocated to the process with the highest priority.
Priority may be static or dynamic.
Static priority: It doesn’t change priority throughout the execution of the process
Dynamic priority: In this dynamic priority, priority can be changed by the scheduler at a regular interval of time.
An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst, the larger the CPU burst, the lower the priority, and vice versa.
Priority scheduling generally has two modes; (i) Non-preemptive (ii) Preemptive.
Some systems use low numbers to represent low priority; others use low numbers for high priority. This difference can lead to confusion but here we assume that low numbers represent low priority.
Criteria: Priority
Mode: Non preemptive or preemptive
Example 1: (Non-preemptive priority scheduling): Lowest number has low priority
Gantt Chart:
Now we calculate Turn Around Time (TAT) and Waiting Time (WT) using the following formula:
TAT = CT - AT, WT = TAT – BT
And
Response Time (RT) = FR (First Response) - AR (Arrival Time)
Note: Here waiting time and response time same because it is non-preemptive. For every non-preemptive scheduling waiting time = response time.
Example 2: (Preemptive priority scheduling) Lowest number has low priority:
Gantt Chart:
Number of Context Switching (CS) = 10
Now we calculate Turn Around Time (TAT) and Waiting Time (WT) using the following formula:
TAT = CT - AT, WT = TAT – BT
And
Response Time (RT) = FR (First Response) - AR (Arrival Time)
Disadvantages:
A major problem with priority scheduling algorithms is indefinite blocking, or starvation. A priority scheduling algorithm can leave some low priority processes waiting indefinitely. In a heavily loaded computer system, a steady stream of higher-priority processes can prevent a low-priority process from ever getting the CPU.
Aging:
A solution to the problem of indefinite blockage of low-priority processes is aging. Aging involves gradually increasing the priority of processes that wait in the system for a long time.
For example
If priorities range from 127 (low) to 0 (high), we could increase the priority of a waiting process by 1 every 15 minutes. Eventually, even a process with an initial priority of 127 would have the highest priority in the system and would be executed. In fact, it would take no more than 32 hours for a priority -127 process to age to a priority -0 process.