-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.java
More file actions
262 lines (245 loc) · 8.35 KB
/
Scheduler.java
File metadata and controls
262 lines (245 loc) · 8.35 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.util.ArrayList;
import java.util.Collections;
public class Scheduler {
ArrayList<Task> al = new ArrayList<Task>();
int sim_stop;
int num_tasks;
static int num_res = 3;
public Scheduler(ArrayList<Task> a,int s,int n )
{
this.al = a;
this.sim_stop = s;
this.num_tasks = n;
}
@SuppressWarnings("unchecked")
public int EDF_Scheduler()
{
ArrayList<Task> out = new ArrayList<Task>();
ArrayList<Task>[] res = new ArrayList[num_res];
for(int k=0; k<num_res; k++)
{
res[k] = new ArrayList<Task>();
}
Task currentprio_task = null;
int[] flag = new int[num_res] ;
for(int k=0; k<num_res; k++)
{
flag[k] = 0;
}
Lock[] locks = new Lock[num_res];
for(int k=0; k<num_res; k++)
{
int t = k+1;
String l = "R" + t;
locks[k] = new Lock(l,0);
}
int flagc = 0;
System.out.println();
System.out.println("Time\tJob\t\tActions\t\t\tCurrent priority");
System.out.println("--------------------------------------------------------------------------");
for(int i=0;i<=sim_stop;i++)
{
for(Task t:al)
{
if((t.p.jn * t.p.period) + t.p.phase == i)
{
out.add(t); //----->If phase is equal to time instant,add task to the queue
Collections.sort(out,new TaskComp()); //------->Sort the task queue
currentprio_task = out.get(0);
t.p.jn++;
}
}
/************************************* EDF Functionality ***************************************/
/*if(out.size()!=0)
{
System.out.println(i + "\t" + out.get(0).p.tn);
out.get(0).p.count++;
if(out.get(0).p.count == out.get(0).p.exe_time)
{
out.remove(0);
}
}
else
System.out.println(i + "\t" + "No task running");*/
/********************* EDF Functionality + PIP functionality ***************************************/
if(out.size()!=0)
{
flagc = 0;
int flagt =0;
for(int k=0; k<num_res; k++)
{
flag[k] = 0;
}
int[] start = new int[num_res];
for(int k=0; k < num_res ;k++)
{
try
{
start[k] = out.get(0).r.get(k).res_start + out.get(0).p.phase;
}catch(IndexOutOfBoundsException ie)
{
start[k] = 7777777;
}
if(i >= start[k] && !(out.get(0).r.get(k).res_count == out.get(0).r.get(k).res_len) && (out.get(0).p.count == out.get(0).r.get(k).res_start))
{
if(!((ArrayList<Task>) res[k]).contains(out.get(0)))
{
((ArrayList<Task>) res[k]).add(out.get(0));
}
if(locks[k].get_lock_taskname() == "")
{
flag[k] = locks[k].acquire_lock(out.get(0).p.tn); //-------->If resource1 is currently free,allocate it
}
else
{
if(locks[k].get_lock_taskname() == out.get(0).p.tn )
{
//continue; //------>If resource is allocated to the current task,continue
}
else
{
Task temp = null; //-->Otherwise, get the task that has the resource and put it in head
for(Task t:out) //--> of the queue and the highest priority task blocks on the resource
{
if(t.p.tn.equals(locks[k].get_lock_taskname()))
temp = t;
}
int index = out.indexOf(temp);
out.remove(index);
out.add(0, temp);
/************************Rescan resource request****************************/
for(int j=0; j < num_res ;j++)
{
try
{
start[j] = out.get(0).r.get(j).res_start + out.get(0).p.phase;
}catch(IndexOutOfBoundsException ie)
{
start[j] = 7777777;
}
if(i >= start[j] && !(out.get(0).r.get(j).res_count == out.get(0).r.get(j).res_len) && (out.get(0).p.count == out.get(0).r.get(j).res_start))
{
if(!((ArrayList<Task>) res[j]).contains(out.get(0)))
{
((ArrayList<Task>) res[j]).add(out.get(0));
}
else
{
/* int in = ((ArrayList<Task>) res[j]).indexOf(out.get(0));
((ArrayList<Task>) res[j]).remove(in);
((ArrayList<Task>) res[j]).add(0, out.get(0));*/
}
if(locks[j].get_lock_taskname() == "")
{
flag[j] = locks[j].acquire_lock(out.get(0).p.tn); //-------->If resource1 is currently free,allocate it
//System.out.println("\t" + flag[j]);
}
else
{
if(locks[j].get_lock_taskname() == out.get(0).p.tn )
{
//continue; //------>If resource is allocated to the current task,continue
}
}
}
}
}
}
}
}
System.out.print("|" + i + "|" + "\t" + "|" + out.get(0).p.tn + out.get(0).p.jn + "|");
out.get(0).p.count++;
for(int k=0; k < num_res ;k++)
{
try
{
if(((ArrayList<Task>) res[k]).size()!= 0 )
{
//System.out.println("k:" + k + "\t" + ((ArrayList<Task>) res[k]).get(0).p.tn);
if(((ArrayList<Task>) res[k]).contains(out.get(0)))
{
// System.out.println("In lock");
if(flag[k]==1)
{
// System.out.println("In lock:" + k);
int t = k+1;
System.out.print("\t lock R" + t );
//flag[k] = 0;
flagt =1;
}
//int in = ((ArrayList<Task>) res[k]).indexOf(out.get(0)) ;
out.get(0).r.get(k).res_count++;
}
}
}catch(IndexOutOfBoundsException ie)
{
}
}
for(int k=0; k < num_res ;k++)
{
try
{
if(((ArrayList<Task>) res[k]).get(((ArrayList<Task>) res[k]).indexOf(out.get(0))).r.get(k).res_count == ((ArrayList<Task>) res[k]).get(((ArrayList<Task>) res[k]).indexOf(out.get(0))).r.get(k).res_len)
{
int t = k+1;
System.out.print("\tUnlock R"+t);
((ArrayList<Task>) res[k]).get(0).r.get(k).res_count = 0;
locks[k].release_lock(out.get(0).p.tn);
((ArrayList<Task>) res[k]).remove(((ArrayList<Task>) res[k]).indexOf(out.get(0)));
flagt=1;
}
}
catch(IndexOutOfBoundsException ie)
{
}
}
for(int k=0; k < num_res ;k++)
{
try
{
if(!((currentprio_task.p.tn).equals(out.get(0).p.tn))) //-->If the highest priority task is executing, print it
{
if(flagc!=1)
{
if(flagt==1)
System.out.println("\t\t\t" + "|" + currentprio_task.p.tn + currentprio_task.p.jn + "|");
else
System.out.println("\t\t\t\t\t " + "|" + currentprio_task.p.tn + currentprio_task.p.jn + "|");
}
flagc = 1;
if(((ArrayList<Task>) res[k]).size()!= 0 && ((ArrayList<Task>) res[k]).contains(currentprio_task))
{
//System.out.println("In res k:" + ((ArrayList<Task>) res[k]).get(0).p.tn + "\t" + k);
Collections.sort(out,new TaskComp()); //------->Sort the task queue
if(((ArrayList<Task>) res[k]).get(0).p.tn.equals(currentprio_task.p.tn))
{
Collections.sort(out,new TaskComp()); //------->Sort the task queue
//System.out.println(out.get(0).p.tn);
}
}
}
}
catch(IndexOutOfBoundsException ie)
{
}
}
if(out.get(0).p.count == out.get(0).p.exe_time)
{
out.get(0).p.count = 0;
out.remove(0); //--->If the task execution requirement is done,remove it from queue
try{
currentprio_task = out.get(0);
}catch(IndexOutOfBoundsException ie)
{
//System.out.println("No task running");
}
}
System.out.println();
}
else
System.out.println("|" + i + "|" + "\t" + "/---------------No task to run---------------------/");
/****************************To check if a task has completed execution ****************/
}
return 0;
}
}