From f023ec1a688039ea548680cc56df13c51f31f39b Mon Sep 17 00:00:00 2001 From: Hunter104 <000plagueinc@gmail.com> Date: Sat, 14 Sep 2024 22:17:11 -0300 Subject: [PATCH] refactor: parallelization with openmp --- CMakeLists.txt | 5 ++--- src/main.c | 46 +++++++++++++--------------------------------- src/proxy.c | 2 +- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8167517..58e7b27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) -set(THREADS_PREFER_PTHREADS_FLAG ON) -find_package(Threads REQUIRED) +find_package(OpenMP REQUIRED) add_executable(proxy_checker src/main.c ./src/proxy.c ./src/memory.c) target_include_directories(proxy_checker PRIVATE include) -target_link_libraries(proxy_checker PRIVATE curl cjson Threads::Threads) +target_link_libraries(proxy_checker PRIVATE curl cjson OpenMP::OpenMP_C) diff --git a/src/main.c b/src/main.c index 0b04fa9..60fe45b 100644 --- a/src/main.c +++ b/src/main.c @@ -15,20 +15,13 @@ #define RED_TEXT "\033[0;31m" #define RESET_TEXT "\033[0m" -typedef struct { - ProxyAddress address; - int thread_id; -} ThreadArgs; - -void *TestProxyThread(void *arg) { - ThreadArgs *args = (ThreadArgs *)arg; - char *status = CheckIsProxyAlive(args->address) - ? GREEN_TEXT "Success" RESET_TEXT - : RED_TEXT "Failure" RESET_TEXT; - printf("Proxy %s status: %s\n", args->address.url, status); - - free(args); - pthread_exit(NULL); +FILE *safe_fopen(const char *path, const char *mode) { + FILE *file = fopen(path, mode); + if (!file) { + fprintf(stderr, "Failed to open file %s\n", path); + abort(); + } + return file; } int main(int argc, char *argv[]) { @@ -37,29 +30,16 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - FILE *file = fopen(argv[1], "r"); - if (!file) { - fprintf(stderr, "Failed to open file: %s\n", argv[1]); - exit(EXIT_FAILURE); - } + FILE *file = safe_fopen(argv[1], "r"); ProxyVector *list = GetProxiesFromFile(file); fclose(file); - pthread_t threads[list->len]; - - for (int i = 0; i < list->len; i++) { - ThreadArgs *args = malloc(sizeof(ThreadArgs)); - args->address = list->data[i]; - args->thread_id = i; - - if (pthread_create(&threads[i], NULL, TestProxyThread, (void *)args)) { - fprintf(stderr, "Error creating thread %d\n", i + 1); - free(args); - } - } - +#pragma omp parallel for for (int i = 0; i < list->len; i++) { - pthread_join(threads[i], NULL); + char *status = CheckIsProxyAlive(list->data[i]) + ? GREEN_TEXT "Success" RESET_TEXT + : RED_TEXT "Failure" RESET_TEXT; + printf("Proxy %s status: %s\n", list->data[i].url, status); } freeVector(list); diff --git a/src/proxy.c b/src/proxy.c index f14ac1a..64c8703 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -7,7 +7,7 @@ #include #include #define INITIAL_CAPACITY 10 -#define TEST_URL "https://lumtest.com/myip.json" +#define TEST_URL "www.google.com" uint8_t getByte(uint32_t ipAddress, int n) { int power = 24 - (n << 3);