-
Notifications
You must be signed in to change notification settings - Fork 1
/
myscheduler.cpp
312 lines (260 loc) · 8.81 KB
/
myscheduler.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//myschedule.cpp
/*Define all the functions in 'myschedule.h' here.*/
/*
Group members: Ghunley Zhang, Jinwei Liu, Kevin Jiang
Date last modified: 3/4/2017
Due: 3/5/2017 11:59pm
Description: In this project we implement a schduler for a n-processor architecture.
It would work for four policies, FCFS, SRTFwoP, SRTFwP, and PBSwP.
*/
#include "myscheduler.h"
// constructor
MyScheduler::MyScheduler(Policy p, unsigned int n) : Scheduler(p, n) {
// initialize the data structure
threadVector.clear();
num_scheduledCPU = 0;
clear_CPU();
}
// destructor
MyScheduler::~MyScheduler() {
threadVector.clear();
orderedVector.clear();
clear_CPU();
}
void MyScheduler::CreateThread(int arriving_time, int remaining_time, int priority, int tid) //Thread ID not Process ID
{
//Function to Create Thread(s) and insert them in the student
//defined data structure
// create a thread using passed-in values
ThreadDescriptorBlock *t = new ThreadDescriptorBlock();
t->arriving_time = arriving_time;
t->remaining_time = remaining_time;
t->priority = priority;
t->tid = tid;
// add newly created thread to the vector
ThreadsStatus *thread = new ThreadsStatus();
thread->thread = t;
thread->isScheduled = false;
thread->CPU_id = 0;
thread->isRunning = false;
// insert thread into the thread vector based on arrival time
auto it = threadVector.begin();
while (it != threadVector.end() && it->thread->arriving_time <= thread->thread->arriving_time) {
it++;
}
threadVector.insert(it, 1, *thread);
// send message
//cout << "*** Thread #" << tid << " has been created. \n" << "\tArrival: " << arriving_time << "\tRemaining: " << remaining_time << "\tPriority: " << priority << endl << endl;
return;
}
bool MyScheduler::Dispatch()
{
// iterate through thread vector and schedule threads
while (!threadVector.empty() && threadVector[0].thread->arriving_time <= timer) {
threadVector[0].isScheduled = true;
push_to_ordered_list(&threadVector[0]);
threadVector.erase(threadVector.begin());
}
//Todo: Check and remove finished threads
//Todo: Check if all the threads are finished; if so, return false
switch (policy) {
case FCFS: { //First Come First Serve
// iterate through cpus
for (unsigned int i = 0; i < num_cpu; i++) {
// an empty CPU is found
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0) {
auto it = orderedVector.begin();
while (it != orderedVector.end() && (it->thread->remaining_time == 0 || it->isRunning)) {
// remove from ordered vector if thread has been processed
if (it->thread->remaining_time == 0) {
it = orderedVector.erase(it);
}
else { // move to next thread
it++;
}
}
// assign processor to next available thread in ordered vector;
if (it != orderedVector.end()) {
CPUs[i] = it->thread;
it->isRunning = true;
}
else {
CPUs[i] = NULL;
}
}
}
break;
}
case STRFwoP: { //Shortest Time Remaining First, without preemption
// iterate through cpus
for (unsigned int i = 0; i < num_cpu; i++) {
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0) {
auto it = orderedVector.begin();
while (it != orderedVector.end() && (it->thread->remaining_time == 0 || it->isRunning)) {
// remove from ordered vector if thread has been processed
if (it->thread->remaining_time == 0) {
it = orderedVector.erase(it);
}
else { // move to next thread
it++;
}
}
// assign processor to next available thread in ordered vector;
if (it != orderedVector.end()) {
CPUs[i] = it->thread;
it->isRunning = true;
}
else {
CPUs[i] = NULL;
}
}
}
break;
}
case STRFwP: { //Shortest Time Remaining First, with preemption
auto it = orderedVector.begin();
// assign first n threads in ordered vector to a cpu
for (unsigned int i = 0; i < num_cpu; i++) {
// get next ready thread
it = orderedVector.begin();
while (it != orderedVector.end() && (it->thread->remaining_time == 0 || it->isRunning)) {
// remove from ordered vector if thread has been processed
if (it->thread->remaining_time == 0) {
it = orderedVector.erase(it);
}
else { // move to next thread
it++;
}
}
// check if the CPU is able to be assigned to this thread
if (it != orderedVector.end()) {
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0 || CPUs[i]->remaining_time > it->thread->remaining_time) {
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0) {
CPUs[i] = it->thread;
it->isRunning = true;
//cout << " thread #" << it->thread->tid << " is assigned to CPU " << i << " in time " << timer << "\n";
}
else { // preemption
// find the thread status that contains CPUs[i] thread
auto iter = orderedVector.begin();
while (iter->thread != CPUs[i] && iter != orderedVector.end()) {
iter++;
}
if (it != orderedVector.end()){
iter->isRunning = false; // turn off the status of the evict thread
CPUs[i] = it->thread;
it->isRunning = true;
//cout << " thread #" << it->thread->tid << " is preempted with thread #" << iter->thread->tid << " in time " << timer << "\n";
}
}
}
}
}
break;
}
case PBS: { //Priority Based Scheduling, with preemption
auto it = orderedVector.begin();
// assign first n threads in ordered vector to a cpu
for (unsigned int i = 0; i < num_cpu; i++) {
// get next ready thread
it = orderedVector.begin();
while (it != orderedVector.end() && (it->thread->remaining_time == 0 || it->isRunning)) {
// remove from ordered vector if thread has been processed
if (it->thread->remaining_time == 0) {
it = orderedVector.erase(it);
}
else { // move to next thread
it++;
}
}
// check if the CPU is able to be assigned to this thread
if (it != orderedVector.end()) {
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0 || CPUs[i]->priority > it->thread->priority) {
if (CPUs[i] == NULL || CPUs[i]->remaining_time == 0) {
CPUs[i] = it->thread;
it->isRunning = true;
//cout << " thread #" << it->thread->tid << " is assigned to CPU " << i << " in time " << timer << "\n";
}
else { // preemption
// find the thread status that contains CPUs[i] thread
auto iter = orderedVector.begin();
while (iter->thread != CPUs[i] && iter != orderedVector.end()) {
iter++;
}
if (it != orderedVector.end()){
iter->isRunning = false; // turn off the status of the evict thread
CPUs[i] = it->thread;
it->isRunning = true;
//cout << " thread #" << it->thread->tid << " is preempted with thread #" << iter->thread->tid << " in time " << timer << "\n";
}
}
}
}
}
break;
}
default:
cout << "Invalid policy!";
throw 0;
}
return !(orderedVector.empty() && threadVector.empty());
}
// a pre-ordered thread vector that do estimation to re-arrange thread vector
void MyScheduler::push_to_ordered_list(ThreadsStatus *thread) {
// find a proper place in the vector to push the thread based on policyv
// vector head gets to process first, followed by the second item, etc.
auto it = orderedVector.begin();
switch (policy)
{
case FCFS: //First Come First Serve
while (it != orderedVector.end() && (it->thread->arriving_time <= thread->thread->arriving_time || it->isRunning)) {
it++;
}
orderedVector.insert(it, *thread);
break;
case STRFwoP: { //Shortest Time Remaining First, without preemption
// find first thread with greater remaining time in ordered vector that is not currently running
while (it != orderedVector.end() && (it->thread->remaining_time <= thread->thread->remaining_time || it->isRunning)) {
it++;
}
orderedVector.insert(it, *thread);
break;
}
case STRFwP: { //Shortest Time Remaining First, with preemption
// find first thread with greater remaining time in ordered vector that is not currently running
while (it != orderedVector.end()
&& it->thread->remaining_time + it->thread->arriving_time <= thread->thread->remaining_time + thread->thread->arriving_time) {
it++;
}
orderedVector.insert(it, *thread);
/* // check the vector order for debug
auto itera = orderedVector.begin();
cout << "\tthread order: ";
while (itera != orderedVector.end()) {
cout << itera->thread->tid << " ";
itera++;
}
cout << endl;
*/
break;
}
case PBS: { //Priority Based Scheduling, with preemption
// find first thread with lower priority
while (it != orderedVector.end() && it->thread->priority <= thread->thread->priority) {
it++;
}
// insert thread in front of lower priority thread
orderedVector.insert(it, 1, *thread);
break;
}
default:
cout << "Invalid policy!";
throw 0;
}
return;
}
void MyScheduler::clear_CPU() {
for (unsigned int i = 0; i < num_cpu; i++) { // clear out the array of CPU indicator
CPUs[i] = NULL;
}
}