-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.cpp
113 lines (90 loc) · 3.13 KB
/
fork.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
#include <utility>
#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <sys/time.h>
#include <chrono>
#include "fork.h"
extern sem_t *log_sem;
#define SEMAPHORE_NAME ("/")
Fork::Fork( std::string fork_name, std::string owner_name)
{
this->name = std::move(fork_name);
this->owner = std::move(owner_name);
if ( (sem = sem_open((SEMAPHORE_NAME + name).c_str(), O_CREAT, 0777, 1)) == SEM_FAILED ) {
// writeLog(owner_name, "sem " + name + " opened");
writeSingleLog("sem " + name + " opened");
perror("sem_open");
}
}
Fork::~Fork()
{
sem_unlink((SEMAPHORE_NAME + name).c_str());
}
void Fork::take()
{
if (sem_trywait(sem) == 0) {
// writeLog(owner, "semaphore " + SEMAPHORE_NAME(name) + " acquired" + "\n");
writeSingleLog( "semaphore " + name + " acquired\n");
} else {
// writeLog(owner, "need to wait for semaphore " + name + "\n");
writeSingleLog( "need to wait for semaphore " + name + "\n");
sem_wait(sem);
}
// writeLog(owner, owner + " take fork " + name + "\n");
writeSingleLog(owner + " take fork " + name + "\n");
}
int Fork::takeRight()
{
if (sem_trywait(sem) == 0) {
// writeLog(owner, "semaphore " + SEMAPHORE_NAME(name) + " acquired" + "\n");
writeSingleLog( "semaphore " + name + " acquired\n");
} else {
// writeLog(owner, "need to wait for semaphore " + name + "\n");
writeSingleLog( "need to wait for semaphore " + name + "\n");
// sem_wait(sem);
return 1;
}
//sem_wait(sem);
// writeLog(owner, owner + " take fork " + name + "\n");
writeSingleLog(owner + " take fork " + name + "\n");
return 0;
}
void Fork::put()
{
// writeLog(owner, owner + " put fork " + name + "\n");
writeSingleLog(owner + " put fork " + name + "\n");
sem_post(sem);
}
void writeLog(std::string filename, std::string message) {
std::ofstream mylogStream;
mylogStream.open(filename.c_str(), std::ios::app);
std::chrono::system_clock::time_point time = std::chrono::system_clock::now();
time_t t = std::chrono::system_clock::to_time_t(time);
// std::string str = std::string(std::ctime(&t));
// str = str.substr(0, str.length() - 1);
std::tm * ptm = std::localtime(&t);
char buffer[32];
std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm);
timespec ts{};
clock_gettime(CLOCK_REALTIME, &ts);
mylogStream << buffer << ":" << std::to_string(ts.tv_nsec) + " " + message;
mylogStream.close();
}
void writeSingleLog(const std::string &message) {
sem_wait(log_sem);
std::ofstream mylogStream;
mylogStream.open("mylog.txt", std::ios::app);
std::chrono::system_clock::time_point time = std::chrono::system_clock::now();
time_t t = std::chrono::system_clock::to_time_t(time);
std::tm * ptm = std::localtime(&t);
char buffer[32];
std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm);
timespec ts{};
clock_gettime(CLOCK_REALTIME, &ts);
mylogStream << buffer << ":" << std::to_string(ts.tv_nsec) + " " + message;
mylogStream.close();
sem_post(log_sem);
}