-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessManager.cpp
165 lines (131 loc) · 4.57 KB
/
ProcessManager.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
//
// Created by Gianluca on 24/05/2019.
//
#include "ProcessManager.h"
#include <windows.h>
#include <sstream>
#include <iostream>
#include <cmath>
#include <utility>
#include "IOManager.h"
#include "Utils.h"
#include "Genetic.h"
#include <thread>
#include <chrono>
extern char programName[50];
ProcessManager::ProcessManager(std::shared_ptr<Solution> sol, Genetic &gen) :
gen_(gen){
solution_ = std::move(sol);
}
ProcessManager::~ProcessManager() {
//std::cout << "destroying ProcessManager" << std::endl;
if(running_){
TerminateProcess(pi_.hProcess, 0);
CloseHandle( pi_.hProcess );
CloseHandle( pi_.hThread );
CloseHandle(si_.hStdError);
}
std::this_thread::sleep_for(std::chrono::seconds(1));
if(!folderName_.empty())
cleanFS();
}
void ProcessManager::startProcess() {
std::stringstream name;
name << programName << " " << folderName_ << " " << solution_->getArrayString();
char nameChar[400];
strcpy(nameChar, name.str().c_str());
ZeroMemory( &si_, sizeof(si_) );
si_.cb = sizeof(si_);
ZeroMemory( &pi_, sizeof(pi_) );
redirectOutputToNull();
running_ = CreateProcess(nullptr, // No module name (use command line)
nameChar, // Command line
nullptr, // Process handle not inheritable
nullptr, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
nullptr, // Use parent's environment block
nullptr, // Use parent's starting directory
&si_, // Pointer to STARTUPINFO structure
&pi_ ) != 0;
if(!running_)
throw utils::GeneticException();
}
std::pair<double, bool> ProcessManager::runAnalysis() {
DWORD returnValue;
startProcess();
WaitForSingleObject( pi_.hProcess, INFINITE );
int exitcode = GetExitCodeProcess(pi_.hProcess, &returnValue);
CloseHandle( pi_.hProcess );
CloseHandle( pi_.hThread );
CloseHandle(si_.hStdError);
running_ = false;
if(gen_.stop_ || !exitcode || returnValue != 0) {
//std::cout << "Failed" << std::endl;
throw utils::GeneticException();
}
return computeObjf(folderName_);
}
std::pair<double, bool> ProcessManager::computeObjf(std::string &folderName) {
//read from file and compute objf value
std::vector<double> values;
const double maxD1 = 0.021, maxD25 = 0.007;
bool feasible = true;
//read from output files
for(int i=0; i<5; i++){
std::string filename = folderName + "/" + std::to_string(i) + ".txt";
try {
values.push_back(IOManager::getMaxAbsValue(filename));
}
catch(...){
throw utils::GeneticException();
}
}
double tot = 0;
for(int i=0; i<5; i++){
auto val = values.at(i);
if((i == 0 && val >= maxD1) || (i != 0 && val > maxD25)) {
feasible = false;
}
tot += std::pow(val, 2);
}
//TODO vedi se aggiungere un moltiplicatore per tener conto del num di colonne
return std::make_pair(std::sqrt(tot), feasible);
}
void ProcessManager::createDirectory(){
if(!CreateDirectoryA(folderName_.c_str(), nullptr))
throw utils::GeneticException();
}
void ProcessManager::cleanFS() {
for(int i=0; i<5; i++){
std::string name = folderName_ + "/" + std::to_string(i) + ".txt";
DeleteFileA(name.c_str());
}
std::string nullfile = folderName_ + "/null";
DeleteFileA(nullfile.c_str());
RemoveDirectoryA(folderName_.c_str());
}
void ProcessManager::setFolderName(int id){
folderName_ = "__folder" + std::to_string(id) + "__";
}
void ProcessManager::redirectOutputToNull(){
std::string filename = folderName_ + "/null";
char namech[100];
strcpy(namech, filename.c_str());
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE h = CreateFile(namech,
FILE_APPEND_DATA,
FILE_SHARE_WRITE | FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
si_.cb = sizeof(STARTUPINFO);
si_.dwFlags |= STARTF_USESTDHANDLES;
si_.hStdInput = NULL;
si_.hStdError = h;
si_.hStdOutput = h;
}