Loading…
OS · CPU scheduling · Program 16 · In the record
Write a program in c to implement the priority bases scheduling algorithm
1// A lower number means a higher priority2 3#include <stdio.h>4 5int main()6{7 int n, i, j, temp;8 int p[20], bt[20], pr[20], wt[20], tat[20];9 float total_wt = 0, total_tat = 0;10 11 printf("Enter number of processes: ");12 scanf("%d", &n);13 14 printf("Enter burst time and priority for each process:\n");15 for (i = 0; i < n; i++)16 {17 printf("P%d: ", i + 1);18 scanf("%d %d", &bt[i], &pr[i]);19 p[i] = i + 1;20 }21 22 // Sort processes by priority (selection sort)23 for (i = 0; i < n - 1; i++)24 {25 for (j = i + 1; j < n; j++)26 {27 if (pr[j] < pr[i])28 {29 temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;30 temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;31 temp = p[i]; p[i] = p[j]; p[j] = temp;32 }33 }34 }35 36 wt[0] = 0;37 for (i = 1; i < n; i++)38 wt[i] = wt[i - 1] + bt[i - 1];39 40 for (i = 0; i < n; i++)41 {42 tat[i] = wt[i] + bt[i];43 total_wt += wt[i];44 total_tat += tat[i];45 }46 47 printf("\nProcess\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");48 for (i = 0; i < n; i++)49 printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i], pr[i], bt[i], wt[i], tat[i]);50 51 printf("\nAverage Waiting Time = %.2f\n", total_wt / n);52 printf("Average Turnaround Time = %.2f\n", total_tat / n);53 54 return 0;55}4 ⏎ 10 3 ⏎ 1 1 ⏎ 2 4 ⏎ 5 2Enter number of processes: 4 Enter burst time and priority for each process: P1: 10 3 P2: 1 1 P3: 2 4 P4: 5 2 Process Priority Burst Time Waiting Time Turnaround Time P2 1 1 0 1 P4 2 5 1 6 P1 3 10 6 16 P3 4 2 16 18 Average Waiting Time = 5.75 Average Turnaround Time = 10.25
Run program 16 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.