Seems finished

This commit is contained in:
Ano-sys
2025-01-29 20:17:35 +01:00
parent e265ffa526
commit c0d2ffffd3
14 changed files with 184 additions and 122 deletions
+90 -27
View File
@@ -2,7 +2,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <regex.h>
#include <zmq.h>
@@ -44,7 +43,7 @@ char *get_file_content(FILE *fp)
if (delimiter_pos < 0)
{
printf("No delimiting character was found -> taking whole buffer");
fprintf(stderr, "No delimiting character was found -> taking whole buffer");
delimiter_pos = (int)bytes_read;
}
@@ -88,7 +87,7 @@ void *receiverFunc(void *args)
zmq_ctx_destroy(context);
return NULL;
}
printf("[THREAD] Connected to worker: %s\n", addr);
fprintf(stderr, "[THREAD] Connected to worker: %s\n", addr);
size_t len = strlen(params->data) + 4;
char *request = (char*)malloc(sizeof(char) * len);
@@ -117,7 +116,7 @@ void *receiverFunc(void *args)
zmq_ctx_destroy(context);
return NULL;
}
printf("[THREAD] Send request: %s\n", request);
fprintf(stderr, "[THREAD] Send request: %s\n", request);
if(zmq_send(requester, request, strlen(request), 0) == -1){
perror("[THREAD] Failed to send request");
free(params);
@@ -129,7 +128,7 @@ void *receiverFunc(void *args)
free(request);
printf("[THREAD]: Waiting for response\n");
fprintf(stderr, "[THREAD]: Waiting for response\n");
char reply[MESSAGE_LENGTH];
int bytes_recv = zmq_recv(requester, reply, MESSAGE_LENGTH - 1, 0);
if (bytes_recv == -1){
@@ -143,6 +142,7 @@ void *receiverFunc(void *args)
reply[bytes_recv] = '\0';
if(strcmp(reply, "rip") == 0){
perror("[THREAD] Got rip -> shutting down");
free(params);
zmq_close(requester);
zmq_ctx_destroy(context);
@@ -153,7 +153,7 @@ void *receiverFunc(void *args)
len = strlen(reply);
if(!words_list){
words_list = (char*)malloc(len + 1);
words_list = strdup(reply);
if(!words_list){
perror("[THREAD] Failed to allocate memory for words_list");
free(params);
@@ -177,8 +177,6 @@ void *receiverFunc(void *args)
words_list = tmp;
}
strcat(words_list, reply);
free(params);
pthread_mutex_unlock(&words_mutex);
@@ -189,7 +187,7 @@ void *receiverFunc(void *args)
return NULL;
}
char *map(size_t worker_count, char **worker_ports, char *filename){
void map(size_t worker_count, char **worker_ports, char *filename){
FILE *fp = fopen(filename, "r");
char *file_content_chunks;
int worker_index = 0;
@@ -235,7 +233,6 @@ char *map(size_t worker_count, char **worker_ports, char *filename){
}
free(threads);
return words_list;
}
char *extract_reduce_string(const char * toExtract){
@@ -256,7 +253,7 @@ char *extract_reduce_string(const char * toExtract){
return extracted;
}
char *reduce(char *toReduce, size_t worker_count, char **worker_ports){
void reduce(char *toReduce, size_t worker_count, char **worker_ports){
pthread_t *threads = malloc(sizeof(pthread_t) * worker_count);
if(!threads){
perror("[MAIN- reduce] Failed to allocate memory for threads");
@@ -264,16 +261,10 @@ char *reduce(char *toReduce, size_t worker_count, char **worker_ports){
exit(1);
}
key_value_pair *head, *tail;
extract_key_value_pairs(toReduce, &head, &tail);
size_t thread_count = 0;
key_value_pair *kv_it = head;
char *extraction_ptr = toReduce;
for(int i = 0; kv_it && i < worker_count; i++){
for(int i = 0; i < worker_count; i++){
THREAD_ARGS *args = (THREAD_ARGS*)malloc(sizeof(THREAD_ARGS));
if(!args){
perror("[MAIN - reduce] Failed to allocate memory for args");
@@ -291,7 +282,7 @@ char *reduce(char *toReduce, size_t worker_count, char **worker_ports){
continue;
}
extraction_ptr = toReduce + strlen(args->data) + 1;
extraction_ptr = toReduce + strlen(args->data);
if(pthread_create(&threads[thread_count], NULL, receiverFunc, (void*)args) != 0){
perror("[MAIN - reduce] Failed to create thread");
@@ -299,7 +290,6 @@ char *reduce(char *toReduce, size_t worker_count, char **worker_ports){
free(args);
continue;
}
kv_it = kv_it->next;
thread_count++;
}
@@ -309,7 +299,65 @@ char *reduce(char *toReduce, size_t worker_count, char **worker_ports){
}
free(threads);
return words_list;
}
void rip(size_t worker_count, char **worker_ports){
pthread_t *threads = malloc(sizeof(pthread_t) * worker_count);
if(!threads){
perror("[MAIN- reduce] Failed to allocate memory for threads");
pthread_mutex_destroy(&words_mutex);
exit(1);
}
size_t thread_count = 0;
for(int i = 0; i < worker_count; i++){
THREAD_ARGS *args = (THREAD_ARGS*)malloc(sizeof(THREAD_ARGS));
if(!args){
perror("[MAIN - rip] Failed to allocate memory for args");
free(threads);
pthread_mutex_destroy(&words_mutex);
exit(1);
}
args->port = atol(worker_ports[i]);
args->data = strdup("rip");
args->type = RIP;
if (!args->data) {
fprintf(stderr, "[MAIN - reduce] Warning: extract_reduce_string() returned NULL\n");
free(args->data);
free(args);
continue;
}
if(pthread_create(&threads[thread_count], NULL, receiverFunc, (void*)args) != 0){
perror("[MAIN - reduce] Failed to create thread");
free(args);
continue;
}
thread_count++;
}
// wait for workers to finish
for(int i = 0; i < thread_count; i++){
pthread_join(threads[i], NULL);
}
free(threads);
}
void print_words_list(char *list){
key_value_pair *kv, *head = NULL, *tail = NULL;
extract_key_value_pairs(list, &head, &tail, false);
kv = head;
printf("word,frequency\n");
while(kv){
printf("%s,%s\n", kv->key, kv->value);
kv = kv->next;
}
free_key_value_pairs(&head, &tail);
}
int main(const int argc, char **argv) {
@@ -321,15 +369,30 @@ int main(const int argc, char **argv) {
pthread_mutex_init(&words_mutex, NULL);
// MAP
char *word_list_mapped = map(argc - 2, argv + 2, argv[1]);
printf("MAP:\n%s\n", word_list_mapped);
map(argc - 2, argv + 2, argv[1]);
fprintf(stderr, "MAP:\n%s\n", words_list);
// TODO: REDUCE
char *word_list_reduced = reduce(word_list_mapped, argc - 2, argv + 2);
char *word_list_mapped = strdup(words_list);
free(words_list);
words_list = NULL;
// REDUCE
reduce(word_list_mapped, argc - 2, argv + 2);
free(word_list_mapped);
printf("REDUCE:\n%s\n", word_list_reduced);
// TODO: RIP
char *word_list_reduced = strdup(words_list);
free(words_list);
words_list = NULL;
fprintf(stderr, "REDUCE:\n%s\n", word_list_reduced);
// RIP
rip(argc - 2, argv + 2);
// print words_list
print_words_list(word_list_reduced);
free(word_list_reduced);
pthread_mutex_destroy(&words_mutex);
return 0;
}