Name: Ajay Chauhan
Branch:CSIT-4A
Roll no: 2400290119001
Lab:-3
Object: Implement priority scheduling algorithm using c programming.
#include <stdio.h>
struct Process {
int id;
int burstTime;
int priority;
int waitingTime;
int turnaroundTime;
};
void sortByPriority(struct Process p[], int n) {
struct Process temp;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (p[i].priority > p[j].priority) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
printf("Enter Burst Time and Priority for Process %d: ", i + 1);
scanf("%d %d", &p[i].burstTime, &p[i].priority);
sortByPriority(p, n);
p[0].waitingTime = 0;
p[0].turnaroundTime = p[0].burstTime;
for (int i = 1; i < n; i++) {
p[i].waitingTime = p[i - 1].waitingTime + p[i - 1].burstTime;
p[i].turnaroundTime = p[i].waitingTime + p[i].burstTime;
}
printf("\nProcess\tBT\tPriority\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i].id, p[i].burstTime, p[i].priority, p[i].waitingTime,
p[i].turnaroundTime);
float totalWT = 0, totalTAT = 0;
for (int i = 0; i < n; i++) {
totalWT += p[i].waitingTime;
totalTAT += p[i].turnaroundTime;
printf("\nAverage Waiting Time = %.2f", totalWT / n);
printf("\nAverage Turnaround Time = %.2f\n", totalTAT / n);
return 0;
Output
Enter number of processes:
Enter Burst Time and Priority for Process 1: 4 2
Enter Burst Time and Priority for Process 2: 2 1
Enter Burst Time and Priority for Process 3: 6 3
Process BT Priority WT TAT
P2 2 1 0 2
P1 4 2 2 6
P3 6 3 6 12
Average Waiting Time = 2.67
Average Turnaround Time = 6.67