-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStructs.cpp
More file actions
462 lines (421 loc) · 11.6 KB
/
Copy pathStructs.cpp
File metadata and controls
462 lines (421 loc) · 11.6 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include<iostream>
#include<thread>
#include<chrono>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include "Structs.h"
#include <mutex>
using namespace std;
//互斥锁
mutex coutMutex1;
mutex coutMutex2;
mutex coutMutex3;
mutex coutMutex4;
//堆栈构造函数
Stack::Stack() {
this->top = StackSize - 1;
}
//压栈
bool Stack::push(Task *n) {
if (this->top >=0) {
this->base[this->top] = n;
this->top--;
return true;
}
else {
return false;
}
}
//弹栈
Task* Stack::pop() {
if (this->top<StackSize-1) {
this->top++;
return this->base[this->top];
}
else {
return nullptr;
}
}
//判断是否栈空
bool Stack::isVoid() {
if (this->top < StackSize-1 ) {
return false;
}
else {
return true;
}
}
//队列构造函数
Queue::Queue() {
this->head = this->tail = nullptr;
this->size = 0;
}
//入队
void Queue::Push(Task *n) {
if (this->head==nullptr) {
this->head = this->tail = n;
}
else{
this->tail->next = n;
n->pre = this->tail;
this->tail = n;
}
}
//出队优先级最高的一个Task
Task* Queue::PopUrgent(){
Task * q, * p;
q = this->head;
p = this->head;
for (;q->next!=this->tail &&q->next != nullptr;) {
if (p->priority < q->next->priority) {
p = q->next;
}
q = q->next;
}
if (p == this->head) {
return this->Pop();
}
else if (p == this->tail) {
Task * t = this->tail;
this->tail = this->tail->pre;
return t;
}
else {
return this->Pop(p);
}
}
//出队优先级最高的一个Task
Task* Queue::PopUrgent(Task * q) {
Task * p,*t;
if (q == nullptr) {
return nullptr;
}
p=t = q;
for (; p->next != this->tail&&q->next != nullptr;) {
if (t->priority < p->next->priority) {
t = p->next;
}
p= p->next;
}
if (t == q) {
return nullptr;
}
else {
return this->Pop(t);
}
}
//出队最短进程
Task* Queue::PopShort(){
Task * q, * p;
q = this->head;
p = this->head;
for (; q->next != this->tail&&q->next != nullptr;) {
if (p->request_t < q->next->request_t) {
p = q->next;
}
q = q->next;
}
if (p==this->head) {
return this->Pop();
}
else {
return this->Pop(p);
}
}
//中间出队
Task* Queue::Pop(Task * q) {
if (q==this->head) {
return this->Pop();
}
else if (q==this->tail) {
Task * t = this->tail;
this->tail = this->tail->pre;
return t;
}
else {
q->next->pre = q->pre;
q->pre->next = q->next;
}
return q;
}
//出队
Task* Queue::Pop() {
Task * q;
for (; this->head==nullptr;);
q = this->head;
this->head = this->head->next;
return q;
}
//判断是否队空
bool Queue::isVoid() {
if (this->head != nullptr) {
return false;
}
else {
return true;
}
}
//返回队尾
Task * Queue::getTail(){
return this->tail;
}
//处理机构造函数
Cpu::Cpu(){
this->q = Queue();//Task队列
this->s = Stack();//Task栈
this->record = Queue();//Task记录
this->taskNum = TaskNum;
}
//先来先服务算法
int Cpu::FCFS(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "************************" << endl;
cout<<"****先来先服务(FCFS)****"<<endl;
cout << "************************" << endl;
for (int i=0;i<TaskNum;i++) {
for (; this->q.isVoid(););
Task *t;
t = this->q.Pop();
t->response_t = GetTickCount();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "执行-进程-" << t->index << endl;
Sleep(t->request_t);
t->finish_t = GetTickCount();
t->wait_t=t->response_t - t->submit_t;
this->record.Push(t);
coutMutex2.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "完成-进程-" << t->index << endl;
coutMutex2.unlock();
}
return 0;
}
//短进程优先算法
int Cpu::SJF(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "************************" << endl;
cout << "****短作业优先(SJF)****" << endl;
cout << "************************" << endl;
for (int i = 0; i < TaskNum; i++) {
for (; this->q.isVoid(););
Task *t;
t = this->q.PopShort();
t->response_t = GetTickCount();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "执行-进程-" << t->index << endl;
Sleep(t->request_t);
t->finish_t = GetTickCount();
t->wait_t=t->response_t - t->submit_t;
this->record.Push(t);
coutMutex2.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "完成-进程-" << t->index << endl;
coutMutex2.unlock();
}
return 0;
}
//优先级调度算法(非抢占式)
int Cpu::Priority_non_Preemptive(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "**********************************" << endl;
cout << "****优先级调度算法(非抢占式)****" << endl;
cout << "**********************************" << endl;
for (int i = 0; i < TaskNum; i++) {
for (; this->q.isVoid(););
Task *t;
t = this->q.PopUrgent();
t->response_t = GetTickCount();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "执行-进程-" << t->index << endl;
Sleep(t->request_t);
t->finish_t = GetTickCount();
t->wait_t = t->response_t - t->submit_t;
this->record.Push(t);
coutMutex2.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "完成-进程-" << t->index << endl;
coutMutex2.unlock();
}
return 0;
}
//优先级调度算法(抢占式)
int Cpu::Priority_Preemptive() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "**********************************" << endl;
cout << "****优先级调度算法(抢占式)******" << endl;
cout << "**********************************" << endl;
for (; this->taskNum > 0;) {
for (; this->q.isVoid(););
Task *t;
t = this->q.PopUrgent();
t->response_t = GetTickCount();
this->Priority_Preemptive_Execult(t);
}
return 0;
}
//抢占式执行
int Cpu::Priority_Preemptive_Execult(Task * t){
Task * x;
bool flag = false;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "执行-进程-" << t->index << endl;
for (int j = 0; j < (t->request_t - t->run_t); j++) {
Sleep(1);
x = this->q.PopUrgent(t);//尝试出队优先级更高的Task
if (x!=nullptr) {
t->run_t += j;
flag = true;
this->Interrupt(t, x);//进入中断
}
}
if (!flag) {
t->finish_t = GetTickCount();
this->record.Push(t);
this->taskNum--;
coutMutex4.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "完成-进程-"<< t->index << endl;
coutMutex4.unlock();
}
return 0;
}
//中断处理
int Cpu::Interrupt(Task * tOld, Task * tNew) {
coutMutex3.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_BLUE);
cout << "中断-进程-" << tOld->index << " --->" << tNew->index << endl;
coutMutex3.unlock();
tOld->interrupt_t = GetTickCount();
this->s.push(tOld);//旧进程压栈
tNew->response_t = GetTickCount();
this->Priority_Preemptive_Execult(tNew);//新进程执行
this->Restore();//中断恢复
return 0;
}
//中断恢复
int Cpu::Restore() {
Task * t = this->s.pop();//旧进程弹栈
coutMutex2.lock();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | BACKGROUND_GREEN);
cout << "恢复-进程-" << t->index << endl;
coutMutex2.unlock();
t->wait_t += (GetTickCount() - t->interrupt_t);
this->Priority_Preemptive_Execult(t);//旧进程执行
return 0;
}
//算法评价
int Cpu::Evaluate() {
double avgWait_t = 0, avgTotal_t = 0, avgWeightTotal_t = 0, avgResponse_t = 0;
double maxWait_t = 0, maxTotal_t = 0, maxWeightTotal_t = 0, maxResponse_t = 0;
cout << endl <<"完成顺序:"<<endl;
for (int i = 0; i < TaskNum; i++) {
Task *t = this->record.Pop();
t->total_t = t->finish_t - t->submit_t;
t->response_t = t->response_t - t->submit_t;
t->weight_total_t = t->total_t / t->request_t;
avgTotal_t += t->total_t;
avgWeightTotal_t += t->weight_total_t;
avgWait_t += t->wait_t;
avgResponse_t += t->response_t;
if (t->wait_t > maxWait_t) {
maxWait_t = t->wait_t;
}
if (t->total_t > maxTotal_t) {
maxTotal_t = t->total_t;
}
if (t->weight_total_t > maxWeightTotal_t) {
maxWeightTotal_t = t->weight_total_t;
}
if (t->response_t > maxResponse_t) {
maxResponse_t = t->response_t;
}
cout<<t->index<<"-->";
}
avgTotal_t /= TaskNum;
avgWeightTotal_t /= TaskNum;
avgWait_t /= TaskNum;
avgResponse_t /= TaskNum;
cout << endl << "*************************************************" << endl;
cout << "处理的进程总数 = " << TaskNum << endl;
cout << "*************************************************" << endl;
cout << "此算法 平均等待时间 = " << avgWait_t << "ms"
<< " 平均周转时间 = " << avgTotal_t << "ms"
<< " 平均带权周转时间 = " << avgWeightTotal_t << "ms"
<< " 平均响应时间 = " << avgResponse_t << "ms"
<< endl;
cout << "*************************************************" << endl;
cout << "单个进程的最大等待时间 = " << maxWait_t << "ms"
<< " 最大周转时间 = " << maxTotal_t << "ms"
<< " 最大带权周转时间 = " << maxWeightTotal_t << "ms"
<< " 最大响应时间 = " << maxResponse_t << "ms"
<< endl;
cout << "*************************************************" << endl << endl << endl;
return 0;
}
//获取该对象的队列成员的指针
Queue * Cpu::getQuene(){
return &this->q;
}
//生成器构造函数
Generator::Generator(Queue *q, int taskNum){
this->existingTasks = 0;
this->q = q;
this->taskNum = taskNum;
this->genThread = nullptr;
}
//生成器线程执行的函数
void Generator::Generate(){
for (int i=0;i< this->taskNum;i++) {
Task *t;
t = (Task *)malloc(sizeof(Task));
t->index = i;//Task唯一识别序号
t->submit_t = GetTickCount();//提交时间
t->request_t = this->Rand(MinWait, MaxWait);//运行需时
t->run_t = 0;//已运行时间
t->start_t = 0;//开始时间
t->response_t = 0;//响应时间
t->wait_t = 0;//等待时间
t->finish_t = 0;//完成时间
t->total_t = 0;//周转时间
t->weight_total_t = 0;//带权周转时间
t->interrupt_t = 0;//进入中断时的时间
t->priority = this->Rand(MinPirorty, MaxPirorty);//优先级
t->pre = nullptr;//前一个Task的指针
t->next = nullptr;//后一个Task的指针
this->q->Push(t);//新的进程入队
this->existingTasks ++;
//coutMutex1.lock();
//unique_lock<std::mutex> lock(coutMutex1);
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
//unique_lock<std::mutex> lock(coutMutex1);
//cout<<"生成进程"<< i <<endl;
//coutMutex1.unlock();
this_thread::sleep_for(std::chrono::milliseconds(2));
//定时休眠
if (this->existingTasks%GenerateGap ==0) {
this_thread::sleep_for(std::chrono::milliseconds(GenerateSleep));
}
}
}
void Generator::Start(){
if (this->genThread==nullptr) {
//启动生成器线程
this->genThread = new thread(&Generator::Generate, this);
}
}
//终止生成
void Generator::Stop(){
this->genThread = nullptr;
}
//清空进程计数器
void Generator::Clear(){
this->Stop();
this->existingTasks = 0;
this->q = nullptr;
}
//随机数生成器,范围a-b
int Generator::Rand(int a, int b){
srand((int) time(0) + rand());
return (rand() % (b - a)) + a;
}