-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCR&FIT.cpp
More file actions
709 lines (663 loc) · 19 KB
/
PCR&FIT.cpp
File metadata and controls
709 lines (663 loc) · 19 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//
// main.cpp
// PCR
//
// Created by Ryan on 2017/12/27.
// Copyright © 2017年 Ryan. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <unistd.h>
#include <vector>
#include <list>
#define TRUE 1
#define FALSE 0
#define OK 1
#define Datatype int
#define MaxLength 255
using namespace std;
static int realtime = 0;
static int timeslice_origin = 2;
static int timeslice = timeslice_origin;
static int OccupySize;
//定义进程节点
typedef struct node
{
int ID;
int prio;
int round;
int cputime;
int needtime;
int finishtime;
int count;
int state;
int memory_start;
int memory_length;
struct node *next;
}PCB;
static vector<int> MEMORY;
static vector<int> StartPoint;
static vector<int> Length;
static vector<int> Tag;
//定义事件节点
typedef struct QueueNode
{
Datatype data;
PCB *process;
struct QueueNode *next;
}QueueNode;
typedef list<QueueNode> LISTQ;
//定义头节点
typedef struct{
QueueNode *front;
QueueNode *rear;
int count;
}LinkQueue;
//初始化链队列,头节点置空
static void InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = NULL;
}
//判断链队列是否为空
static int QueueEmpty(LinkQueue *Q)
{
return (Q->front == NULL && Q->rear == NULL);
}
// 事件入队
static void EnLinkQueue(LinkQueue *Q, QueueNode *p)
{
// QueueNode *p;
// p = (QueueNode *)malloc(sizeof(QueueNode));//为新的节点分配空间
// p->data = v;
p->next = NULL;
if(QueueEmpty(Q))
Q->front = Q->rear = p;
else
{
Q->rear->next = p; //将新的节点连接到队列
Q->rear = p; //指向队列尾
}
Q->count++;
}
//交换队列
static void ExLinkQueue(LinkQueue *Q, LinkQueue *P)
{
QueueNode *p = P->front;
if (QueueEmpty(Q))
{
Q->front = Q->rear = p;
}
else
{
Q->rear->next = p;
Q->rear = p;
}
if(P->front == P->rear) //判断队列是否只有一个节点
P->front = P->rear = NULL;
else
P->front = p->next;
p->next = NULL;
Q->count++;
P->count--;
//free(p);
}
//出队
static Datatype DeLinkQueue(LinkQueue *Q)
{
Datatype i;
QueueNode *s;
if(QueueEmpty(Q)) //判断队列是否为空
printf("Error,the linkqueue is empty!");
s = Q->front;
i = s->data;
if(Q->front == Q->rear) //判断队列是否只有一个节点
Q->front = Q->rear = NULL;
else
Q->front = s->next;
//free(s);
Q->count--;
return i;
}
//读取队列头元素,不改变队列状态
static Datatype ReadLinkQueue(LinkQueue *Q)
{
Datatype i = 0;
QueueNode *p = Q->front;
if(QueueEmpty(Q)) //判断队列是否为空
cout<<"-> NULL";
while (p != NULL)
{
i = p->data;
cout<<"-> "<<p->process->ID;
p = p->next;
}
cout<<endl;
return i;
}
static QueueNode *QueueHead = (QueueNode *)malloc(sizeof(node));
static PCB *head = (node *)malloc(sizeof(node)); //声明头程序空间
static LinkQueue Ready;
static LinkQueue Running;
static LinkQueue Finished;
static LinkQueue Event;
static LISTQ MemoryList;
// int n;
static void EventCreate()
{
//std::ifstream fin("/Users/Ryan/Documents/C++ Project/data.txt", std::ios::in);
QueueHead->next = NULL;
QueueNode *q = QueueHead;
std::ifstream fin1("/Users/Ryan/Documents/C++ Project/PCR.txt",std::ios::in);
char line1[1024] = {0};
int x = 0;
int y = 0;
int z = 0;
int w = 0;
while(fin1.getline(line1,sizeof(line1)))
{
QueueNode *p = nullptr;
p = (QueueNode *)malloc(sizeof(QueueNode));
node *pro = (node *)malloc(sizeof(node));
p->process = pro;
std::stringstream word(line1);
word>>x;
word>>y;
word>>z;
word>>w;
p->process->ID = x;
p->data = y;
p->process->cputime = z;
p->process->memory_length = w;
p->process->needtime = p->process->cputime;
p->process->state = 0;
p->next = NULL;
q->next = p;
q = q->next;
EnLinkQueue(&Event, p);
}
fin1.clear();
fin1.close();
}
//获得空闲表
static void GetFreeChart()
{
StartPoint.clear();
Length.clear();
Tag.clear();
for (int i = 0; i < MEMORY.size(); i++) {
if (MEMORY[i] == 0) {
int count = 1;
while (MEMORY[i+count] == 0) {
count++;
}
StartPoint.push_back(i);
Length.push_back(count);
Tag.push_back(0);
i = i + count - 1;
}
}
}
//内存剩余空间
static int MemoryLeft()
{
int Space = 0;
for (int i = 0; i < MEMORY.size(); i++) {
if (MEMORY[i] == 0) {
Space++;
}
else
Space = Space;
}
return Space;
}
//初始化内存
static void InitMemory(int MemorySize, int OccupySize)
{
MEMORY.resize(MemorySize);
for (int i = 0; i < OccupySize; i++) {
MEMORY[i] = 1;
}
for (int j = OccupySize; j < MemorySize; j++) {
MEMORY[j] = 0;
}
}
//程序进入内存
static void EnMemory(int i, QueueNode *qnode)
{
qnode->process->memory_start = StartPoint[i];
Tag[i] = 1;
for (int j = StartPoint[i]; j < StartPoint[i] + qnode->process->memory_length; j++){
MEMORY[j] = 1;
}
MemoryList.push_back(*qnode);
}
//程序弹出内存
static void DeMemory(QueueNode *qnode)
{
LISTQ::iterator p = MemoryList.begin();
for (int i = qnode->process->memory_start; i < qnode->process->memory_start + qnode->process->memory_length; i++) {
MEMORY[i] = 0;
}
for (p = MemoryList.begin(); p != MemoryList.end(); p++) {
if (p->process == qnode->process) {
MemoryList.erase(p);
cout<<"Process Pop Out of Memory."<<endl;
break;
}
}
}
//压缩内存
static void CompressMemory()
{
if (MemoryList.size() == 0) {
cout<<"No Progress in the Memory."<<endl;
}
else{
QueueNode q = MemoryList.front();
QueueNode *p = &q;
int start = p->process->memory_start;
int length = p->process->memory_length;
if (q.process->memory_start != OccupySize) {
for (int k = p->process->memory_start; k < p->process->memory_start + p->process->memory_length; k++) {
MEMORY[k] = 0;
}
p->process->memory_start = OccupySize;
start = p->process->memory_start;
for (int i = OccupySize; i < OccupySize + p->process->memory_length; i++) {
MEMORY[i] = 1;
}
p = p->next;
}
else
p = p->next;
for (int j = 0; j < MemoryList.size() - 1; j++) {
for (int k = p->process->memory_start; k < p->process->memory_start + p->process->memory_length; k++) {
MEMORY[k] = 0;
}
p->process->memory_start = start + length;
for (int i = start + length; i < start + length + p->process->memory_length; i++) {
MEMORY[i] = 1;
}
length = p->process->memory_length;
start = p->process->memory_start;
p = p->next;
}
cout<<"Compress Complete."<<endl;
}
}
//先来先服务
static void FCFS()
{
FCFS:
if (QueueEmpty(&Running) && !QueueEmpty(&Ready))
{
ExLinkQueue(&Running, &Ready);
//Running.front->process->needtime--;
Running.rear->process->state = 2;
if (Running.rear->process->needtime == 0)
{
EnLinkQueue(&Finished, Running.rear);
Finished.rear->process->state = 3;
Finished.rear->process->finishtime = realtime;
DeMemory(Finished.rear);
DeLinkQueue(&Running);
goto FCFS;
}
}
else if (!QueueEmpty(&Running))
{
Running.rear->process->needtime--;
if (Running.rear->process->needtime == 0)
{
EnLinkQueue(&Finished, Running.front);
Finished.rear->process->state = 3;
Finished.rear->process->finishtime = realtime;
DeMemory(Finished.rear);
DeLinkQueue(&Running);
goto FCFS;
}
}
else
return;
}
//时间片轮转
static void RR()
{
RR:
//添加进程
if (QueueEmpty(&Running) && !QueueEmpty(&Ready))
{
ExLinkQueue(&Running, &Ready);
Running.rear->process->state = 2;
//瞬间完成
if (Running.rear->process->needtime == 0)
{
EnLinkQueue(&Finished, Running.rear);
Finished.rear->process->state = 3;
Finished.rear->process->finishtime = realtime;
DeMemory(Finished.rear);
DeLinkQueue(&Running);
goto RR;
}
}
//运行
else if (!QueueEmpty(&Running))
{
Running.front->process->needtime--;
timeslice--;
cout<<"using timeslice: "<<timeslice<<endl;
//完成
if (Running.front->process->needtime == 0)
{
EnLinkQueue(&Finished, Running.front);
Finished.rear->process->state = 3;
Finished.rear->process->finishtime = realtime;
DeMemory(Finished.rear);
DeLinkQueue(&Running);
timeslice = timeslice_origin;
//添加新的进程
goto RR;
}
//时间片耗尽
if (!QueueEmpty(&Running) && timeslice == 0)
{
ExLinkQueue(&Ready, &Running);
Ready.rear->process->state = 1;
timeslice = timeslice_origin;
goto RR;
}
}
else
return;
}
//最先适应分配算法
static int FirstFit(QueueNode *p)
{
ProcessADD:
GetFreeChart();
int count = 0;
// LISTINT::iterator i;
//准备队列和运行队列被调度进了程序
if (p != NULL) {
for (int i = 0; i < Length.size(); i++) {
//当空间足够而且没有被占用时,直接放入
if (Length[i] >= p->process->memory_length && Tag[i] == 0) {
EnMemory(i, p);
//ExLinkQueue(&Ready, &Event);
//p = p->next;
return 1;
}
else
count++;
}
//没有足够的空间
if (count >= Length.size()) {
//总空间不够
if (MemoryLeft() < p->process->memory_length) {
cout<<"Out of Memory Space. PID: "<<p->process->ID<<endl;
// DeLinkQueue(&Event);
// EnLinkQueue(&Event, p);
//p = p->next;
return 0;
}
//总空间够,但是有碎片
else{
cout<<"Memory Left: "<<MemoryLeft()<<endl;
cout<<"Need Memory: "<<p->process->memory_length<<endl;
cout<<"Enough Memory Space, But Can't Push in PID: "<<p->process->ID<<endl;
CompressMemory(); //压缩空间
goto ProcessADD;
}
}
}
return 0;
}
//最优适应分配方法
static int BestFit(QueueNode *p)
{
int temp = MEMORY.size();
int temp_i = Length.size();
Bestfit:
GetFreeChart();
int count = 0;
if (p != NULL) {
for (int i = 0; i < Length.size(); i++) {
//当空间足够而且没有被占用时,直接放入
if (Length[i] >= p->process->memory_length && Tag[i] == 0) {
if (Length[i] - p->process->memory_length < temp) {
temp = Length[i] - p->process->memory_length;
temp_i = i;
}
//ExLinkQueue(&Ready, &Event);
//p = p->next;
//return 1;
}
else
count++;
}
//没有足够的空间
if (count >= Length.size()) {
//总空间不够
if (MemoryLeft() < p->process->memory_length) {
cout<<"Out of Memory Space. PID: "<<p->process->ID<<endl;
// DeLinkQueue(&Event);
// EnLinkQueue(&Event, p);
//p = p->next;
return 0;
}
//总空间够,但是有碎片
else{
cout<<"Memory Left: "<<MemoryLeft()<<endl;
cout<<"Need Memory: "<<p->process->memory_length<<endl;
cout<<"Enough Memory Space, But Can't Push in PID: "<<p->process->ID<<endl;
CompressMemory(); //压缩空间
goto Bestfit;
}
}
else{
EnMemory(temp_i, p);
return 1;
}
}
return 0;
}
//最差适应分配方法
static int WorstFit(QueueNode *p)
{
int temp = 0;
int temp_i = 0;
Worstfit:
GetFreeChart();
int count = 0;
if (p != NULL) {
for (int i = 0; i < Length.size(); i++) {
//当空间足够而且没有被占用时,直接放入
if (Length[i] >= p->process->memory_length && Tag[i] == 0) {
if (Length[i] - p->process->memory_length > temp) {
temp = Length[i] - p->process->memory_length;
temp_i = i;
}
//ExLinkQueue(&Ready, &Event);
//p = p->next;
//return 1;
}
else
count++;
}
//没有足够的空间
if (count >= Length.size()) {
//总空间不够
if (MemoryLeft() < p->process->memory_length) {
cout<<"Out of Memory Space. PID: "<<p->process->ID<<endl;
// DeLinkQueue(&Event);
// EnLinkQueue(&Event, p);
//p = p->next;
return 0;
}
//总空间够,但是有碎片
else{
cout<<"Memory Left: "<<MemoryLeft()<<endl;
cout<<"Need Memory: "<<p->process->memory_length<<endl;
cout<<"Enough Memory Space, But Can't Push in PID: "<<p->process->ID<<endl;
CompressMemory(); //压缩空间
goto Worstfit;
}
}
else{
EnMemory(temp_i, p);
return 1;
}
}
return 0;
}
int main()
{
// cout<<"请输入系统进程数: ";
// cin>>n;
// int m=n;
// int timeslice = 2;
// node *p = head->next;
// if(n==0)
// cout<<"此时没有就绪进程"<<endl;
// else
// {
// create(n);
// cout<<endl;
// cout<<"先来先服务调度"<<endl;
// FCFS();
// cout<<endl;
// cout<<"时间片轮转"<<endl;
// RR(timeslice);
// }
int MemorySize = 64;
OccupySize = 12;
InitMemory(MemorySize, OccupySize);
GetFreeChart();
string METHOD;
cout<<"Input Method: ";
cin>>METHOD;
InitQueue(&Event);
InitQueue(&Ready);
InitQueue(&Finished);
InitQueue(&Running);
EventCreate();
QueueNode *p = Event.front;
ADD:
while(!QueueEmpty(&Event) || !QueueEmpty(&Ready) || !QueueEmpty(&Running))
{
p = Event.front;
for (int i = 0; i < Event.count; i++){
if (p != NULL && realtime >= p->data)
{
int Mem = FirstFit(p);
if (Mem == 1) {
p = p->next;
ExLinkQueue(&Ready, &Event);
Ready.rear->process->state = 1;
}
else
p = p->next;
}
else if(p != NULL && p->next != NULL)
p = p->next;
}
if (!QueueEmpty(&Ready) || !QueueEmpty(&Running))
{
if (METHOD == "FCFS")
{
FCFSAG:
FCFS();
p = Event.front;
for (int i = 0; i < Event.count; i++){
if (p != NULL && realtime >= p->data)
{
int Mem = WorstFit(p);
if (Mem == 1) {
p = p->next;
ExLinkQueue(&Ready, &Event);
Ready.rear->process->state = 1;
}
else
p = p->next;
}
else if(p != NULL && p->next != NULL)
p = p->next;
}
p = Event.front;
for (int i = 0; i < Event.count; i++){
if (p != NULL && realtime >= p->data && MemoryLeft() >= p->process->memory_length) {
goto FCFSAG;
}
else
p = p->next;
}
if (QueueEmpty(&Running) && !QueueEmpty(&Ready)) {
goto RRAG;
}
cout<<"time: "<<realtime<<endl;
cout<<"Running ";
ReadLinkQueue(&Running);
cout<<"Ready ";
ReadLinkQueue(&Ready);
cout<<"Not Created ";
ReadLinkQueue(&Event);
cout<<"Finished ";
ReadLinkQueue(&Finished);
realtime++;
goto ADD;
}
else if (METHOD == "RR")
{
RRAG:
RR();
p = Event.front;
for (int i = 0; i < Event.count; i++){
if (p != NULL && realtime >= p->data)
{
int Mem = FirstFit(p);
if (Mem == 1) {
p = p->next;
ExLinkQueue(&Ready, &Event);
Ready.rear->process->state = 1;
}
else
p = p->next;
}
else if(p != NULL && p->next != NULL)
p = p->next;
}
p = Event.front;
for (int i = 0; i < Event.count; i++){
if (p != NULL && realtime >= p->data && MemoryLeft() >= p->process->memory_length) {
goto FCFSAG;
}
else
p = p->next;
}
if (QueueEmpty(&Running) && !QueueEmpty(&Ready)) {
goto RRAG;
}
cout<<"time: "<<realtime<<endl;
cout<<"Running ";
ReadLinkQueue(&Running);
cout<<"Ready ";
ReadLinkQueue(&Ready);
cout<<"Not Created ";
ReadLinkQueue(&Event);
cout<<"Finished ";
ReadLinkQueue(&Finished);
realtime++;
goto ADD;
}
else
cout<<"Wrong Method"<<endl;
}
}
//getchar();
return 0;
}