Skip to content

Commit

Permalink
refactor: parallelization with openmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunter104 committed Sep 15, 2024
1 parent 6eff0a0 commit f023ec1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
46 changes: 13 additions & 33 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>
#include <string.h>
#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);
Expand Down

0 comments on commit f023ec1

Please sign in to comment.