-
Notifications
You must be signed in to change notification settings - Fork 2
/
my-fgrep.c
368 lines (276 loc) · 9.85 KB
/
my-fgrep.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#define _GNU_SOURCE
#include "../../lib/lib-misc.h"
#include <fcntl.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
typedef struct {
char buffer[BUFFER_SIZE];
bool done;
unsigned turn;
unsigned nreader;
pthread_mutex_t lock;
pthread_cond_t *pcond;
} shared_rf;
void init_shared_rf(shared_rf *s, unsigned nreader) {
int err;
s->done = 0;
s->turn = 1;
s->nreader = nreader;
if ((err = pthread_mutex_init(&s->lock, NULL)) != 0)
exit_with_err("pthread_mutex_init", err);
s->pcond = malloc(sizeof(pthread_cond_t) * (nreader + 1));
for (unsigned i = 0; i < nreader + 1; i++)
if ((err = pthread_cond_init(&s->pcond[i], NULL)) != 0)
exit_with_err("pthread_cond_init", err);
}
void destroy_shared_rf(shared_rf *s) {
pthread_mutex_destroy(&s->lock);
for (unsigned i = 0; i < s->nreader + 1; i++)
pthread_cond_destroy(&s->pcond[i]);
free(s->pcond);
free(s);
}
typedef struct {
char buffer[BUFFER_SIZE];
bool turn;
bool done;
pthread_mutex_t lock;
pthread_cond_t pcond[2];
pthread_barrier_t barrier;
} shared_fw;
void init_shared_fw(shared_fw *s) {
int err;
s->turn = s->done = 0;
if ((err = pthread_mutex_init(&s->lock, NULL)) != 0)
exit_with_err("pthread_mutex_init", err);
for (int i = 0; i < 2; i++)
if ((err = pthread_cond_init(&s->pcond[i], NULL)) != 0)
exit_with_err("pthread_cond_init", err);
if ((err = pthread_barrier_init(&s->barrier, NULL, 3)) != 0)
exit_with_err("pthread_barrier_init", err);
}
void destroy_shared_fw(shared_fw *s) {
pthread_mutex_destroy(&s->lock);
for (int i = 0; i < 2; i++)
pthread_cond_destroy(&s->pcond[i]);
pthread_barrier_destroy(&s->barrier);
free(s);
}
typedef struct {
// dati privati
pthread_t tid;
unsigned thread_n;
char *filename;
char *word;
bool i_flag;
bool v_flag;
// dati condivisi
shared_rf *srf;
shared_fw *sfw;
} thread_data;
bool reader_put_line(thread_data *td, char *strt) {
int err;
char *line;
bool ret_value = 1;
if ((err = pthread_mutex_lock(&td->srf->lock)) != 0)
exit_with_err("pthread_mutex_lock", err);
while (td->srf->turn != td->thread_n)
if ((err = pthread_cond_wait(&td->srf->pcond[td->thread_n],
&td->srf->lock)) != 0)
exit_with_err("pthread_cond_wait", err);
if ((line = strtok(strt, "\n")) == NULL) {
ret_value = 0;
td->srf->done = 1;
} else
strncpy(td->srf->buffer, line, BUFFER_SIZE);
td->srf->turn = 0;
if ((err = pthread_cond_signal(&td->srf->pcond[0])) != 0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->srf->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
return ret_value;
}
void reader(void *arg) {
thread_data *td = (thread_data *)arg;
int err, fd;
char *map, *strt;
struct stat statbuf;
if ((fd = open(td->filename, O_RDONLY)) == -1)
exit_with_sys_err("open");
if (fstat(fd, &statbuf) == -1)
exit_with_sys_err("fstat");
if ((map = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
fd, 0)) == NULL)
exit_with_sys_err("mmap");
if (close(fd) == -1)
exit_with_sys_err("close");
reader_put_line(td, map);
while (reader_put_line(td, NULL))
;
munmap(map, statbuf.st_size);
pthread_exit(NULL);
}
bool filter_pass(thread_data *td, char *line) {
char *word = NULL;
if (td->i_flag)
word = strcasestr(line, td->word);
else
word = strstr(line, td->word);
if (td->v_flag)
return word == NULL;
else
return word != NULL;
}
void filterer(void *arg) {
int err;
thread_data *td = (thread_data *)arg;
unsigned actual_reader = 1;
char *line = NULL;
char buffer[BUFFER_SIZE];
while (1) {
if ((err = pthread_mutex_lock(&td->srf->lock)) != 0)
exit_with_err("pthread_mutex_lock", err);
while (td->srf->turn != 0)
if ((err = pthread_cond_wait(&td->srf->pcond[0], &td->srf->lock)) !=
0)
exit_with_err("pthread_cond_wait", err);
if (td->srf->done) {
actual_reader++;
if (actual_reader > td->srf->nreader)
break;
td->srf->done = 0;
td->srf->turn = actual_reader;
if ((err = pthread_cond_signal(&td->srf->pcond[actual_reader])) !=
0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->srf->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
continue;
} else
strncpy(buffer, td->srf->buffer, BUFFER_SIZE);
td->srf->turn = actual_reader;
if ((err = pthread_cond_signal(&td->srf->pcond[actual_reader])) != 0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->srf->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
if (filter_pass(td, buffer)) {
// Inserimento nella struttura dati condivisa con il Writer
if ((err = pthread_mutex_lock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_lock", err);
while (td->sfw->turn != 0)
if ((err = pthread_cond_wait(&td->sfw->pcond[0],
&td->sfw->lock)) != 0)
exit_with_err("pthread_cond_wait", err);
strncpy(td->sfw->buffer, buffer, BUFFER_SIZE);
td->sfw->turn = 1;
if ((err = pthread_cond_signal(&td->sfw->pcond[1])) != 0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
}
}
if ((err = pthread_mutex_lock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_lock", err);
while (td->sfw->turn != 0)
if ((err = pthread_cond_wait(&td->sfw->pcond[0], &td->sfw->lock)) != 0)
exit_with_err("pthread_cond_wait", err);
td->sfw->done = 1;
td->sfw->turn = 1;
if ((err = pthread_cond_signal(&td->sfw->pcond[1])) != 0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
if ((err = pthread_barrier_wait(&td->sfw->barrier)) > 0)
exit_with_err("pthread_barrier_wait", err);
pthread_exit(NULL);
}
void writer(void *arg) {
int err;
thread_data *td = (thread_data *)arg;
while (1) {
if ((err = pthread_mutex_lock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_lock", err);
while (td->sfw->turn != 1)
if ((err = pthread_cond_wait(&td->sfw->pcond[1], &td->sfw->lock)) !=
0)
exit_with_err("pthread_cond_wait", err);
if (td->sfw->done)
break;
printf("%s\n", td->sfw->buffer);
td->sfw->turn = 0;
if ((err = pthread_cond_signal(&td->sfw->pcond[0])) != 0)
exit_with_err("pthread_cond_signal", err);
if ((err = pthread_mutex_unlock(&td->sfw->lock)) != 0)
exit_with_err("pthread_mutex_unlock", err);
}
if ((err = pthread_barrier_wait(&td->sfw->barrier)) > 0)
exit_with_err("pthread_barrier_wait", err);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: %s [-v] [-i] [word] <file-1> [file-2] [file-3] [...]\n",
argv[0]);
exit(EXIT_FAILURE);
}
int err;
int _from = 1;
char *word;
bool v_flag = 0;
bool i_flag = 0;
// parsing delle flag -i/-v (opzionali)
if (!strcmp(argv[1], "-v") || !strcmp(argv[2], "-v")) {
v_flag = 1;
_from++;
}
if (!strcmp(argv[1], "-i") || !strcmp(argv[2], "-i")) {
i_flag = 1;
_from++;
}
thread_data td[argc - _from + 1];
shared_rf *srf = malloc(sizeof(shared_rf));
shared_fw *sfw = malloc(sizeof(shared_fw));
init_shared_rf(srf, argc - _from - 1);
init_shared_fw(sfw);
unsigned thread_data_index = 0;
// Reader
for (unsigned i = _from + 1; i < argc; i++) {
td[thread_data_index].filename = argv[i];
td[thread_data_index].srf = srf;
td[thread_data_index].thread_n = thread_data_index + 1;
if ((err = pthread_create(&td[thread_data_index].tid, NULL,
(void *)reader, &td[thread_data_index])) != 0)
exit_with_err("pthread_create", err);
thread_data_index++;
}
// Filterer
td[thread_data_index].i_flag = i_flag;
td[thread_data_index].v_flag = v_flag;
td[thread_data_index].sfw = sfw;
td[thread_data_index].srf = srf;
td[thread_data_index].thread_n = 0;
td[thread_data_index].word = argv[_from];
if ((err = pthread_create(&td[thread_data_index].tid, NULL,
(void *)filterer, &td[thread_data_index])) != 0)
exit_with_err("pthread_create", err);
// Writer
thread_data_index++;
td[thread_data_index].sfw = sfw;
if ((err = pthread_create(&td[thread_data_index].tid, NULL, (void *)writer,
&td[thread_data_index])) != 0)
exit_with_err("pthread_create", err);
for (unsigned i = 0; i < thread_data_index + 1; i++)
if ((err = pthread_detach(td[i].tid)) != 0)
exit_with_err("pthread_detach", err);
if ((err = pthread_barrier_wait(&sfw->barrier)) > 0)
exit_with_err("pthread_barrier_wait", err);
destroy_shared_rf(srf);
destroy_shared_fw(sfw);
}