//[ Issue-Fix ] How to implement Priority Based CPU scheduling Algorithm using C program ?

[ Issue-Fix ] How to implement Priority Based CPU scheduling Algorithm using C program ?

How to implement Priority Based CPU scheduling Algorithm using C program  to find average turnaround and waiting time ?

Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned first arrival time (less arrival time process first) if two processes have same arrival time, then compare to priorities (highest process first). Also, if two processes have same priority then compare to process number (less process number first). This process is repeated while all process get executed.Below is the C program to implement priority scheduling algorithm

C program for Priority Scheduling without Arrival Time 

#include<stdio.h>

int main()
{
int burst[20],p[20],waiting[20],turnaround[20],priority[20],i,j,limit,total=0,pos,temp,avg_wt,avg_tat;
printf("\n\n\nProgram to calculating average Turnaround and Waiting Time for Priority CPU scheduling \n\n Developed and compiled : Pineapplem3.com\n=====================================================================================================\n");
printf("\nEnter Total Number of Process : ");
scanf("%d",&limit);

printf("\nEnter Burst Time and Priority for %d processes \n============================================================\n",limit);
for(i=0;i<limit;i++)
{

printf("\nEnter Burst Time of process %d : ",i+1);
scanf("%d",&burst[i]);
printf("\nEnter Priority or Rank of process %d : ",i+1);
scanf("%d",&priority[i]);
p[i]=i+1; //contains process number
}

printf("\n===========================================================================\n\nBurst Time and Priority Added sucessfully ! \n\n");

//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<limit;i++)
{
pos=i;
for(j=i+1;j<limit;j++)
{
if(priority[j]<priority[pos])
pos=j;
}

temp=priority[i];
priority[i]=priority[pos];
priority[pos]=temp;

temp=burst[i];
burst[i]=burst[pos];
burst[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

waiting[0]=0; //waiting time for first process is zero

//calculate waiting time
for(i=1;i<limit;i++)
{
waiting[i]=0;
for(j=0;j<i;j++)
waiting[i]+=burst[j];

total+=waiting[i];
}

avg_wt=total/limit; //average waiting time
total=0;
printf("\nProcessing...............................DONE!\nDisplaying Output..................DONE!\n");

printf("\nProcess\t\tBurst Time \tWaiting Time \tTurnaround Time\n\n");
for(i=0;i<limit;i++)
{
turnaround[i]=burst[i]+waiting[i]; //calculate turnaround time
total+=turnaround[i];
printf("\nProcess %d\t| %d\t\t| %d\t\t|\t\t%d",p[i],burst[i],waiting[i],turnaround[i]);
}
printf("\n\n===============================================================================\nCalculating Average Waiting Time and Turnaround Time");
avg_tat=total/limit; //average turnaround time
printf("\n\nAverage Waiting Time = %d",avg_wt);
printf("\nAverage Turnaround Time = %d\n",avg_tat);
printf("\nProgram success !\nDeveloped & Compiled : pineapplem3.com\n\n"); 

return 0;
}

 

Output :

 

 

Nirmal Ram is the Founder & CEO of pineapplem3 Inc , Co-Founder of Gene Ai Foundation & Researcher in Artificial Intelligence & Machine Learning. Born on April 1998 in Kerala ,India . He's a passionate Programmer & Developer . He believed in his work with ethics and filled with confidence. His start-up journey begins on 2016 , Skilled in handling 10+ computer Languages .He's Hard worker , Freelancer & Instructor . He started earning with his Freelancing Journey since 2017. brought up pineapplem3.com worth about $67.16 in 2016.