This repository was archived by the owner on Mar 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgrep.cpp
150 lines (127 loc) · 3.34 KB
/
pgrep.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
//Adam Venger xvenge00
/**
* Prvy projekt na IPS spolocna praca
* Peter Babka xbabka01
* Adam Venger xvenge00
*/
#include <stdio.h>
#include<unistd.h>
#include<thread>
#include<queue>
#include<mutex>
#include<vector>
#include <iostream>
#include<string.h>
#include <regex>
char *line;
int score;
int end = 1;
std::mutex res_thread, load_line;
unsigned working;
std::vector<std::mutex *> zamky; /* pole zamku promenne velikosti */
std::vector<std::regex *> reg; // zoradene regexy
std::vector<int> min_score; // skore regexu
char *read_line(int *res) {
std::string line;
char *str;
if (std::getline(std::cin, line)) {
str = (char *) malloc(sizeof(char) * (line.length() + 1));
strcpy(str, line.c_str());
*res = 1;
return str;
} else {
*res = 0;
return NULL;
}
}
void f(int ID) {
std::mutex *my_mutex;
std::regex *my_regex;
int my_score;
my_mutex = zamky[ID];
my_regex = reg[ID];
my_score = min_score[ID];
int res = 0;
while (1) {
while (!my_mutex->try_lock()) {
if (!end) {
return;
}
}
res = std::regex_match(line, (*my_regex));
res_thread.lock();
score += res ? my_score : 0;
working -= 1;
if (working == 0) {
load_line.unlock();
}
res_thread.unlock();
}
}
int main(int argc, char **argv) {
load_line.lock();
/*************************
* deklaracia premennych
*/
unsigned num = 0; // pocet regex
int minimum;
std::vector < std::thread * > thread;
/**************************
* kontroala vstupu a init
*/
if ((argc % 2 != 0) || (argc < 4)) {
fprintf(stderr, "Zle zadane argumenty\n");
return 1;
}
num = (argc - 2) / 2;
minimum = strtod(argv[1], NULL); //todo skontrolovat argv[1] ci je cislo
// nastavenie velikosti poli
zamky.resize(num);
thread.resize(num);
min_score.resize(num);
reg.resize(num);
// inicializovanie poli
for (unsigned i = 0; i < num; i++) {
std::mutex *new_mutex = new std::mutex();
std::regex *new_regex = new std::regex(argv[2 + 2 * i]);
int new_score = (int) strtod(argv[3 + 2 * i], NULL); //todo skontrolovat ci retazec je cislo
//zamknutie mutex
new_mutex->try_lock();
//nastavenie poli
zamky[i] = new_mutex;
reg[i] = new_regex;
min_score[i] = new_score;
}
// spustenie threadou
for (unsigned i = 0; i < num; i++) {
std::thread *new_thread = new std::thread(f, i);
thread[i] = new_thread;
}
/**********************************
* Vlastni vypocet pgrep
* ********************************/
line = read_line(&end);
while (end) {
score = 0;
working = num;
for (unsigned i = 0; i < num; i++) {
zamky[i]->unlock();
}
load_line.lock(); //todo deadlock
if (minimum <= score) {
printf("%s\n", line);
}
free(line);
line = read_line(&end);
}
/**********************************
* Uvolneni pameti
* ********************************/
for (unsigned i = 0; i < num; i++) {
(*(thread[i])).join();
delete thread[i];
delete zamky[i];
delete reg[i];
}
return 0;
}