-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskManagement.cpp
375 lines (315 loc) · 10.8 KB
/
taskManagement.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include <string>
#include <random>
#include <vector>
#include "mem.h"
#include <taskManagement.h>
using namespace std;
#define TRUE 1
#define FALSE 0
void task_management(PCB_type (&mem)[100], vector<PCB_type> & runningQueue, vector<PCB_type> & readyQueue)
{
if (readyQueue.empty())
{
// cout << "No tasks in the ready Queue." << endl;
return;
}
else if (runningQueue.empty())
{
readyQueue[0].state = RUNNING;
runningQueue.push_back(readyQueue[0]);
readyQueue.erase(readyQueue.begin());
}
for (int i = 0; i < 100; i++)
{
if (runningQueue[0].id == mem[i].id)
{
mem[i].state = RUNNING;
}
}
}
int create_task(string userName, string appName, string & jsonmem, PCB_type (&mem)[100], vector<PCB_type> & readyQueue, vector<PCB_type> & runningQueue)
{
int i, j, random_num;
default_random_engine random;
random_num = random();
for (j = 0; j < 100; j++) {
//Debug
// cout << mem[j].id << endl;
while (mem[j].id == random_num)
{
random_num = random();
while (random_num > 2147483646) {
random_num = random();
}
}
}
for (i = 0; i < 100; i++)
if (mem[i].state == EMPTY)
break;
mem[i].id = random_num;
mem[i].state = READY;
mem[i].app_name = appName;
mem[i].user_name = userName;
readyQueue.push_back(mem[i]);
cout << "The task is created successfully, task id:" << mem[i].id << endl;
//print the task id
task_management(mem, runningQueue, readyQueue);
return mem[i].id;
}
void display(vector<string> user_name, string jsonmem, PCB_type (&mem)[100], vector<PCB_type> & runningQueue, vector<PCB_type> & blockQueue, vector<PCB_type> & readyQueue)
{
int i;
cout<<endl;
cout << "Number of tasks in memory: " << runningQueue.size() + readyQueue.size() + blockQueue.size() << endl;
for (i = 0; i < 100; i++)
{
if (mem[i].state == RUNNING)
{
cout << endl;
cout << "Task id =" << mem[i].id << " is running." << endl;
cout << "User name:" << mem[i].user_name << endl;
cout << "App name:" << mem[i].app_name << endl;
cout << "Memory Use:" << get_task_mem(mem[i].id, jsonmem) << "B" << endl;
}
if (mem[i].state == READY)
{
cout<<endl;
cout << "Task id =" << mem[i].id << " is ready." << endl;
cout << "User name:" << mem[i].user_name << endl;
cout << "App name:" << mem[i].app_name << endl;
cout << "Memory Use:" << get_task_mem(mem[i].id, jsonmem) << "B" << endl;
}
if (mem[i].state == BLOCK)
{
cout<<endl;
cout << "Task id =" << mem[i].id << " is blocked." << endl;
cout << "User name:" << mem[i].user_name << endl;
cout << "App name:" << mem[i].app_name << endl;
cout << "Memory Use:" << get_task_mem(mem[i].id, jsonmem) << "B" << endl;
}
}
cout << endl;
if (user_name.size() > 0) {
int used = 0;
int total = 0x7FF;
cout << "User Memory Usage" << endl;
for (i = 0; i < int(user_name.size()); i++)
{
cout << "User name:" << user_name[i] << endl;
cout << "Memory Usage:" << get_user_mem(user_name[i], jsonmem) << "B" << endl;
used += get_user_mem(user_name[i], jsonmem);
}
cout << endl;
cout << "Used/Total: " << used << "B/" << total << "B" << endl;
} else {
cout << "No user is using memory." << endl;
}
cout <<endl;
}
bool block(string userName, int id, PCB_type (&mem)[100], vector<PCB_type> & runningQueue, vector<PCB_type> & blockQueue, vector<PCB_type> & readyQueue)
{
int i;
if (runningQueue.empty() && readyQueue.empty() && blockQueue.empty())
{
cout << "No task in the memory." << endl;
return false;
}
else if (runningQueue.empty() && readyQueue.empty())
{
cout << "Every task is blocked." << endl;
return false;
}
bool find = 0; //stands for whether task exists in memory
for (i = 0; i < 100; i++)
{
if (id == mem[i].id)
{
find = 1;
if (mem[i].user_name != userName)
{
cout << "You can only block your task." << endl;
return false;
}
if (mem[i].state == EMPTY) //The task is already killed.
cout << "The task does not exist." << endl;
else if (mem[i].state == BLOCK) //The task has been already blocked.
cout << "The task has been already blocked." << endl;
else if (mem[i].state == RUNNING)
{
mem[i].state = BLOCK; //The task is blocked.
cout << "The task is blocked." << endl;
blockQueue.push_back(mem[i]);
vector<PCB_type>::iterator iter;
for (iter = runningQueue.begin(); iter != runningQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
runningQueue.erase(iter);
break;
}
}
}
else if (mem[i].state == READY)
{
mem[i].state = BLOCK; //The task is blocked.
cout << "The task is blocked." << endl;
blockQueue.push_back(mem[i]);
vector<PCB_type>::iterator iter;
for (iter = readyQueue.begin(); iter != readyQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
readyQueue.erase(iter);
break;
}
}
}
}
}
if (find == 0) //No such tasks in the memory
{
cout << "You entered a wrong id!" << endl;
}
task_management(mem, runningQueue, readyQueue);
return TRUE;
}
bool wake_up(string userName,
int id,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue, vector<PCB_type> & blockQueue, vector<PCB_type> & readyQueue)
{
int i;
if (runningQueue.empty() && readyQueue.empty() && blockQueue.empty())
{
cout << "No task in the memory." << endl;
return false;
}
else if (blockQueue.empty())
{
cout << "Every task is either running or ready, no task need to wake up." << endl;
return false;
}
bool find = 0; //stands for whether task exists in memory
for (i = 0; i < 100; i++)
{
if (id == mem[i].id)
{
find = 1;
if (mem[i].user_name != userName)
{
cout << "You can only wake up your task." << endl;
return false;
}
if (mem[i].state == EMPTY){
cout << "The task does not exist." << endl;
return false;
}
else if (mem[i].state != BLOCK){
cout << "The task is either running or ready. "
"No need to wake it up."
<< endl;
return false;
}
else //the task is blocked.
{
mem[i].state = READY; //The task is waken up.
cout << "The task is waken up to ready state." << endl;
readyQueue.push_back(mem[i]);
vector<PCB_type>::iterator iter;
for (iter = blockQueue.begin(); iter != blockQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
blockQueue.erase(iter);
break;
}
}
}
}
}
if (find == 0) //No such tasks in the memory
{
cout << "Task " << id << " is not found." << endl;
return false;
}
task_management(mem, runningQueue, readyQueue);
return true;
}
bool kill(string userName,
int id,
string & jsonmem,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue, vector<PCB_type> & blockQueue, vector<PCB_type> & readyQueue)
{
int i;
if (runningQueue.empty() && readyQueue.empty() && blockQueue.empty())
{
cout << "No task in the memory." << endl;
return false;
}
bool find = 0; //stands for whether task exists in memory
for (i = 0; i < 100; i++)
{
if (id == mem[i].id)
{
find = 1;
if (mem[i].user_name != userName)
{
cout << "You can only kill your task." << endl;
return false;
}
if (mem[i].state == EMPTY)
{ //The task is already killed.
cout << "The task does not exist." << endl;
return false;
}
else if (mem[i].state == RUNNING)
{
mem[i].state = EMPTY; //The task is killed.
cout << "The task is killed." << endl;
vector<PCB_type>::iterator iter;
for (iter = runningQueue.begin(); iter != runningQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
runningQueue.erase(iter);
break;
}
}
task_mem_free(id, mem[i].user_name, jsonmem);
}
else if (mem[i].state == BLOCK)
{
mem[i].state = EMPTY; //The task is killed.
cout << "The task is killed." << endl;
vector<PCB_type>::iterator iter;
for (iter = blockQueue.begin(); iter != blockQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
blockQueue.erase(iter);
break;
}
}
task_mem_free(id, mem[i].user_name, jsonmem);
}
else if (mem[i].state == READY)
{
mem[i].state = EMPTY; //The task is killed.
cout << "The task is killed." << endl;
vector<PCB_type>::iterator iter;
for (iter = readyQueue.begin(); iter != readyQueue.end(); iter++)
{
if (iter->id == mem[i].id) {
readyQueue.erase(iter);
break;
}
}
task_mem_free(id, mem[i].user_name, jsonmem);
}
break;
}
}
if (find == 0) //No such tasks in the memory
{
cout << "You entered a wrong id!" << endl;
}
task_management(mem, runningQueue, readyQueue);
return TRUE;
}