-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilosopher.cpp
70 lines (51 loc) · 1.67 KB
/
philosopher.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
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include <sys/time.h>
#include "philosopher.h"
#include "fork.h"
extern sem_t *log_sem;
Philosopher::Philosopher(int phil_num, std::string name, std::string leftFork_name,
std::string rightFork_name) : leftFork(std::move(leftFork_name), name), rightFork(std::move(rightFork_name), name) {
this->phil_num = phil_num;
this->name = name;
}
Philosopher::~Philosopher() {
sem_unlink(LOG_SEMAPHORE_NAME);
}
void Philosopher::eat() {
// writeLog(name, name + " start eating\n");
writeSingleLog(name + " start eating\n");
std::this_thread::sleep_for(std::chrono::milliseconds(phil_num * 1000));
// writeLog(name, name + " stop eating\n");
writeSingleLog(name + " stop eating\n");
}
void Philosopher::think() {
// writeLog(name, name + " start thinking\n");
writeSingleLog(name + " start thinking\n");
std::this_thread::sleep_for(std::chrono::milliseconds(phil_num * 1000));
// writeLog(name, name + " stop thinking\n");
writeSingleLog(name + " stop thinking\n");
}
void Philosopher::live() {
// writeLog(name, name + " start live\n");
writeSingleLog(name + " start live\n");
for (int i = 0; i < 10; i++)
//while (true)
{
leftFork.take();
int canTakeRight = rightFork.takeRight();
if (canTakeRight == 1) {
leftFork.put();
std::this_thread::sleep_for(std::chrono::milliseconds(phil_num * 500));
continue;
}
eat();
rightFork.put();
leftFork.put();
think();
}
// writeLog(name, name + " end live\n");
writeSingleLog(name + " end live\n");
}