forked from Dajvid/IPS-proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsed.c
184 lines (153 loc) · 3.79 KB
/
psed.c
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
/* David Sedlák <xsedla1d>, Karel Hanák <xhanak34> */
#include <stdio.h>
#include <unistd.h>
#include <thread>
#include <queue>
#include <mutex>
#include <vector>
#include <iostream>
#include <string.h>
#include <regex>
using namespace std;
vector <mutex *> locks;
mutex *entered_chain_mutex, *finished_mutex;
bool line_ready = false, line_processed = false, thread_terminate = false;
int in_chain = 0, finished = 0;
char *line;
/* converts c++ string to null terminated array of chars (char *)*/
char *to_cstr(string a) {
char *str;
str = (char *)malloc(sizeof(char) * (a.length() + 1));
strcpy(str, a.c_str());
return str;
}
/* reads one line from input */
char *read_line(int *res) {
string line;
char *str;
if (getline(cin, line)) {
str = to_cstr(line);
*res = 1;
return str;
} else {
*res = 0;
return NULL;
}
}
/* implementation of worker thread */
void worker_thread(int ID, char *RE, char *REPL, int thread_count) {
char *buf;
int iteration = 0;
while (1) {
/* lock mutex for next thread in chain, no one is waiting for last thread */
if (ID != (thread_count - 1)) {
locks[ID]->lock();
}
/* thread ID is now part of the chain, increment shared counter of threads in chain */
entered_chain_mutex->lock();
in_chain++;
entered_chain_mutex->unlock();
while (!line_ready) {
if (thread_terminate) {
return;
}
}
/* apply regex */
regex re1(RE);
string res = regex_replace(line, re1, REPL);
char *str = to_cstr(res);
/* make sure all threads are in the chain before entering the stage of unchaining */
while(in_chain != thread_count);
/**
* Wait for output of predecesing thread
* Thread with id 0 doesn't have to wait for anything
*/
if (!ID == 0) {
locks[ID - 1]->lock();
}
/* print the output */
printf("%s\n", str);
free(str);
if (ID != (thread_count - 1)) {
locks[ID]->unlock();
} else {
/**
* last thread must set information about completing the line
* and set default value of shared variables
*/
in_chain = 0;
line_ready = false;
line_processed = true;
}
/* unlock mutex of predeceasing thread to be ready for next line */
if (ID != 0) {
locks[ID - 1]->unlock();
}
/* threads wait until the line is processed */
while(!line_processed);
finished_mutex->lock();
finished++;
finished_mutex->unlock();
}
}
int main(int argc, char **argv) {
if (argc < 3 || !((argc -1 ) % 2 == 0)) {
printf("Invalid arguments\n");
return EXIT_FAILURE;
}
/* turn of buffering */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
int thread_count = (argc - 1) / 2;
int lock_count = thread_count - 1;
/* array of threads */
vector <thread *> threads;
/* try to create locks and threads */
try {
entered_chain_mutex = new mutex();
finished_mutex = new mutex();
locks.resize(lock_count);
for (int i = 0; i < lock_count; i++) {
mutex *new_zamek = new mutex();
locks[i] = new_zamek;
}
threads.resize(thread_count);
for(int i = 0; i < thread_count; i++){
thread *new_thread = new thread(worker_thread, i, argv[2 * i + 1], argv[2 * i + 2], thread_count);
threads[i] = new_thread;
}
} catch (bad_alloc&) {
fprintf(stderr, "Unable to allocate resources\n");
return(EXIT_FAILURE);
}
int res;
line = read_line(&res);
if (res) {
line_ready = true;
}
int processed = 0;
while (res) {
while(!line_processed);
free(line);
line = read_line(&res);
while (finished != thread_count);
finished = 0;
line_processed = false;
if (res) {
line_ready = true;
}
}
thread_terminate = true;
/* join and free threads */
for(int i = 0; i < thread_count; i++){
(*(threads[i])).join();
delete threads[i];
}
/* free locks */
delete entered_chain_mutex;
delete finished_mutex;
for(int i = 0;i < lock_count; i++){
delete locks[i];
}
return EXIT_SUCCESS;
}