-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationDriver.cpp
More file actions
221 lines (198 loc) · 5.91 KB
/
Copy pathSimulationDriver.cpp
File metadata and controls
221 lines (198 loc) · 5.91 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
//Kyrylo Glamazdin
//CSCI 340
#include "SimulationDriver.hpp"
void runSimulation(){
long long RAM_SIZE = getInitialRAMData();
int NUM_OF_HARD_DISKS = getInitialHDData();
MemorySimulation MEMORY_SIMULATION(RAM_SIZE, NUM_OF_HARD_DISKS);
//continuously listen for user commands
listenForInput(MEMORY_SIMULATION);
}
long long getInitialRAMData(){
long long ram_size;
cout << "Enter the amount of RAM memory in bytes: ";
cin >> ram_size;
if (ram_size < 0){
cout << "The amount of memory cannot be negative." << endl;
abort();
}
else{
return ram_size;
}
}
int getInitialHDData(){
int hard_disk_num;
cout << "Enter the number of hard disks: ";
cin >> hard_disk_num;
if (hard_disk_num < 0){
cout << "The number of hard disks cannot be negative." << endl;
abort();
}
else{
return hard_disk_num;
}
}
void listenForInput(MemorySimulation& m){
while (true){
string next_command = "";
getline(cin, next_command); //receive a user command as a string
if (!trimInput(next_command)){
if (next_command.size() > 0){
//run a command decoder for single words
decodeSingleWord(next_command, m);
}
}
else{
//run a command decoder for commands with spaces in between
decodeDoubleWord(next_command, m);
}
}
}
bool trimInput(string& s){
int front_space_counter = 0;
while (front_space_counter < s.size() && (int)s[front_space_counter] == 32){
front_space_counter++;
}
if (front_space_counter > 0){
//remove all front spaces
s = s.substr(front_space_counter, s.size() - front_space_counter);
}
if (s.size() == 0){
return false; //return if command is completely empty
}
int back_space_counter = 0;
while ((int)s[s.size() - back_space_counter - 1] == 32) {
back_space_counter++;
}
//remove all back spaces
if (back_space_counter > 0){
s = s.substr(0, s.size() - back_space_counter);
}
int j = 0;
bool middle_space_found = false;
for (j = 0; j < s.size(); j++){
if ((int)s[j] == 32){
middle_space_found = true;
break;
}
}
//return if there's no space in the middle of a command
if (!middle_space_found){
return false;
}
//find all extra middle spaces and remove them
int initial_middle_space = j;
while ((int)s[j] == 32){
j++;
}
if (j - initial_middle_space == 1){
return true;
}
string first_half = s.substr(0, initial_middle_space);
string second_half = "";
while (j < s.size()){
second_half = second_half + s[j];
j++;
}
s = first_half + " " + second_half;
int final_space_counter = 0;
for (int k = 0; k < s.size(); k++){
if ((int)s[k] == 32){
final_space_counter++;
}
}
return true;
}
void decodeSingleWord(string w, MemorySimulation& m){
//end time slice for the current process
if (w == "Q"){
m.endTimeSlice();
return;
}
//terminate current process
else if (w == "t"){
m.terminateCurrentProcess();
return;
}
else {
cout << "Command " << w << " doesn't exist. Please enter a valid command." << endl;
}
}
void decodeDoubleWord(string w, MemorySimulation& m){
//counts the number of separate spaces in the command.
//in case a user enter a command with 2 or more separate middle spaces (ex. "A 1 000 000"), the command is ignored
int separate_space_counter = 0;
for (int i = 0; i < w.size(); i++){
if ((int)w[i] == 32){
separate_space_counter++;
}
}
if (separate_space_counter > 1){
cout << "Command " << w << " doesn't exist. Please enter a valid command." << endl;
return;
}
int space_index = 0;
string first_half = "";
string second_half = "";
for (int i = 0; i < w.size(); i++){
if ((int)w[i] == 32){
space_index = i;
break;
}
}
first_half = w.substr(0, space_index);
second_half = w.substr(space_index + 1, w.size() - 1 - space_index);
//create a regular process
if (first_half == "A"){
long long process_size = stoll(second_half);
if (process_size > 0){
bool success = m.addProcess(regular, process_size);
if (!success){
cout << "The process you're trying to add is too large" << endl;
}
}
else{
cout << "Process must have a positive size" << endl;
}
}
//create a real-time process
else if (first_half == "AR"){
long long process_size = stoll(second_half);
if (process_size > 0){
bool success = m.addProcess(real_time, process_size);
if (!success){
cout << "The process you're trying to add is too large" << endl;
}
}
else{
cout << "Process must have a positive size" << endl;
}
}
//request disk usage
else if (first_half == "d"){
int disk_num = stoi(second_half);
m.requestDiskUsage(disk_num);
}
//finish using the disk
else if (first_half == "D"){
int disk_num = stoi(second_half);
m.endDiskUsage(disk_num);
}
else if (first_half == "S"){
if (second_half == "r"){
m.displayQueueInfo();
}
else if (second_half == "i"){
m.displayDiskUsage();
}
else if (second_half == "m"){
m.displayStateOfMemory();
}
else{
cout << "Command " << w << " doesn't exist. Please enter a valid command." << endl;
}
}
else {
cout << "Command " << w << " doesn't exist. Please enter a valid command." << endl;
}
}