|
| 1 | +// Program to Implement Priority Scheduling (Preemptive) |
| 2 | +// Rohit Kumar | 1900320100131 |
| 3 | +#include <stdio.h> |
| 4 | +// Function to Swap two Numbers |
| 5 | +void swap(int *a, int *b) |
| 6 | +{ |
| 7 | + int temp; |
| 8 | + temp = *a; |
| 9 | + *a = *b; |
| 10 | + *b = temp; |
| 11 | +} |
| 12 | +// Driver Code |
| 13 | +void main() |
| 14 | +{ |
| 15 | + int n; |
| 16 | + // STDIN |
| 17 | + printf("No of Process : "); |
| 18 | + scanf("%d", &n); |
| 19 | + int AT[n], BT[n], PR[n], CT[n], WT[n], TAT[n], RBT[n]; |
| 20 | + printf("Enter the AT,BT and Priority of Process\n"); |
| 21 | + for (int i = 0; i < n; i++) |
| 22 | + { |
| 23 | + printf("P[%d]\t", i); |
| 24 | + scanf("%d%d%d", &AT[i], &BT[i], &PR[i]); |
| 25 | + } |
| 26 | + |
| 27 | + float avgWT = 0.0; |
| 28 | + float avgTAT = 0.0; |
| 29 | + |
| 30 | + // Loop for Sorting Process on the Basic of Arrival time |
| 31 | + for (int i = 0; i < n; i++) |
| 32 | + { |
| 33 | + for (int j = 0; j < n - 1 - i; j++) |
| 34 | + { |
| 35 | + if (*(AT + j) > *(AT + j + 1)) |
| 36 | + { |
| 37 | + swap((AT + j), (AT + j + 1)); |
| 38 | + swap((BT + j), (BT + j + 1)); |
| 39 | + swap((PR + j), (PR + j + 1)); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + int sum = 0; |
| 45 | + // Loop to Copy Burst Time to Rough Burst Time & calculating Sum of Burst Time of all Process |
| 46 | + for (int i = 0; i < n; i++) |
| 47 | + { |
| 48 | + RBT[i] = BT[i]; |
| 49 | + sum += BT[i]; |
| 50 | + } |
| 51 | + |
| 52 | + int time = AT[0], min, k; |
| 53 | + // Loop for Calculation of smallest Process |
| 54 | + // Takes Each Process by 1 Unit |
| 55 | + for (int i = 0; i < sum; i++) |
| 56 | + { |
| 57 | + min = 100; |
| 58 | + k = 0; |
| 59 | + for (int j = 0; j < n; j++) |
| 60 | + { |
| 61 | + if (RBT[j] != 0) |
| 62 | + { |
| 63 | + if (AT[j] <= time) |
| 64 | + { |
| 65 | + if (PR[j] < min) |
| 66 | + { |
| 67 | + min = PR[j]; |
| 68 | + k = j; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + RBT[k] = RBT[k] - 1; |
| 75 | + time = time + 1; |
| 76 | + CT[k] = time; |
| 77 | + } |
| 78 | + // Calculation of Burst Time |
| 79 | + for (int i = 0; i < n; i++) |
| 80 | + { |
| 81 | + WT[i] = CT[i] - AT[i] - BT[i]; |
| 82 | + TAT[i] = CT[i] - AT[i]; |
| 83 | + avgTAT += TAT[i]; |
| 84 | + avgWT += WT[i]; |
| 85 | + } |
| 86 | + |
| 87 | + avgWT = avgWT / n; |
| 88 | + avgTAT = avgTAT / n; |
| 89 | + // STDOUT |
| 90 | + printf("\n\t\tAT\tBT\tPR\tWT\tTAT\n"); |
| 91 | + for (int i = 0; i < n; i++) |
| 92 | + { |
| 93 | + printf("P[%d]\t%d\t%d\t%d\t%d\t%d\n", i, AT[i], BT[i], PR[i], WT[i], TAT[i]); |
| 94 | + } |
| 95 | + printf("\nAverage Waiting Time is %f Units\n", avgWT); |
| 96 | + printf("Average Turn Around Time is %f Units", avgTAT); |
| 97 | +} |
0 commit comments