C Program To Implement Preemptive Priority Scheduling Algorithm
Learn How To Implement Preemptive Priority Scheduling Algorithm in C Programming Language. The Pre – emptive Priority Scheduling Algorithm is a Disk Scheduling Algorithm used in Operating System process management. The Priority CPU Scheduling Algorithm is normally represented using Gantt’s Chart on paper.
This Priority Scheduling Algorithm uses Arrival Time and consists of Structures instead of Arrays. The Algorithm is a Preemptive Algorithm. It is much more efficient than the Non Preemptive Algorithm as it helps to efficiently manage the Turnaround time and waiting time as well.
What is Preemptive Priority Scheduling Algorithm?
The Priority Scheduling Algorithm using Structure primarily works on the priorities assigned to different processes in the Ready Queue. Every process is assigned a particular priority and the process with the highest priority will get executed above all the other processes in the ready queue.
Since this is a Preemptive Scheduling Algorithm, the CPU can leave the process midway. That means, the CPU can be taken away from a process in execution and start execution of any other process either in the Ready Queue or the Waiting Queue. The state of the previous incomplete process is saved and the CPU can resume from the saved state for that process.
C Program For Preemptive Priority Scheduling Algorithm with Arrival Time
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 or compilation errors in this C Program to Implement Priority Scheduling Algorithm using Preemptive technique, Arrival time and Structures, let us know about in the Comment Section below. Check Careerride for more information on Preemptive Scheduling Algorithms.