Repository navigation
Expand file tree
/
Copy pathProcess.cpp
More file actions
90 lines (61 loc) · 1.73 KB
/
Copy pathProcess.cpp
File metadata and controls
90 lines (61 loc) · 1.73 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
#include "Process.h"
#include "ProcessState.h"
int Process::processCounter = 0;
void Process::completeBurst() {
// updates the prediction factor using the exponential prediction
timePrediction = alpha * burstTimes.front() + (1 - alpha) * timePrediction;
// removes the burst form the queue
burstTimes.pop();
if (isCompleted())
changeState(new ProcessTerminatedState(this));
else
changeState(new ProcessWaitingIOState(this));
}
void Process::receiveIO() {
// removes the I/O form the queue
IOTimes.pop();
if (isCompleted())
changeState(new ProcessTerminatedState(this));
else
changeState(new ProcessReadyState(this));
}
void Process::changeState(ProcessState* state) {
delete currentState;
currentState = state;
}
Process::Process(std::queue<int> burstTimes, std::queue<int> IOTimes) :
processID(processCounter++),
burstTimes(burstTimes),
IOTimes(IOTimes) {
changeState(new ProcessReadyState(this));
}
Process::~Process() {
delete currentState;
}
bool Process::isCompleted()
{
if (burstTimes.empty() && IOTimes.empty())
return true;
return false;
}
void Process::start() {
changeState(new ProcessRunningState(this));
}
void Process::update()
{
currentState->update();
}
void Process::printProcess()
{
std::cout << "Process #" << processID << "\n";
std::cout << "\t" << "Status: " << currentState->getName() << "\n";
std::cout << "\t" << "Time Waiting: " << timeWaiting << "\n";
std::cout << "\t" << "Time Running: " << timeRunning << "\n";
std::cout << "\t" << "Time Waiting For I/O: " << timeIO << "\n\n";
}
std::string Process::getState() {
return currentState->getName();
}
int Process::getID() {
return processID;
}