-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cpp
More file actions
83 lines (71 loc) · 2.03 KB
/
process.cpp
File metadata and controls
83 lines (71 loc) · 2.03 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
/*
g++ -std=c++11 -c utils.cpp -o utils.o
g++ -std=c++11 -c process.cpp -o process.o
g++ -std=c++11 utils.o process.o -lrpc -lpthread -o process
*/
#include <iostream>
#include <fstream>
#include <queue>
#include "rpc/server.h"
#include "rpc/client.h"
#include <mutex>
#include <chrono>
#include <iomanip>
#include <string>
#include <sstream>
#include <unistd.h>
#include "utils.h"
#define GRANT 0
#define REQUEST 1
#define RELEASE 2
#define PROCESS_IP "127.0.0.1"
#define COORDINATOR_IP "127.0.0.1"
#define COORDINATOR_PORT 9090
#define BASE_PORT 9091
bool is_granted = false;
int get_ith_integer_argument(int argc, char** argv, int index) {
if(argc != 4) {
std::cout << "Usage: process [id] [iter_number] [sleep_time]" << std::endl;
exit(-1);
}
if(index > argc)
exit(-1);
return std::stoi(argv[index]);
}
void grant()
{
is_granted = true;
}
void log_results(int pid, int sleep_time)
{
std::ofstream results;
results.open("resultado.txt", std::ios::app);
if (results.is_open()) {
results << "[" << pid << "] - " << get_date() << std::endl;
results.close();
sleep(sleep_time);
} else {
std::cout << "Erro ao abrir o arquivo!" << std::endl;
}
}
int main(int argc, char** argv)
{
int pid = get_ith_integer_argument(argc, argv, 1);
int iter_number = get_ith_integer_argument(argc, argv, 2);
int sleep_time = get_ith_integer_argument(argc, argv, 3);
int rcv_grant_port = BASE_PORT + pid;
rpc::server grant_server(rcv_grant_port);
grant_server.bind("grant", &grant);
grant_server.async_run(1);
rpc::client client(COORDINATOR_IP, COORDINATOR_PORT);
for(int r=0; r<iter_number; r++)
{
client.call("request", pid, PROCESS_IP, rcv_grant_port);
// TODO 8: pensar em usar blocking wait
while(!is_granted);
log_results(pid, sleep_time);
client.call("release", pid, PROCESS_IP);
is_granted = false;
}
return 0;
}