-
Notifications
You must be signed in to change notification settings - Fork 1
/
priorityscheduling.c
94 lines (79 loc) · 2.01 KB
/
priorityscheduling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the number of processes: ");
int n;
scanf("%d", &n);
int *bt = (int *)malloc(n * sizeof(int));
int *priority = (int *)malloc(n * sizeof(int));
int *p_id = (int *)malloc(n * sizeof(int));
int *wt = (int *)malloc(n * sizeof(int));
int *tat = (int *)malloc(n * sizeof(int));
printf("Enter the burst time for each process: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &bt[i]);
}
printf("Enter the priority for each process: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &priority[i]);
}
for (int i = 0; i < n; i++)
{
p_id[i] = i + 1;
}
// selection sort
for (int i = 0; i < n - 1; i++)
{
int min = priority[i];
int min_index = i;
for (int j = i + 1; j < n; j++)
{
if (priority[j] < min)
{
min = priority[j];
min_index = j;
}
}
int temp = priority[i];
priority[i] = priority[min_index];
priority[min_index] = temp;
temp = bt[i];
bt[i] = bt[min_index];
bt[min_index] = temp;
temp = p_id[i];
p_id[i] = p_id[min_index];
p_id[min_index] = temp;
}
int t = 0;
int done = 0;
while (done != n)
{
for (int i = 0; i < n; i++)
{
t += bt[i];
tat[i] = t;
wt[i] = tat[i] - bt[i];
done++;
}
}
printf("Process ID\tBurst Time\tPriority\tTurnaround Time\tWaiting Time\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p_id[i], bt[i], priority[i], tat[i], wt[i]);
}
float avg_wt = 0;
float avg_tat = 0;
for (int i = 0; i < n; i++)
{
avg_wt += wt[i];
avg_tat += tat[i];
}
avg_wt /= n;
avg_tat /= n;
printf("Average waiting time: %f\n", avg_wt);
printf("Average turnaround time: %f\n", avg_tat);
return 0;
}