-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiningPhilosophers.cpp
More file actions
199 lines (164 loc) · 4.41 KB
/
diningPhilosophers.cpp
File metadata and controls
199 lines (164 loc) · 4.41 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
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
#include <iostream>
#include <process/defer.hpp>
#include <process/dispatch.hpp>
#include <process/future.hpp>
#include <process/pid.hpp>
#include <process/process.hpp>
#include <stout/nothing.hpp>
#include <stout/os.hpp>
using namespace process;
using std::string;
using std::vector;
class TableProcess : public Process<TableProcess>
{
public:
// TableProcess()
// {
// this->numberOfPhilosophers = numberOfPhilosophers;
// }
void setNumber(int i)
{
numberOfPhilosophers = i;
philosophersLeft = i;
}
Future<bool> askForFork(int i)
{
if (i == numberOfPhilosophers) {
i = 0;
}
if (forks[i]) {
forks[i] = false;
return true;
} else {
return false;
}
}
Nothing replaceFork(int i)
{
if (i == numberOfPhilosophers) {
i = 0;
}
forks[i] = true;
return Nothing();
}
void checkIfDone()
{
philosophersLeft--;
if (philosophersLeft == 0) {
std::cout << "All Done" << std::endl;
done.set(Nothing());
terminate(self());
}
}
Future<Nothing> empty() { return done.future(); }
virtual void initialize()
{
for (int i = 0; i < numberOfPhilosophers; i++){
forks.push_back(true);
}
}
private:
Promise<Nothing> done;
vector<bool> forks;
int philosophersLeft;
int numberOfPhilosophers;
};
class Table {
public:
Table (int i) { spawn(process); process.setNumber(i); }
~Table () { terminate(process); wait(process); }
Future<Nothing> empty() { return dispatch(process, &TableProcess::empty); }
Future<bool> askForFork(int i) { return dispatch(process, &TableProcess::askForFork, i); }
Future<Nothing> replaceFork(int i) { return dispatch(process, &TableProcess::replaceFork, i); }
void checkIfDone() { dispatch(process, &TableProcess::checkIfDone); }
private:
TableProcess process;
};
class PhilosopherProcess : public Process<PhilosopherProcess>
{
public:
PhilosopherProcess(Table *table, int philosopherNumber, int servingsLeft)
{
this->table = table;
this->philosopherNumber = philosopherNumber;
this->servingsLeft = servingsLeft;
}
protected:
void think() {
int i = rand();
if (i % 2 == 0) {
double d = ((double) rand() / (RAND_MAX)) + 1;
os::sleep(Seconds(d));
}
}
Future<Nothing> replaceBoth()
{
return table->replaceFork(philosopherNumber).then([this] (Nothing) { return table->replaceFork(philosopherNumber + 1); });
}
virtual void initialize()
{
think();
perform();
}
void eat()
{
std::cout << philosopherNumber << " Eating" << std::endl;
os::sleep(Seconds(((double) rand() / (RAND_MAX)) + 1));
std::cout << philosopherNumber << " Done Eating" << std::endl;
servingsLeft--;
}
Nothing perform()
{
if (servingsLeft == 0) {
table->checkIfDone();
terminate(self());
return Nothing();
}
table->askForFork(philosopherNumber)
.then(defer(self(), [this] (bool available) {
if (available) {
std::cout << philosopherNumber << " picked up left fork" << std::endl;
table->askForFork(philosopherNumber + 1)
.then(defer(self(), [this] (bool available) -> Future<Nothing> {
if (available) {
std::cout << philosopherNumber << " picked up right fork" << std::endl;
eat();
return replaceBoth().then([this] (Nothing) { return perform(); });
} else {
std::cout << philosopherNumber << " Right fork unavailable" << std::endl;
return table->replaceFork(philosopherNumber).then([this] (Nothing) { think(); return perform(); });
}
}));
} else {
std::cout << philosopherNumber << " Left fork unavailable" << std::endl;
think();
return perform();
}
}));
}
private:
Table *table;
int philosopherNumber;
int servingsLeft;
};
int main(int argc, char** argv)
{
srand(time(0));
int numberOfPhilosophers;
std::cout << "How many philosophers?" << std::endl;
std::cin >> numberOfPhilosophers;
if (numberOfPhilosophers == 1) {
std::cout << "You need at least 2 philosophers to eat" << std::endl;
return 0;
}
int servings;
std::cout << "How many times should each philosopher eat?" << std::endl;
std::cin >> servings;
Table table(numberOfPhilosophers);
for (int i = 0; i < numberOfPhilosophers; i++) {
PhilosopherProcess* philosopher = new PhilosopherProcess(&table, i, servings);
spawn(philosopher, true);
}
table.empty().await();
return 0;
}