Let us learn how to implement preemptive priority scheduling algorithm in C programming with its explanation, output, advantages, disadvantages and much more.
What is Preemptive Priority Scheduling Algorithm?
The preemptive priority scheduling algorithm is a popular operating system process management and job scheduling algorithm.
Every job that enters the job queue is assigned a priority based on which its execution takes place. As simple it sounds, the processes with a higher priority will be executed first and then the processes with the lower priorities will be executed.
If there are multiple processes in the queue with the same priority, then such jobs are executed in the order of their arrival often called as first come first served.
In this preemptive implementation of priority scheduling program in C, we consider the arrival time of the processes.
Since this is a preemptive job scheduling algorithm, the CPU can leave the process midway. The current state of the process will be saved by the context switch.
The system can then search for another process with a higher priority in the ready queue or waiting queue and start its execution.
Once the CPU comes back to the previous incomplete process, the job is resumed from where it was earlier paused.
Advantages
- Preemptive priority scheduling is much more efficient as compared to the non-preemptive version.
- This priority job scheduling algorithm is quite simple to implement.
- The aging technique is implemented to reduce the starvation of lower priority processes.
- The average turnaround time and waiting time is efficient.
Disadvantages
- Indefinite blockage of the lower priority jobs.
- For a system failure occurs, the unfinished lower priority jobs are removed from the system and cannot be recovered.
Note: This implementation of preemptive priority scheduling program in C with arrival time is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.
Preemptive Priority Scheduling Algorithm in C Programming
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 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include<stdio.h> struct process { char process_name; int arrival_time, burst_time, ct, waiting_time, turnaround_time, priority; int status; }process_queue[10]; int limit; void Arrival_Time_Sorting() { struct process temp; int i, j; for(i = 0; i < limit - 1; i++) { for(j = i + 1; j < limit; j++) { if(process_queue[i].arrival_time > process_queue[j].arrival_time) { temp = process_queue[i]; process_queue[i] = process_queue[j]; process_queue[j] = temp; } } } } void main() { int i, time = 0, burst_time = 0, largest; char c; float wait_time = 0, turnaround_time = 0, average_waiting_time, average_turnaround_time; printf("\nEnter Total Number of Processes:\t"); scanf("%d", &limit); for(i = 0, c = 'A'; i < limit; i++, c++) { process_queue[i].process_name = c; printf("\nEnter Details For Process[%C]:\n", process_queue[i].process_name); printf("Enter Arrival Time:\t"); scanf("%d", &process_queue[i].arrival_time ); printf("Enter Burst Time:\t"); scanf("%d", &process_queue[i].burst_time); printf("Enter Priority:\t"); scanf("%d", &process_queue[i].priority); process_queue[i].status = 0; burst_time = burst_time + process_queue[i].burst_time; } Arrival_Time_Sorting(); process_queue[9].priority = -9999; printf("\nProcess Name\tArrival Time\tBurst Time\tPriority\tWaiting Time"); for(time = process_queue[0].arrival_time; time < burst_time;) { largest = 9; for(i = 0; i < limit; i++) { if(process_queue[i].arrival_time <= time && process_queue[i].status != 1 && process_queue[i].priority > process_queue[largest].priority) { largest = i; } } time = time + process_queue[largest].burst_time; process_queue[largest].ct = time; process_queue[largest].waiting_time = process_queue[largest].ct - process_queue[largest].arrival_time - process_queue[largest].burst_time; process_queue[largest].turnaround_time = process_queue[largest].ct - process_queue[largest].arrival_time; process_queue[largest].status = 1; wait_time = wait_time + process_queue[largest].waiting_time; turnaround_time = turnaround_time + process_queue[largest].turnaround_time; printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d", process_queue[largest].process_name, process_queue[largest].arrival_time, process_queue[largest].burst_time, process_queue[largest].priority, process_queue[largest].waiting_time); } average_waiting_time = wait_time / limit; average_turnaround_time = turnaround_time / limit; printf("\n\nAverage waiting time:\t%f\n", average_waiting_time); printf("Average Turnaround Time:\t%f\n", average_turnaround_time); } |
Output

If you have any doubts about the implementation of preemptive priority scheduling algorithm in C programming, let us know about it in the comment section. For more details, check CareerRide.
Can we use Arrays to write a code for Preemptive Priority Scheduling Algorithm?
Is this a preemptive priority algorithm or a non preemptive priority algorithm?
The code is definitely not preemptive priority scheduling.This is because as soon as the B process with 1 priority came,the execution of process A will stop and process A has to wait for a time of 54 for B to complete.
Your waiting time for A is 0 that is WRONG.
The Priority Scheduling algorithm is a apecial case of the Shortest Job First CPU scheduling algorithm.
that is nice !! but i want to implement multilevel priority packet scheduling in ns2 . but i didn’t get any hint to do it . is there any one who can send me the source code ??
nice but it should be better to provide gantt chart with the program in order to understand betterly
what does process_queue[9].priority = -9999; mean?how does one know what is the highest priority