-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuscheduler.cpp
115 lines (104 loc) · 2.45 KB
/
cpuscheduler.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
#include "cpuscheduler.h"
CPUScheduler::CPUScheduler(QObject *parent) : QObject(parent)
{
}
void CPUScheduler::newProcess(PCB* newP)
{
if(readyQueue->size() < 6)
readyQueue->push(*newP);
else
jobQueue->push(*newP);
}
void CPUScheduler::newProcess(string PID,int time,int priority,string father)
{
PCB* newP = new PCB(PID,time,priority,father);
if(readyQueue->size() < 6)
readyQueue->push(*newP);
else
{
if(father == "")
jobQueue->push(*newP);
else
{
queue<PCB>* tempQ = new queue<PCB>;
tempQ->push(*newP);
while(!jobQueue->empty())
{
tempQ->push(jobQueue->front());
jobQueue->pop();
}
queue<PCB>* a = jobQueue;
jobQueue = tempQ;
delete a;
}
}
}
void CPUScheduler::exec1()
{
if(readyQueue->empty())
return;
PCB tempE = readyQueue->top();
if(tempE.suspend)
return;
cpu->exec1(tempE);
readyQueue->pop();
//其余进程优先度增加
int inc = 1;
int size = (int)readyQueue->size();
priority_queue<PCB>* tempQ = new priority_queue<PCB>;
for(int i = 0;i < size;i++)
{
PCB temp2 = readyQueue->top();
if(temp2.suspend)
{
tempQ->push(temp2);
readyQueue->pop();
continue;
}
if(temp2.priority + inc <= 140)
{
temp2.priority += inc;
temp2.priority_pre += inc;
}
else
{
temp2.priority = 140;
temp2.priority_pre = 140;
}
tempQ->push(temp2);
readyQueue->pop();
}
priority_queue<PCB>* a;
a = readyQueue;
readyQueue = tempQ;
delete a;
//进程未完成push回 已完成push历史队列
if(tempE.time > 0)
{
readyQueue->push(tempE);
}
else
{
//运行完毕队列
free(tempE.father);
historyQueue->push_back(tempE);
}
}
void CPUScheduler::free(string fatherPID)
{
priority_queue<PCB>* tempQ = new priority_queue<PCB>;
while(!readyQueue->empty())
{
PCB tempP = readyQueue->top();
if(tempP.PID == fatherPID)
{
tempP.running_children--;
qDebug()<<"father --";
}
tempQ->push(tempP);
readyQueue->pop();
}
priority_queue<PCB>* a = readyQueue;
readyQueue = tempQ;
delete a;
}