-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.cpp
211 lines (185 loc) · 5.44 KB
/
CPU.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
/*
* CPU.cpp
*
* Created on: Aug 27, 2016
* Author: iryna
*/
#include "CPU.h"
CPU::CPU()
{
timeUnitsMap[VARIABLE] = 0;
timeUnitsMap[PRINT] = 0;
timeUnitsMap[LOCK] = 0;
timeUnitsMap[UNLOCK] = 0;
timeUnitsMap[END] = 0;
quantum = 0;
for(char var = 97; var != 123; ++var ) {
variables[var] = 0;
}
hasLock = false;
}
void CPU::InitializePrograms( std::queue<std::string> parsedInstructionsVector,
unsigned variableTU,
unsigned printTU,
unsigned lockTU,
unsigned unlockTU,
unsigned endTU,
unsigned _quantum)
{
timeUnitsMap[VARIABLE] = variableTU;
timeUnitsMap[PRINT] = printTU;
timeUnitsMap[LOCK] = lockTU;
timeUnitsMap[UNLOCK] = unlockTU;
timeUnitsMap[END] = endTU;
quantum = _quantum;
for(char var = 97; var != 123; ++var ) {
variables[var] = 0;
}
hasLock = false;
for(unsigned id = 1; id <= parsedInstructionsVector.size(); ++id) {
std::string instr;
std::queue<std::string> oneProgramInstructions;
unsigned numberOfStatementsForOneProgram = 0;
do{
numberOfStatementsForOneProgram++;
instr = parsedInstructionsVector.front();
boost::trim(instr);
parsedInstructionsVector.pop();
oneProgramInstructions.push(instr);
} while(instr != "end");
if(numberOfStatementsForOneProgram <= 25 && LockUnlockCheck(oneProgramInstructions)) {
Program *program = new Program(oneProgramInstructions, id);
readyQueue.push(program);
} else if (numberOfStatementsForOneProgram > 25) {
std::cout << "Program " << id << " was not added to queue " \
"because of consisting more then 25 statements."<<std::endl;
}
}
}
void CPU::Run(){
while(!readyQueue.empty()){
exec(readyQueue.front(), quantum);
}
}
int CPU::exec(Program * program, unsigned quant) {
std::string statement = program->GetNextStatement();
unsigned requiredTime = GetRequiredTime(statement);
unsigned executedTime = program->GetExecutedTime(); //time command was executed previous time
unsigned totalAvailableTime = quant + executedTime;
while (totalAvailableTime >= requiredTime && requiredTime != 0) {
if(std::regex_match(statement, std::regex("[a-z]\\s+=\\s+\\d+")))
{
statement = std::regex_replace(statement, std::regex("\\s+"), "");
std::vector< std::string > tokens;
boost::split(tokens, statement, boost::is_any_of("="));
unsigned variableValue = std::stoi(tokens[1]);
checkValueIsLess100(variableValue);
variables[*(tokens[0].c_str())] = variableValue;
} else if(std::regex_match(statement, std::regex("print\\s+[a-z]")))
{
statement = std::regex_replace(statement, std::regex("\\s+"), " ");
std::vector< std::string > tokens;
boost::split(tokens, statement, boost::is_any_of(" "));
unsigned programId = readyQueue.front()->GetProgramID();
std::cout << programId << ": "<< variables[*(tokens[1].c_str())] << std::endl;
} else if(statement == "lock")
{
if(hasLock) {
blockedQueue.push(readyQueue.front());
readyQueue.pop();
return 0;
} else {
hasLock = true;
}
} else if(statement == "unlock")
{
if (!blockedQueue.empty()) {
readyQueue.push(blockedQueue.front());
blockedQueue.pop();
}
hasLock = false;
} else if(statement == "end")
{
program->RemoveExecuted();
delete readyQueue.front();
readyQueue.pop();
return 0;
} else {
std::cout<<"Error: Undefined statement: "<<statement<< \
". Check if all instructions in input file match all rules." << std::endl;
}
program->RemoveExecuted();
totalAvailableTime -= requiredTime;
statement = program->GetNextStatement();
requiredTime = GetRequiredTime(statement);
}
program->SetExecutedTime(totalAvailableTime > 0 ? totalAvailableTime : 0);
// moving program to the end of the readyQueue if it was not mover already to blockedQueue
if (!readyQueue.empty()) {
readyQueue.push(readyQueue.front());
readyQueue.pop();
}
return 0;
}
unsigned CPU::GetRequiredTime(std::string statement)
{
if(std::regex_match(statement, std::regex("\\w\\s+=\\s+\\d+"))) {
return timeUnitsMap[VARIABLE];
} else if(std::regex_match(statement, std::regex("print\\s+[a-z]"))) {
return timeUnitsMap[PRINT];
} else if(statement == "lock") {
return timeUnitsMap[LOCK];
} else if(statement == "unlock") {
return timeUnitsMap[UNLOCK];
} else if(statement == "end") {
return timeUnitsMap[END];
} else {
std::cout<<"Error: Undefined statement;"<<statement<<std::endl;
return 0;
}
}
bool CPU::checkValueIsLess100(int value) {
if (value <= 100 && value >= 0) {
return true;
} else {
std::cout << "Found incorrect value of variable. Check input file." << std::endl;
return false;
}
}
bool CPU::LockUnlockCheck(std::queue<std::string> instructions) {
std::queue<std::string> instr(instructions);
bool isLock = false;
bool isUnlock = false;
if(!instr.empty()) {
std::string statement;
do {
statement = instr.front();
if (statement == "lock") {
if (isLock == true && isUnlock == false) {
std::cout<<"Error: Not allowed nested lock/unlock. Program removed from queue."<<std::endl;
return false;
}
isLock = true;
}
else if (statement == "unlock") {
isUnlock = true;
}
instr.pop();
} while (statement != "end");
}
if (isLock != isUnlock) {
std::cout<<"Error: Lock/unlock do not occur in pair. Program removed from queue."<<std::endl;
}
return isLock == isUnlock;
}
CPU::~CPU() {
while(!readyQueue.empty()) {
delete readyQueue.front();
readyQueue.pop();
}
while(!blockedQueue.empty()) {
delete blockedQueue.front();
blockedQueue.pop();
}
delete instance;
}