-
Notifications
You must be signed in to change notification settings - Fork 129
/
Priority4.c
97 lines (68 loc) · 1.38 KB
/
Priority4.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
95
96
97
#include <stdio.h>
void main()
{
int n, i, t, bt [20], at [20], j, sum = 0, bt1 [20], wt[20], tat[20], p [20], pro [30], index, pr=-1, temp = -1;
float avg_wt=0, avg_tat=0;
printf ("Enter number of processes:");
scanf ("%d", &n);
for(i=0;i<n; i++)
{ printf ("\n Arrival Time of P%d=", i+1);
scanf ("%d", & at [i]);
printf ("\n Bust Time of P%d= ", i+1);
scanf ("%d", & bt[i]);
printf ("\n Priority of P%d=", i+1);
scanf ("%d", &p[i]);
pro[i] = i;
printf ("\n");
}
for (i=0;i<n; i++) {
for(j=i+1; j<n; j++) {
if (at[i]> at [j])
{
temp= at [i];
at [i] = at [j];
at [j] = temp;
temp = bt[i];
bt[i]=bt[j];
bt [j] = temp;
temp= p[i];
p[i]=p[j];
p[j] = temp;
temp= pro [i];
pro[i]=pro[j];
pro[j] = temp;
}
}
sum=sum+bt[i];
bt1[i] = bt[i];
wt [i]=0;
tat[i]=0;
if (temp<p[i])
temp=p[i];
}
t=0;
while (t<sum) {
pr= temp+1;
for (i=0; i<=n; i++)
{
if (bt[i]>0 && at[i]<=t)
if (pr>p[i])
{
pr=p[i];
index=i;
}
}
bt[index]=0;
wt [index] = t-at [index];
tat [index] = wt [index]+ bt1[index];
t = t+bt1 [index];
}
printf ("\n Process\t Arival Time \t Burst Time \t Waiting Time \t Turnaround Time \n");
for (i=0;i<n; i++)
{
printf ("p%d\t%d\t%d\t%d\t%d\n", (pro[i]+1), at [i], bt1[i], wt[i],tat[i]);
avg_wt= avg_wt + wt[i];
avg_tat= avg_tat + tat[i];
}
printf ("\n Average waiting time = %f\n Average turnaround time = %f",avg_wt/n,avg_tat/n);
}