-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine.cpp
More file actions
93 lines (77 loc) · 2.64 KB
/
Copy pathmachine.cpp
File metadata and controls
93 lines (77 loc) · 2.64 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
/**
* William Ruman 2015 -- Truman State University
* Implementation of the machine class, which manages the ready queue and takes
* outputs from the provided scheduling algorithm
*/
#include "machine.hpp"
#include <list>
#include "process.hpp"
#include <vector>
#include <iostream>
#include "algorithm.hpp"
Machine::Machine() {
tick = 0;
}
Machine::Machine(std::vector<Process>& pList) {
tick = 0;
processList = pList;
}
unsigned int Machine::getTime() {
return tick;
}
// Given the time, place processes in the ready queue that have
// arrived at that time
void Machine::loadReadyQueueForTick(unsigned int tick) {
for (std::vector<Process>::iterator i = processList.begin(); i != processList.end(); ++i) {
if (i->getArrivalTime() == tick) {
readyQueue.push_back(&(*i));
}
}
}
// Print machine statistics, like avg waits and turnarounds
void Machine::printStats() {
double avgWaitTime;
double avgTurnaroundTime;
unsigned int waitSum = 0;
unsigned int turnSum = 0;
for (std::vector<Process>::iterator i = processList.begin(); i != processList.end(); ++i) {
turnSum += i->getTurnaroundTime();
waitSum += i->getWaitTime();
}
avgTurnaroundTime = double(turnSum) / processList.size();
avgWaitTime = double(waitSum) / processList.size();
std::cout << "Average waiting time = " << avgWaitTime << std::endl;
std::cout << "Average turnaround time = " << avgTurnaroundTime << std::endl << std::endl;
}
// Print scheduler info, process info, and call printstats
void Machine::printResults() {
std::cout << "Algorithm: *** " << schedulingAlgorithm->name << " ***" << std::endl;
std::cout << "Process Arrival Departure On CPU Turnaround Time Waiting Time" << std::endl;
for (std::vector<Process>::iterator i = processList.begin(); i != processList.end(); ++i) {
i->printRow();
}
std::cout << std::endl;
this->printStats();
}
// Check process list to determine if all processes have completed
bool Machine::allProcessesComplete() {
for (std::vector<Process>::iterator i = processList.begin(); i != processList.end(); ++i) {
if (!(i->isDone())) {
return false;
}
}
return true;
}
// Begin the machine's event loop, which exits when all processes are
// done and increments the clock tick
void Machine::start() {
while (!allProcessesComplete()) {
this->loadReadyQueueForTick(tick);
schedulingAlgorithm->scheduleNext(readyQueue, tick);
++tick;
}
this->printResults();
}
void Machine::setSchedulingAlgorithm(Algorithm* selected) {
schedulingAlgorithm = selected;
}