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.
- 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.
- 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
int arrival_time, burst_time, ct, waiting_time, turnaround_time, priority;
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;
int i, time = 0, burst_time = 0, largest;
float wait_time = 0, turnaround_time = 0, average_waiting_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
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");
process_queue[i].status = 0;
burst_time = burst_time + process_queue[i].burst_time;
process_queue.priority = -9999;
printf("\nProcess Name\tArrival Time\tBurst Time\tPriority\tWaiting Time");
for(time = process_queue.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);
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.priority = -9999; mean?how does one know what is the highest priority