This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cpp
209 lines (164 loc) · 4.3 KB
/
Program.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Created by barto on 13.03.18.
//
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <fstream>
#include "Program.h"
#include "WaitForInput.h"
using namespace std;
int Program::numberOfPhilosophers = 5;
Waiter* Program::waiter = new Waiter(Program::numberOfPhilosophers);
vector<Philosopher*> Program::philosophers;
vector<thread> Program::threads;
bool Program::fileExport = false;
bool Program::shouldTerminate = false;
time_t Program::startTime;
void Program::start() {
cout<<"Save simulation output to file? [Y/n]: ";
char temp = 'n';
cin>>temp;
if (temp == 'Y' || temp == 'y') fileExport = true;
fstream* file;
if (fileExport) {
string path = to_string(time(0));
path += "-DiningPhilosophersProblem-" + to_string(numberOfPhilosophers) + ".txt";
file = new fstream(path, fstream::out);
if (!file->is_open()) {
cout << "There was a problem with creating a file. The app will continue without saving the output into a file." << endl;
} else {
cout << "Saving a simulation output to: " << path << endl;
}
}
thread waiterThread = waiter->spawnThread();
for (unsigned int i = 0; i < numberOfPhilosophers; i++) {
philosophers.push_back(new Philosopher(i, waiter));
}
string buffer = "";
buffer = getHeader();
cout<<buffer;
if (fileExport) (*file)<<buffer;
time(&startTime);
for (auto &philosopher : philosophers) {
threads.emplace_back(philosopher->spawnThread());
}
WaitForInput wfi(&philosophers);
thread waitThread = wfi.spawnThread();
while (!shouldTerminate) {
buffer = getThreadsStatus();
cout<<buffer;
if (fileExport) (*file)<<buffer;
this_thread::sleep_for(chrono::milliseconds(250));
}
waitThread.join();
waiter->setTerminate(true);
for (int i = 0; i <threads.size(); i++) {
threads[i].join();
}
waiter->wakeUp();
waiterThread.join();
buffer = "Simulation over.\n";
cout<<buffer;
if (fileExport) (*file)<<buffer;
if (fileExport) file->close();
}
string Program::getThreadsStatus() {
string output = "";
time_t currentTime;
time(¤tTime);
string diffTime = to_string((int) difftime(currentTime, startTime));
output += diffTime;
for (int i = 0; i < (5 - diffTime.length()); i++) {
output += " ";
}
output += "| ";
for (unsigned int i = 0; i < numberOfPhilosophers; i++) {
if (i == 0) {
if (philosophers[i]->getState() == 4)
shouldTerminate = true;
else
shouldTerminate = false;
} else {
if (shouldTerminate && philosophers[i]->getState() == 4)
shouldTerminate = true;
else
shouldTerminate = false;
}
if (philosophers[i]->getState() == 4) {
output += "Dead";
output += " ";
} else if (philosophers[i]->getState() == 3) {
output += "Eating";
output += " ";
} else if (philosophers[i]->getState() == 2) {
output += "Waiting";
output += " ";
} else if (philosophers[i]->getState() == 1) {
output += "Thinking";
output += " ";
} else if (philosophers[i]->getState() == 0)
output += "Not yet start";
else {
output += "Error!";
output += " ";
}
output += " | ";
}
if (waiter->getState() == 3) {
output += "Dead";
output += " ";
} else if (waiter->getState() == 2) {
output += "Checking";
output += " ";
} else if (waiter->getState() == 1) {
output += "Sleeping";
output += " ";
} else if (waiter->getState() == 0)
output += "Not yet start";
else {
output += "Error!";
output += " ";
}
output += " | ";
vector<bool> forks = waiter->getForks();
for(int i = 0; i < forks.size(); i++) {
if (forks[i]) {
output += "Free";
output += " ";
} else if (!forks[i]) {
output += "Occupied";
output += "";
} else {
output += "Error!";
output += " ";
}
if (i < numberOfPhilosophers - 1)
output += " | ";
else
output += "\n";
}
return output;
}
string Program::getHeader() {
string output = "";
output += "Time | ";
for (unsigned int i = 0; i < numberOfPhilosophers; i++) {
output += "Philosopher " + to_string(i);
output += " | ";
}
output += "Waiter | ";
for (unsigned int i = 0; i < numberOfPhilosophers; i++) {
output += "Fork " + to_string(i) + " ";
if (i < numberOfPhilosophers - 1)
output += " | ";
else
output += "\n";
}
for (unsigned int i = 0; i < 155; i++) {
output += "-";
}
output += "\n";
return output;
}