-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2
230 lines (212 loc) · 4.43 KB
/
Q2
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include<stdio.h>
#include<process.h>
void main()
{
int burst[20],p[20],wait_T[20],at[20],done[20],bt[20],tat[20],nominate[20],tot,pos,temp,temp1,size=0,i,k,l=0,j,total=0,TW=0,n,x;
int arrival_T[20];
float priority[20],temp2;
void init() // to initialize nominate array with 99999
{
int i;
for(i=0;i<tot;i++)
nominate[i]=99999;
}
void init2() // to initialize priority array with 99999.0
{int i;
for(i=0;i<tot;i++)
priority[i]=99999.0;
}
int prsize() // to get size of priority array(non 99999.0 elements)
{
int pos,size=0;
if(priority[0]!=99999.0)
{
pos=0;
while(priority[pos]!=99999.0)
{
size++;
pos++;
}
}
return size;
}
int nomsize() //to get the size of nominated array(non 99999 elements)
{
int pos,size=0;
if(priority[0]!=99999)
{
pos=0;
while(priority[pos]!=99999)
{
size++;
pos++;
}
}
return size;
}
//============================================================================= MAIN PROGRAM STARTS
printf("Enter number of processes(between 0 and 21) "); //getting total no. of processes
scanf("%d",&tot);
if(tot>=21)
{
printf("\nSTACK OVERFLOW");
exit(0);
}
init();
init2();
if(tot>=1)
{
printf("Enter Estimated Time:\n"); //getting burst time of all processes
for(i=0;i<tot;i++)
{
printf("p%d:",i+1);
scanf("%d",&burst[i]);
p[i]=i+1;
}
printf("\nEnter Arrival Time:\n"); //getting arrival time of all processes
for(i=0;i<tot;i++)
{
printf("p%d:",i+1);
scanf("%d",&at[i]);
}
for(i=0;i<tot-1;i++) //sorting processes,burst time based on arrival time
{
pos=i;
for(j=i+1;j<tot;j++)
{
if(at[j]<at[pos])
pos=j;
}
temp=at[i];
at[i]=at[pos];
at[pos]=temp;
temp=p[i];
temp1=burst[i];
p[i]=p[pos];
burst[i]=burst[pos];
p[pos]=temp;
burst[pos]=temp1;
}
//===================== initialization of done,bt,tat array for very first priority
k=0;
done[l]=p[0];
bt[l]=burst[0];
tat[l]=burst[0];
//=================================================================================
k++;l++;
wait_T[0]=0;
TW=burst[0]+TW; //tracking total time elapsed
i=1;
//=================== label
label:
n=nomsize();
x=0;
if(n==0) //if whole nomination array is 99999
{
for(i;i<tot;i++)
{
if(at[i]<=TW)
{
nominate[x]=i;
priority[x]=1.0+((float)TW-(float)at[nominate[x]])/(float)burst[nominate[x]];
x++;
}
}
}
else{
j=0;
while(nominate[j]!=99999)
{
priority[x]=1.0+((float)TW-(float)at[nominate[x]])/(float)burst[nominate[x]];
j++;
x++;
k++;
}
i=k;
for(i;i<tot;i++)
{
if(i<tot && at[i]<=TW)
{
nominate[x]=i;
priority[x]=1.0+((float)TW-(float)at[nominate[x]])/(float)burst[nominate[x]];
x++;
}
}
}
size=prsize();
if(size>1) //number of non 99999.0 elements in priority array
{ //sorting priority of process
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(priority[i]<priority[j])
{
temp2=priority[i];
temp=nominate[i];
priority[i]=priority[j];
nominate[i]=nominate[j];
priority[j]=temp2;
nominate[j]=temp;
}
}
}
}
if(size==1)
{
i=0;
done[l]=p[nominate[i]];
bt[l]=burst[nominate[i]];
tat[l]=burst[nominate[i]];
wait_T[l]=TW-at[nominate[i]];
k++;l++;
TW=TW+burst[nominate[i]];
}
if(size>1)
{
i=0;
done[l]=p[nominate[i]];
bt[l]=burst[nominate[i]];
tat[l]=burst[nominate[i]];
wait_T[l]=TW-at[nominate[i]];
k++;l++;
TW=TW+burst[nominate[i]];
i++;
while(nominate[i]!=99999)
{
temp=nominate[i];
temp2=priority[i];
nominate[i-1]=temp;
priority[i-1]=temp2;
i++;
}
nominate[i-1]=99999;
priority[i-1]=99999.0;
}
else
{
init();
init2();
}
k=l;
i=k;
j=0;
if(k>tot-1)
goto end;
goto label;
end:
for(i=0;i<tot;i++)
{
burst[i]=bt[i];
p[i]=done[i];
}
printf("\nSequence of processes\t Waiting time\t\t Burst Time\t\t Turnaround Time\n");
for(i=0;i<tot;i++)
printf("p%d\t\t\t\t%d\t\t\t%d\t\t\t%d\n",p[i],wait_T[i],burst[i],tat[i]+wait_T[i]);
}
else
{
printf("\nSORRY no processes found");
exit(0);
}
}