-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.c
377 lines (300 loc) · 9.87 KB
/
encode.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
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct stat sb[100];
unsigned char *addr[100];
int args = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
char** results;
pthread_mutex_t results_mutex;
pthread_cond_t results_cond;
int chunk_size[250000];
int completed[250000] = {0};
int all_tasks_created = 0;
typedef struct task_node {
int chunk_index;
int chunk_start;
int chunk_end;
unsigned char* chunk_data;
struct task_node* next;
} TaskNode;
typedef struct task_queue{
TaskNode* head;
TaskNode* tail;
} TaskQueue;
TaskQueue queue;
void enqueue_task(TaskQueue* queue, int chunk_index, unsigned char* chunk_data, int chunk_start, int chunk_end) {
TaskNode* new_node = malloc(sizeof(TaskNode));
new_node->chunk_index = chunk_index;
new_node->chunk_data = chunk_data;
new_node->chunk_start = chunk_start;
new_node->chunk_end = chunk_end;
new_node->next = NULL;
if (queue->head->next == NULL) {
queue->head->next = new_node;
new_node->next = queue->tail;
}
else{
new_node->next = queue->head->next;
queue->head->next = new_node;
}
}
int isEmpty(TaskQueue* queue) {
int t_f = (queue->head->next == queue->tail);
return t_f;
}
TaskNode* dequeue_task(TaskQueue* queue) {
TaskNode* node = queue->head->next;
queue->head->next = queue->head->next->next;
return node;
}
void* worker_encode() {
TaskNode* node;
for(;;) {
pthread_mutex_lock(&mutex);
while(isEmpty(&queue)) {
if(all_tasks_created) {
pthread_mutex_unlock(&mutex);
return NULL;
}
pthread_cond_wait(&cond, &mutex);
}
node = dequeue_task(&queue);
int string_index = node->chunk_index;
results[string_index] = (char*)malloc((4096 * 2) * sizeof(char));
pthread_mutex_unlock(&mutex);
unsigned char count = 1;
int start = node->chunk_start;
int end = node->chunk_end;
int size = end - start;
unsigned char * addr = node->chunk_data;
char* buffer = results[string_index];
int index = 0;
if(size == 1) {
buffer[index++] = addr[0];
buffer[index++] = count;
}
for(int i = start; i < end; i++) {
if(addr[i] == addr[i + 1]) {
count++;
}
else {
buffer[index++] = addr[i];
buffer[index++] = count;
count = 1;
}
if(i + 2 == end) {
if(addr[i] == addr[i + 1]) {
buffer[index++] = addr[i];
buffer[index++] = count;
}
else {
buffer[index++] = addr[i + 1];
buffer[index++] = count;
}
break;
}
}
pthread_mutex_lock(&results_mutex);
chunk_size[string_index] = index;
completed[string_index] = 1;
pthread_cond_signal(&results_cond);
free(node);
pthread_mutex_unlock(&results_mutex);
}
}
void encode() {
//declare chars to hold the last char and count of that char of a file
unsigned char lastchar = '1';
unsigned int lastcount;
//begins to process files
for(int i = 0; i < args; i++) {
unsigned char count = 1;
if(sb[i].st_size == 1) {
if(lastchar != '1') {
if(addr[i][0] == lastchar) {
count = lastcount + 1;
}
}
lastchar = addr[i][0];
lastcount = 1;
if(i == args - 1 ) {
fwrite(&addr[i][0], 1, 1, stdout);
fwrite(&count, 1, 1, stdout);
}
continue;
}
//checks last char value
if(lastchar != '1') {
//if they are lastchar == first char of new file, update count
if(addr[i][0] == lastchar) {
count = lastcount + 1;
}
else {
//if not print it out
fwrite(&lastchar, 1,1, stdout);
fwrite(&lastcount, 1,1, stdout);
}
}
for(int m = 0; m < sb[i].st_size; m++) {
//checks if the current char is equal to the next one
if(addr[i][m] == addr[i][m + 1]) {
count++;
}
else {
//if they aren't print out current count
fwrite(&addr[i][m], 1, 1, stdout);
fwrite(&count, 1, 1, stdout);
count = 1;
}
//checks to see if its 2 chars before the end of stream
if(m + 2 == sb[i].st_size) {
//this block of code is just to make sure you don't go beyond the number of chars
//this means you're at the last 2 chars, so if they're equal just print them out
if(addr[i][m] == addr[i][m + 1]) {
if(i == args - 1 ) {
fwrite(&addr[i][m], 1, 1, stdout);
fwrite(&count, 1, 1, stdout);
}
lastchar = addr[i][m];
}
else {
//if they aren't equal then print out the last char
if(i == args - 1 ) {
fwrite(&addr[i][m + 1], 1, 1, stdout);
fwrite(&count, 1, 1, stdout);
}
lastchar = addr[i][m + 1];
}
lastcount = count;
break;
}
}
}
}
int main (int argc, char *argv[]) {
int opt, thread_num = 0;
char ** filenames = NULL;
extern char *optarg;
extern int optind;
while ((opt = getopt(argc, argv, "j:")) != -1) {
if (opt == 'j') {
thread_num = atoi(optarg);
break;
}
}
args = argc - optind;
filenames = &argv[optind];
int fd[args];
//opens file descriptors
for(int i = 0; i < args; i++) {
fd[i] = open(filenames[i], O_RDONLY);
}
//gets the size of the file descriptors
for(int i = 0; i < args; i ++) {
if (fstat(fd[i], &sb[i]) == -1)
printf("error");
}
//maps these files to memory
for(int i = 0; i < args; i++) {
addr[i] = mmap(NULL, sb[i].st_size, PROT_READ, MAP_PRIVATE, fd[i], 0);
}
pthread_t threads[thread_num];
//start working
if(thread_num == 0) {
encode();
}
else {
//multi-threaded
results = (char**)malloc(250000 * sizeof(char*));
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_cond_init(&results_cond, NULL);
pthread_mutex_init(&results_mutex, NULL);
queue.head = malloc(sizeof(TaskNode));
queue.tail = malloc(sizeof(TaskNode));
queue.head->next = queue.tail;
queue.tail->next = NULL;
for(int i = 0; i < thread_num; i++) {
pthread_create(&threads[i], NULL, worker_encode, NULL);
}
//enqueue_task(TaskQueue* queue, int chunk_index, char* chunk_data, int chunk_start, int chunk_end)
//create tasks
int chunk_index = 0;
int total_chunks = 0;
for(int i = 0; i < args; i++) {
int file_size = sb[i].st_size;
int chunks = file_size / 4096;
int remainder = file_size - (chunks * 4096);
if(remainder != 0) {
chunks++;
}
for(int m = 0; m < chunks; m++) {
pthread_mutex_lock(&mutex);
if(m == chunks - 1) {
enqueue_task(&queue, chunk_index++, addr[i], m * 4096, file_size);
}
else {
enqueue_task(&queue, chunk_index++, addr[i], m * 4096, (m + 1) * 4096);
}
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
total_chunks += chunks;
}
pthread_mutex_lock(&mutex);
all_tasks_created = 1;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
//chunk_size[i] = number chunk_size[i] - 1 = letter
int i = 0;
pthread_mutex_lock(&results_mutex);
for(;;) {
if(!completed[i]) {
pthread_cond_wait(&results_cond, &results_mutex);
}
else {
pthread_mutex_unlock(&results_mutex);
if(i != 0) {
if(results[i][0] == results[i - 1][chunk_size[i - 1] - 2]) {
unsigned char count = results[i][1] + results[i - 1][chunk_size[i - 1] - 1];
results[i][1] = count;
} else {
fwrite(&results[i - 1][chunk_size[i - 1] - 2], 1, 1, stdout);
fwrite(&results[i - 1][chunk_size[i - 1] - 1], 1, 1, stdout);
}
}
fwrite(results[i], 1, chunk_size[i] - 2, stdout);
i++;
pthread_mutex_lock(&results_mutex);
}
if(total_chunks == i) {
fwrite(&results[i - 1][chunk_size[i - 1] - 2], 1, 1, stdout);
fwrite(&results[i - 1][chunk_size[i - 1] - 1], 1, 1, stdout);
break;
}
}
pthread_mutex_unlock(&results_mutex);
for(int i = 0; i < thread_num; i++) {
pthread_join(threads[i], NULL);
}
free(queue.head);
free(queue.tail);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
pthread_cond_destroy(&results_cond);
pthread_mutex_destroy(&results_mutex);
}
for(int i = 0; i < args; i++) {
munmap(addr[i], sb[i].st_size);
close(fd[i]);
}
return 0;
}