Let us learn how to implement priority scheduling algorithm in C programming with its explanation, output, advantages, disadvantages and much more.
What is Non-Preemptive Priority Scheduling Algorithm?
The priority scheduling algorithm is one of the most common algorithms for scheduling jobs in batch systems.
Every process is assigned a number which denotes the priority, and based on this priority the processes are executed. Therefore, the process having the highest priority (1) is executed first and then the priority 2, 3 and so on.
There can be some scenarios where more two or more processes may have the same priority. In this case, the processes are executed based on First In First Out order or in other words, First Come First Serve.
We do not consider the arrival time of the jobs in this non-preemptive priority scheduling algorithm. This means that unless a job gets completely executed, the CPU won’t leave the current job before it completes its execution.
Only once the process gets out of the job queue after successful execution, the CPU is allowed to process another job from the queue.
Advantages
- This algorithm is very simple to implement.
- The aging technique is implemented to reduce the starvation of lower priority processes.
Disadvantages
- Starvation or indefinite blockage of the lower priority processes.
- Since this is a non-preemptive implementation, the waiting time is comparatively higher.
- The average turnaround time is higher as compared to the preemptive priority scheduling algorithm.
- If a system failure occurs, all the unfinished lower priority jobs get vanished from the system.
Note: This implementation of non-preemptive priority scheduling program in C without arrival time is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.
C Program To Implement Non-Preemptive Priority Scheduling Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<stdio.h> int main() { int burst_time[20], process[20], waiting_time[20], turnaround_time[20], priority[20]; int i, j, limit, sum = 0, position, temp; float average_wait_time, average_turnaround_time; printf("Enter Total Number of Processes:\t"); scanf("%d", &limit); printf("\nEnter Burst Time and Priority For %d Processes\n", limit); for(i = 0; i < limit; i++) { printf("\nProcess[%d]\n", i + 1); printf("Process Burst Time:\t"); scanf("%d", &burst_time[i]); printf("Process Priority:\t"); scanf("%d", &priority[i]); process[i] = i + 1; } for(i = 0; i < limit; i++) { position = i; for(j = i + 1; j < limit; j++) { if(priority[j] < priority[position]) { position = j; } } temp = priority[i]; priority[i] = priority[position]; priority[position] = temp; temp = burst_time[i]; burst_time[i] = burst_time[position]; burst_time[position] = temp; temp = process[i]; process[i] = process[position]; process[position] = temp; } waiting_time[0] = 0; for(i = 1; i < limit; i++) { waiting_time[i] = 0; for(j = 0; j < i; j++) { waiting_time[i] = waiting_time[i] + burst_time[j]; } sum = sum + waiting_time[i]; } average_wait_time = sum / limit; sum = 0; printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n"); for(i = 0; i < limit; i++) { turnaround_time[i] = burst_time[i] + waiting_time[i]; sum = sum + turnaround_time[i]; printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i], turnaround_time[i]); } average_turnaround_time = sum / limit; printf("\nAverage Waiting Time:\t%f", average_wait_time); printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time); return 0; } |
Output

If you have any doubts about the implementation of non-preemptive priority scheduling in C programming, let us know about it in the comment section. For more details, check Wikipedia.
This is just a fantastic and so self – explanatory code for Priority Scheduling! Thanks a lot.
You’re welcome Javed! Do see more Data Structure Programs on CodingAlpha.
How do we process when two processes have the same priority?
If two processes have the same priority, then the process that comes first in the queue will be executed first.
The priority scheduling concept truly relates to real life task scheduling.
The main issue with Priority Scheduling algorithm is Starvation, also called as Indefinite Blocking. It is overcome by Aging method where priorities are automatically decreased or increased based on the situation.
Do you have a program relating to the concept of Aging in C language . If yes , then mail it to me .