-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cc~
More file actions
397 lines (322 loc) · 10.3 KB
/
simulator.cc~
File metadata and controls
397 lines (322 loc) · 10.3 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <fstream>
#include <sstream>
int INIT_TIME = 4;
int FIN_TIME = 100000;
int ARRIVE_MIN = 5;
int ARRIVE_MAX = 30;
int QUIT_PROB = 5;
int CPU_MIN = 1;
int CPU_MAX = 3;
int DISK1_MIN = 2;
int DISK1_MAX = 6;
int DISK2_MIN = 3;
int DISK2_MAX = 7;
int totalItemAddedInDiskQ1 = 0;
int totalItemAddedInDiskQ2 = 0;
using namespace std;
std::vector<std::string> vec;
void readFile(){
string output;
std::ifstream myfile;
myfile.open("configuration.txt");
if (myfile.is_open()){
while (getline(myfile, output)){
vec.push_back(output);
}
} else {
std::cout << "File not found" << std::endl;
}
cout << vec[0] << " " << vec[1] << endl;
myfile.close();
return;
}
class Event{
public:
Event(int time, int jobSequenceNumber, int eventType){
this->time = time;
this->jobSequenceNumber = jobSequenceNumber;
this->eventType = eventType;
}
int time;
int jobSequenceNumber;
int eventType;
};
class less_than_key{
inline bool operator() (const Event& class1, const Event& class2)
{
return (class1.time < class2.time);
}
};
class Queue{
private:
typedef struct node{
Event *data;
node *next;
} *Node;
Node head;
Node current;
Node temporary;
Node tail;
public:
int size = 0;
Queue(){
head = NULL;
current = NULL;
temporary = NULL;
tail = NULL;
}
//pushing the Event in Queue
void push(Event *element){
Node newNode = new node;
newNode->next = NULL;
newNode->data = element;
if(head == NULL){
head = newNode;
tail = head;
size++;
} else {
current = head;
while(current->next != NULL){
current = current->next;
}
current->next = newNode;
tail = current->next;
size++;
}
}
Event pop(){
Event *toBeReturned;
if (head != NULL) {
toBeReturned = head->data;
if (size >0) {
size--;
}
if(head->next != NULL || head != NULL){
head = head->next;
}
return *toBeReturned;
} else {
toBeReturned = NULL;
return *toBeReturned;
}
}
bool isempty(){
if(head == NULL){
return true;
} else return false;
}
bool isfull(){
if (head != NULL) {
return true;
} else return false;
}
void printQ(){
cout << "The printed Que is ";
if (head != NULL) {
Node current = head;
cout << current->data->time << " ";
while(current->next != NULL){
current = current->next;
cout << current->data->time << " ";
}
} else {
cout << "it's empty"<< endl;
}
cout << endl;
}
Event begin(){
return *head->data;
}
Event end(){
return *tail->data;
}
};
class Component{
private:
int eventType;
int jobSequenceNumber = 0;
bool idleState = true;
int cpuClock = 0;
int processTime = 1;
int minTime;
int maxTime;
string name;
Event *processEvent = new Event(0,0,0);
public:
int totalOperation = 0;
int totalTimeComponentIsBusy = 0;
Component(int minTime, int maxTime, int eventType, string name){
this->minTime = minTime;
this->maxTime = maxTime;
this->eventType = eventType;
this->name = name;
};
void task(vector<Event> *cQueue, int tick, vector<Event> *priorityQ){
bool pDebug = false; //Set it to true for debugging purposes
if (idleState) { //if idle is true
if(pDebug) cout << name << " is in idle mode " << endl;
if (cQueue->empty()) { //if the cQueue is empty
idleState = true;
if(pDebug)cout << "que is empty" << endl;
return;
} else { //if cQueue is not empty
idleState = false; //set the process to busy
Event processEvent = cQueue->front();
cQueue->erase(cQueue->begin());
jobSequenceNumber = processEvent.jobSequenceNumber;
//cout << "Process event time " << processEvent.time << endl;
processTime = rand()%maxTime-minTime + maxTime;
if(pDebug)cout << name <<" just took a job: " << jobSequenceNumber << " job time: " << processTime << " cpu clock: " << cpuClock<< endl;
//printLog(eventToBeLoaded);
}
} else { // if cpu is busy
totalTimeComponentIsBusy++;
if (cpuClock == processTime) {
totalOperation++;
int probability = rand()%10+1;
Event *eventToBeLoaded = (Event*)malloc(sizeof(Event));
eventToBeLoaded->eventType = eventType;
eventToBeLoaded->time = tick+1;
eventToBeLoaded->jobSequenceNumber = jobSequenceNumber;
priorityQ->push_back(*eventToBeLoaded);
cpuClock = 0;
idleState = true;
} else {
cpuClock++;
if(pDebug) cout << name << " is still processing job " << jobSequenceNumber << " waiting for time " << processTime << " cpu time " << cpuClock << endl;
}
}
std::sort(priorityQ->begin(),priorityQ->end(),[ ]( const Event& lhs, const Event& rhs){
return lhs.time < rhs.time;
});
}
};
void printLog(Event element){
int time = element.time;
int jobSequenceNumber = element.jobSequenceNumber;
int eventType = element.eventType;
switch (eventType) {
case 1: cout << "At time " << time << " Job " << jobSequenceNumber << " arrives" << endl;
break;
case 2: cout << "At time " << time << " Job " << jobSequenceNumber << " finishes at CPU" << endl;
break;
case 3: cout << "At time " << time << " Job " << jobSequenceNumber << " arrives at Disk" << endl;
break;
case 4: cout << "At time " << time << " Job " << jobSequenceNumber << " finishes IO at Disk 1" << endl;
break;
case 5: cout << "At time " << time << " Job " << jobSequenceNumber << " finishes IO at Disk 2" << endl;
break;
case 6: cout << "At time " << time << " Job " << jobSequenceNumber << " exits" << endl;
break;
}
}
void findExitProbability(Event *event, vector<Event> *priorityQ, int tick){
int probability = rand()%10+1;
event->time = tick+1;
if (probability%QUIT_PROB) { //calculating the probablity for the job to exit
//Event eventToBeLoaded = Event(processTime,jobSequenceNumber,6);
event->eventType = 6;
} else { //load the job to the diskQ
event->eventType = 3;
}
priorityQ->push_back(*event);
//printLog(*event);
}
void sendToDisk(Event *event, vector<Event> *diskQ1, vector<Event> *diskQ2){
//cout << "sending to disk" << endl;
if(diskQ1->size()<diskQ2->size()){
diskQ1->push_back(*event);
totalItemAddedInDiskQ1++;
} else {
diskQ2->push_back(*event);
totalItemAddedInDiskQ2++;
}
}
int main(){
//readFile();
srand(time(NULL));
vector<Event> priorityQ;
vector<Event> cpuq;
vector<Event> diskQ1;
vector<Event> diskQ2;
Component *cpu = new Component(CPU_MIN, CPU_MAX, 2, "CPU");
Component *disk1 = new Component(DISK1_MIN,DISK1_MAX, 4, "disk1");
Component *disk2 = new Component(DISK2_MIN, DISK2_MAX, 5, "disk2");
//Creating first
Event *event1 = new Event(1,1,1);
Event *event2 = new Event(2,2,1);
Event *event3 = new Event(3,3,1);
//loading events to priorityQ
priorityQ.push_back(*event1);
priorityQ.push_back(*event2);
priorityQ.push_back(*event3);
//Event *event = new Event(0, 0, 0);
int tick = INIT_TIME; //simulation clock
while(!priorityQ.empty() && tick < FIN_TIME){
//measuring the job creating time randomly
int jobCreationTime = rand()%ARRIVE_MAX-ARRIVE_MIN + ARRIVE_MIN + tick;
//Creating a new event and then pushing it to the queue
Event *newEvent = (Event*)malloc(sizeof(Event));
newEvent->eventType = 1;
newEvent->time = jobCreationTime;
newEvent->jobSequenceNumber = tick+1;
priorityQ.push_back(*newEvent);
std::sort(priorityQ.begin(),priorityQ.end(),[ ]( const Event& lhs, const Event& rhs){
return lhs.time < rhs.time;
});
//cout << "diskQ1 size: "<<diskQ1.size() << endl;
//cout << "diskQ2 size: "<<diskQ2.size() << endl;
Event *event = &priorityQ.front();
priorityQ.erase(priorityQ.begin());
printLog(*event);
switch (event->eventType){
case 1: // code to be executed if n = 1;
cpuq.push_back(*event);
break;
case 2: // code to be executed if n = 2;
findExitProbability(event, &priorityQ, tick);
break;
case 3: // code to be executed if n = 3;
sendToDisk(event, &diskQ1, &diskQ2);
break;
case 4: // code to be executed if n = 4;
event->eventType = 1;
//priorityQ.push_back(*event); //sending the job back to the queue
break;
case 5: // code to be executed if n = 5;
//priorityQ.push_back(*event); // sending the job back to the queue
break;
case 6: // code to be executed if n = 6;
break;
}
//starting the task for CPU & the Disks
cpu->task(&cpuq, tick, &priorityQ);
disk1->task(&diskQ1, tick, &priorityQ);
disk2->task(&diskQ2, tick, &priorityQ);
tick++; //incrementing the time of the clock
}
cout << "============= Stats ===============" << endl;
cout << "Total time ran: " << FIN_TIME << endl;
cout << "========== Queue Stats ============" << endl;
cout << "Incomplete tasks in cpuQ: " << cpuq.size() << endl;
cout << "Total Item added in Disk 1: " << totalItemAddedInDiskQ1 << endl;
cout << "Incomplete tasks in diskQ1: " << diskQ1.size() << endl;
cout << "Total Item added in Disk 2: " << totalItemAddedInDiskQ2 << endl;
cout << "Incomplete tasks in diskQ2: " << diskQ2.size() << endl;
cout << "Incomplete tasks in priorityQ: " << priorityQ.size() << endl;
cout << "======== Component Stats ==========" << endl;
cout << endl;
cout << "Total operations completed by the CPU: " << cpu->totalOperation << endl;
cout << "Total operations completed by the Disk1: " << disk1->totalOperation << endl;
cout << "Total operations completed by the Disk2: " << disk2->totalOperation << endl;
cout << "========== Disk2 Stats ============" << endl;
cout << endl;
cout << "Total time component Cpu is busy doing task: " << cpu->totalTimeComponentIsBusy << endl;
cout << "Total time component disk1 is busy doing task: " << disk2->totalTimeComponentIsBusy << endl;
cout << "Total time component disk2 is busy doing task: " << disk2->totalTimeComponentIsBusy << endl;
cout << "===================================" << endl;
}