In this article, we are taking a look at how to simulate the Priority Scheduling Algorithm using the C programming language.
I have commented out pretty much all the important lines of codes in this program so that you can easily read it line by line and understand what each line of code does.
In this program, for the sake of simplicity, we are assuming the arrival time of all processes as 0.

The logic is very simple and easy to understand.

1. Read the number of processes from the input

2. Read the priority and burst time of each process into appropriate arrays

3. Sort the entire table on the basis of Priority.

Note that compared to SJF and FCFS, here we introduced another term called Priority. So, we are sorting the entire table on the basis of the priority value of each process. Make sure to also change the corresponding PID values as well as the burst time values in the sorting steps, otherwise will lead to a priority, burst time, and PID mismatch in the table.

```//C Program to Simulate Priority CPU Scheduling Algorithm by Nived Kannada
#include<stdio.h>
void main()
{
int n, bt, at,p,wt,tat, priority ,i,j , total=0, pos, temp;
float avgwt=0, avgtat=0;

//Reading number of processes from input.
printf("Enter number of processes: ");
scanf("%d",&n);

//Reading all burst times and Priority of processes from input. Assuming Arrival time is 0 for all.
printf("Enter Burst Time and Priority value of each process: \n");
for(i=0;i<n;i++)
{
p[i]=i;
printf("Burst Time of P[%d]: ",i);
scanf("%d",&bt[i]);
printf("Priority of P[%d]: ",i);
scanf("%d",&priority[i]);
at[i]=0; //Assuming AT is zero for all.
}
//Sorting the table according to the PRIORITY Value (priority[i])
for(i=0;i<n;i++)
{
pos = i;
for(j=i+1;j<n;j++)
{
if(priority[j]<priority[pos])
{
pos=j;
}
}
temp=bt[i];
priority[i]=priority[pos];
priority[pos]=temp;

//Also sorting corresponding PID values.
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
//Also sorting Corresponding BT values
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
}
//After this sorting step, the array priority[] is in ascending order. So the first element in the table doesn't have to wait. Its wt is now 0

/* CALCULATING WAITING TIME */

wt=0; //The current first Process doesnt have to wait, (after sorting)

//Setting wt values for all other processes.
for(i=1;i<n;i++)    //wt of 0 is set as 0. So we start with index 1 of wt[].
{
wt[i]=0;    //initialize wt of i'th index as 0
for(j=0;j<i;j++)
{
wt[i] = wt[i] + bt[j]; // adding waiting times of previous processes and the current burst time.
}
}

/* CALCULATING TURNAROUND TIME */

for(i=0;i<n;i++)
{
tat[i] = wt[i] + bt[i];  //Equation for TAT is TAT= WT+BT
}

//Displaying Table
printf("\n\tPID\tAT\tBT\tWT\tTAT\n");
for(i=0;i<n;i++)
{
printf("\t%d\t%d\t%d\t%d\t%d\n",p[i],at[i],bt[i],wt[i],tat[i]);
}

//Calculating Average Waiting time and Average Turnaround Time
for(i=0;i<n;i++)
{
avgwt = avgwt + wt[i];
avgtat = avgtat + tat[i];
}
avgwt = avgwt/n;
avgtat = avgtat/n;

//Displaying Average WT and Average TAT
printf("\nAverage WT = %f\n", avgwt);
printf("\nAverage TAT = %f\n", avgtat);
}

//END OF PROGRAM
```