From ebe57f783d198f8c1af3716a523233af7083ed26 Mon Sep 17 00:00:00 2001 From: "Semere M. Mebrahtom" Date: Tue, 24 Mar 2026 20:16:27 +0100 Subject: [PATCH] all ops done; without synchronization --- Makefile | 12 +++++- coders/codexion.h | 32 ++++++++++------ coders/main.c | 98 +++++++++++++++++++++++++++++++++++++++-------- coders/utils.c | 6 +-- 4 files changed, 117 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 4e0df54..f4c94bb 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ INCLUDES = coders/codexion.h OBJS = $(SRCS:.c=.o) CC = cc -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra -Werror -pthread all: $(NAME) @@ -16,8 +16,16 @@ $(NAME): $(OBJS) %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< +# number_of_coders +# time_to_burnout +# time_to_compile +# time_to_debug +# time_to_refactor +# number_of_compiles_required +# dongle_cooldown +# scheduler test: $(NAME) - ./$(NAME) 0 1 2 3 4 5 6 7 + ./$(NAME) 10 2000 500 200 300 5 50 1 clean: rm -f $(OBJS) diff --git a/coders/codexion.h b/coders/codexion.h index 28cee84..daf802e 100644 --- a/coders/codexion.h +++ b/coders/codexion.h @@ -12,20 +12,30 @@ #ifndef CODEXION_H # define CODEXION_H +# include +# include + +typedef struct s_args +{ + int number_of_coders; + int time_to_burnout; + int time_to_compile; + int time_to_debug; + int time_to_refactor; + int number_of_compiles_required; + int dongle_cooldown; + int scheduler; +} t_args; typedef struct s_data { - int number_of_coders; - int time_to_burnout; - int time_to_compile; - int time_to_debug; - int time_to_refactor; - int number_of_compiles_required; - int dongle_cooldown; - int scheduler; -} t_data; + struct timeval start; + int coder_id; + t_args args; + pthread_mutex_t *left_dongle; + pthread_mutex_t *right_dongle; +} t_data; -// utils.h -t_data *parse_arguments(int count, char **args); +t_args *parse_arguments(int count, char **args); #endif diff --git a/coders/main.c b/coders/main.c index 959508b..2a0273f 100644 --- a/coders/main.c +++ b/coders/main.c @@ -11,25 +11,93 @@ /* ************************************************************************** */ #include "codexion.h" +#include #include #include +#include +#include + +void compile_op(t_data *data) +{ + struct timeval now; + int diff; + + gettimeofday(&now, NULL); + diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec + - data->start.tv_usec) / 1000; + printf("%d %d is compiling\n", diff, data->coder_id); + usleep(data->args.time_to_compile * 1000L); +} + +void debug_op(t_data *data) +{ + struct timeval now; + int diff; + + gettimeofday(&now, NULL); + diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec + - data->start.tv_usec) / 1000; + printf("%d %d is debugging\n", diff, data->coder_id); + usleep(data->args.time_to_debug * 1000L); +} + +void refactor_op(t_data *data) +{ + struct timeval now; + int diff; + + gettimeofday(&now, NULL); + diff = (now.tv_sec - data->start.tv_sec) * 1000L + (now.tv_usec + - data->start.tv_usec) / 1000; + printf("%d %d is refactoring\n", diff, data->coder_id); + usleep(data->args.time_to_refactor * 1000L); +} + +void *run(void *param) +{ + int i; + t_data *data; + + data = param; + i = 0; + while (i < 5) + { + compile_op(data); + debug_op(data); + refactor_op(data); + i++; + } + return (NULL); +} int main(int argc, char **argv) { - t_data *data; + t_args *args; + pthread_t *coders; + pthread_mutex_t *dongles; + int i; + struct timeval start; + t_data *data; - data = parse_arguments(argc - 1, &argv[1]); - if (!data) - return (0); - printf("d = [%d]\n", data->number_of_coders); - printf("d = [%d]\n", data->time_to_burnout); - printf("d = [%d]\n", data->time_to_compile); - printf("d = [%d]\n", data->time_to_debug); - printf("d = [%d]\n", data->time_to_refactor); - printf("d = [%d]\n", data->number_of_compiles_required); - printf("d = [%d]\n", data->dongle_cooldown); - printf("d = [%d]\n", data->scheduler); - if (data) - free(data); - data = NULL; + args = parse_arguments(argc - 1, &argv[1]); + coders = malloc(sizeof(pthread_t) * args->number_of_coders); + dongles = malloc(sizeof(pthread_mutex_t) * args->number_of_coders); + data = malloc(sizeof(t_data) * args->number_of_coders); + i = 0; + gettimeofday(&start, NULL); + while (i < args->number_of_coders) + { + data[i].args = *args; + data[i].coder_id = i + 1; + data[i].start = start; + pthread_create(&coders[i], NULL, run, (void *)&data[i]); + pthread_mutex_init(&dongles[i], NULL); + i++; + } + i = 0; + while (i < args->number_of_coders) + { + pthread_join(coders[i], NULL); + i++; + } } diff --git a/coders/utils.c b/coders/utils.c index c2b2a81..4c4743d 100644 --- a/coders/utils.c +++ b/coders/utils.c @@ -26,15 +26,15 @@ int isnumeric(char *str) return (1); } -t_data *parse_arguments(int count, char **args) +t_args *parse_arguments(int count, char **args) { int args_int[8]; - t_data *data; + t_args *data; int i; if (count != 8) return (NULL); - data = malloc(sizeof(t_data)); + data = malloc(sizeof(t_args)); if (!data) return (NULL); i = -1;