-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.cpp
192 lines (165 loc) · 5.59 KB
/
Process.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
//
// Created by Kyle Bolton on 10/6/16.
//
#include "Process.h"
int Process::run() {
pid = fork();
if(pid == -1){
throw std::runtime_error("failed to fork: " + std::string(strerror(errno)));
}
if(pid != 0){
inter_com->registerAsParent();
}else{
inter_com->registerAsChild();
for(auto instruction_iterator = instructions.begin();
instruction_iterator != instructions.end();
++instruction_iterator){
//read from pipe (wait)
inter_com->listenToParent();
if(!(*instruction_iterator)()){
//if instruction was not run, move iterator back
instruction_iterator--;
}
}
}
return pid;
}
Process::operator std::string() const {
return "pid: " + std::to_string(pid) + " id: " + std::to_string(id) + " index: " + std::to_string(index) + " deadline: " + std::to_string(deadline) + " remaining processing_time: " + std::to_string(processing_time);
}
bool Process::calculate(const unsigned int &ticks) {
return processingActions("calculate", ticks);
}
bool Process::useresources(const unsigned int &ticks) {
return processingActions("useresources", ticks);
}
bool Process::request(std::vector<unsigned int> requested_resources) {
return resourceActions("request", requested_resources);
}
bool Process::release(std::vector<unsigned int> requested_resources) {
return resourceActions("release", requested_resources);
}
bool Process::processingActions(const std::string &name, const unsigned int &ticks) {
inter_com->tellParent(name + " " + std::to_string(ticks));
//return true if successfully ran as reported by parent and false otherwise
std::unique_ptr<std::string> result = inter_com->listenToParent();
bool wasRun = (*result) == "success";
if(wasRun){
processing_time -= ticks;
//tell parent how much time it took
inter_com->tellParent(std::to_string(ticks));
}
return wasRun;
}
bool Process::resourceActions(const std::string& name, std::vector<unsigned int>& requested_resources){
std::string resources_string = StrOp::join(" ", requested_resources);
inter_com->tellParent(name + " " + resources_string);
std::unique_ptr<std::string> result = inter_com->listenToParent();
//return true if successfully ran as reported by parent and false otherwise
bool wasRun = (*result) == "success";
if(wasRun){
//reduce computation time by 1 if successful
processing_time--;
//tell parent how much time it took
inter_com->tellParent(std::to_string(1));
}
return wasRun;
}
void Process::pushInstruction(Instruction instruction, const unsigned int& ticks) {
//__bind<__mem_fn<void (Process::*)(const unsigned int&)>, Args& > temp = bind(mem_fn(&Process::calculate), forward<Args>(args));
std::function<bool()> delegate;
switch(instruction){
case Instruction::calculate:
delegate = [&]()->bool{
return calculate(ticks);
};
break;
case Instruction::useresources:
delegate = [&]()->bool{
return useresources(ticks);
};
break;
case Instruction::request:
case Instruction::release:
throw std::runtime_error("invalid arguments for Instruction");
}
instructions.push_back(delegate);
}
void Process::pushInstruction(Instruction instruction, std::vector<unsigned int> requested_resources) {
std::function<bool()> delegate;
switch(instruction){
case Instruction::request:
delegate = [this, requested_resources]()->bool{
return request(requested_resources);
};
break;
case Instruction::release:
delegate = [this, requested_resources]()->bool{
return release(requested_resources);
};
break;
case Instruction::calculate:
case Instruction::useresources:
throw std::runtime_error("invalid arguments for Instruction");
}
instructions.push_back(delegate);
}
std::unique_ptr<std::string> Process::instructionsToString(Instruction instruction) {
switch(instruction){
case Instruction::calculate:
return std::make_unique<std::string>("calculate");
case Instruction::useresources:
return std::make_unique<std::string>("useresources");
case Instruction::request:
return std::make_unique<std::string>("request");
case Instruction::release:
return std::make_unique<std::string>("release");
}
}
Process::Instruction Process::stringToInstruction(const std::string& name) {
std::regex calculate_regex("calculate");
std::regex useresources_regex("useresources");
std::regex request_regex("request");
std::regex release_regex("release");
Instruction to_return;
if(std::regex_match(name, calculate_regex)) {
to_return = Instruction::calculate;
}else if(std::regex_match(name, useresources_regex)){
to_return = Instruction::useresources;
}else if(std::regex_match(name, request_regex)){
to_return = Instruction::request;
}else if(std::regex_match(name, release_regex)){
to_return = Instruction::release;
}
return to_return;
}
unsigned int Process::getId() const {
return id;
}
void Process::setId(const unsigned int& id) {
Process::id = id;
}
unsigned int Process::getPid() const {
return pid;
}
void Process::setPid(const unsigned int pid) {
Process::pid = pid;
}
unsigned int Process::getDeadline() const {
return deadline;
}
void Process::setDeadline(const unsigned int deadline) {
Process::deadline = deadline;
}
unsigned int Process::getProcessingTime() const {
return processing_time;
}
void Process::setProcessingTime(const unsigned int processing_time) {
Process::processing_time = processing_time;
}
std::shared_ptr<InterCom> Process::getInterCom() {
return inter_com;
}
unsigned int Process::getIndex() const {
return index;
}