From b192efa4a9b9f41ceeab5eeab7106e231f97cedb Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 26 Jan 2022 15:33:29 +0100 Subject: [PATCH] mise a la norme et renommer fonctions --- .gitmodules | 3 - Makefile | 21 ++----- headers/philo.h | 13 ++++- headers/philo_macro.h | 12 ++++ headers/philo_proto.h | 26 +++++++-- headers/philo_struct.h | 24 ++++++-- libft | 1 - srcs/exec.c | 14 ++++- srcs/generic.c | 71 +++++++++++------------- srcs/init.c | 12 ++++ srcs/main.c | 18 +++++- srcs/{main_thread.c => pere_fouettard.c} | 15 ++++- srcs/utils.c | 60 ++++++++++++++++++++ 13 files changed, 213 insertions(+), 77 deletions(-) delete mode 100644 .gitmodules delete mode 160000 libft rename srcs/{main_thread.c => pere_fouettard.c} (51%) create mode 100644 srcs/utils.c diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index cb2e6ae..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "libft"] - path = libft - url = git@bitbucket.org:LuckyLaszlo/libft.git diff --git a/Makefile b/Makefile index 18eb30a..67e872a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3 VPATH = $(DIR_SRCS) DIR_SRCS = srcs -INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) +INCLUDES = -I$(HEADERS_D) HEADERS_D = ./headers HEADERS = philo.h \ @@ -15,16 +15,13 @@ HEADERS = philo.h \ philo_proto.h \ philo_macro.h -LIBS = -L $(LIBFT_D) -lft \ - -lpthread - -LIBFT_D = ./libft -LIBFT = $(LIBFT_D)/libft.a +LIBS = -lpthread SRCS = main.c \ init.c \ - main_thread.c \ + pere_fouettard.c \ exec.c \ + utils.c \ generic.c DIR_OBJS = builds @@ -34,13 +31,7 @@ OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) # ------ RULES ------- # -------------------- -all: subsystem $(NAME) - -subsystem: - @cd $(LIBFT_D) && $(MAKE) - -$(LIBFT): # dispensable. utile seulement pour un appel direct à $(NAME), si $(LIBFT) n'existe pas - cd $(LIBFT_D) && $(MAKE) +all: $(NAME) $(DIR_OBJS)/%.o: %.c | $(DIR_OBJS) $(CC) $(CFLAGS) -c $< -o $@ @@ -50,7 +41,7 @@ $(DIR_OBJS): $(OBJS): $(HEADERS:%=$(HEADERS_D)/%) -$(NAME): $(OBJS) $(LIBFT) +$(NAME): $(OBJS) $(CC) $(OBJS) -o $(NAME) $(LIBS) clean: diff --git a/headers/philo.h b/headers/philo.h index f2d131d..79fd938 100644 --- a/headers/philo.h +++ b/headers/philo.h @@ -1,7 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:27:01 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:27:02 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PHILO_H # define PHILO_H -# include "libft.h" # include # include # include diff --git a/headers/philo_macro.h b/headers/philo_macro.h index 0147b5b..e69c529 100644 --- a/headers/philo_macro.h +++ b/headers/philo_macro.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_macro.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:27:08 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:27:09 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PHILO_MACRO_H # define PHILO_MACRO_H diff --git a/headers/philo_proto.h b/headers/philo_proto.h index b2d9668..dab46f0 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -1,21 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_proto.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:29:49 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PHILO_PROTO_H # define PHILO_PROTO_H // init.c t_philo *init(int ac, char **av, pthread_t **id); -// main_thread.c -int is_dead(t_philo *philo); +// pere_fouettard.c +int is_dead(t_philo *philo); void launch_threads(t_philo *philo, pthread_t *id); // exec.c void *philo_exec(void *arg); -// generic.c +// utils.c void init_time(t_philo *philo); void update_time(t_philo *philo); -int diff_time(t_time old, struct timeval new); -int print_message(t_philo *philo, char *clr, char *msg); +int diff_time(t_time old, struct timeval new); +int print_message(t_philo *philo, char *clr, char *msg); + +// generic.c +int ft_atoi(const char *str); #endif - diff --git a/headers/philo_struct.h b/headers/philo_struct.h index dd4518c..92340f0 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_struct.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:29:09 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PHILO_STRUCT_H # define PHILO_STRUCT_H -typedef pthread_mutex_t t_mtx; +typedef pthread_mutex_t t_mtx; typedef struct s_time { @@ -11,11 +23,11 @@ typedef struct s_time typedef struct s_params { - int n_phi; // number_of_philosophers - int t_die; // time_to_die - int t_eat; // time_to_eat - int t_slp; // time_to_sleep - int n_eat; // [number_of_times_each_philosopher_must_eat] + int n_phi; + int t_die; + int t_eat; + int t_slp; + int n_eat; } t_params; typedef struct s_global diff --git a/libft b/libft deleted file mode 160000 index af88c1c..0000000 --- a/libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit af88c1ce23f0daa453c70cee0c61a30f71c2a622 diff --git a/srcs/exec.c b/srcs/exec.c index 6b11454..f803d95 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:32:57 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "philo.h" void action_delay(t_philo *philo, int action_time) @@ -45,7 +57,7 @@ void *philo_exec(void *arg) { t_philo *philo; - philo = (t_philo*)arg; + philo = (t_philo *)arg; init_time(philo); if (philo->p_nbr % 2 == 0) usleep(10 * 1000); diff --git a/srcs/generic.c b/srcs/generic.c index 8629fd8..48ef485 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -1,48 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* generic.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -void init_time(t_philo *philo) +int ft_atoi(const char *str) { - struct timeval stime; + int i; + int result; + int is_negative; - if (philo->global->t_start.ts == 0) + is_negative = 0; + result = 0; + i = 0; + while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13)) + i++; + if (str[i] == '+') + i++; + else if (str[i] == '-') { - gettimeofday(&stime, NULL); - philo->global->t_start.ts = stime.tv_sec; - philo->global->t_start.tu = stime.tv_usec; + is_negative = 1; + i++; } - philo->t_last_meal.ts = philo->global->t_start.ts; - philo->t_last_meal.tu = philo->global->t_start.tu; -} - -void update_time(t_philo *philo) -{ - struct timeval stime; - - gettimeofday(&stime, NULL); - philo->t_last_meal.ts = stime.tv_sec; - philo->t_last_meal.tu = stime.tv_usec; -} - -int diff_time(t_time old, struct timeval new) -{ - return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000); -} - -int print_message(t_philo *philo, char *clr, char *msg) -{ - long int time_stamp; - struct timeval stime; - - pthread_mutex_lock(&(philo->global->m_print)); - if (philo->global->dead) + while (str[i] >= '0' && str[i] <= '9') { - pthread_mutex_unlock(&(philo->global->m_print)); - return (1); + result = (result * 10) + (str[i] - '0'); + i++; } - gettimeofday(&stime, NULL); - time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; - time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; - ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); - pthread_mutex_unlock(&(philo->global->m_print)); - return (0); + if (is_negative) + result = result * -1; + return (result); } diff --git a/srcs/init.c b/srcs/init.c index 4ffb585..d500df6 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:30:35 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "philo.h" t_philo *lst_add_philo(t_params *params, t_global *global, int i) diff --git a/srcs/main.c b/srcs/main.c index 4eb6e7b..4a223d1 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,14 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:40 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:32:30 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int main(int ac, char **av) { pthread_t *id; t_philo *philo; - int i; + int i; philo = init(ac, av, &id); if (philo == NULL) - return (0); + return (0); launch_threads(philo, id); i = 0; while (i < philo->params->n_phi) @@ -16,5 +28,5 @@ int main(int ac, char **av) pthread_join(id[i], NULL); i++; } - return (0); + return (0); } diff --git a/srcs/main_thread.c b/srcs/pere_fouettard.c similarity index 51% rename from srcs/main_thread.c rename to srcs/pere_fouettard.c index ad3eb93..47d0cf0 100644 --- a/srcs/main_thread.c +++ b/srcs/pere_fouettard.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pere_fouettard.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int is_dead(t_philo *philo) @@ -17,10 +29,9 @@ int is_dead(t_philo *philo) { pthread_mutex_lock(&(philo->global->m_print)); philo->global->dead = 1; - time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; - ft_printf("%s%i %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); + printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); pthread_mutex_unlock(&(philo->global->m_print)); return (1); } diff --git a/srcs/utils.c b/srcs/utils.c new file mode 100644 index 0000000..d89c284 --- /dev/null +++ b/srcs/utils.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:30:55 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +void init_time(t_philo *philo) +{ + struct timeval stime; + + if (philo->global->t_start.ts == 0) + { + gettimeofday(&stime, NULL); + philo->global->t_start.ts = stime.tv_sec; + philo->global->t_start.tu = stime.tv_usec; + } + philo->t_last_meal.ts = philo->global->t_start.ts; + philo->t_last_meal.tu = philo->global->t_start.tu; +} + +void update_time(t_philo *philo) +{ + struct timeval stime; + + gettimeofday(&stime, NULL); + philo->t_last_meal.ts = stime.tv_sec; + philo->t_last_meal.tu = stime.tv_usec; +} + +int diff_time(t_time old, struct timeval new) +{ + return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000); +} + +int print_message(t_philo *philo, char *clr, char *msg) +{ + long int time_stamp; + struct timeval stime; + + pthread_mutex_lock(&(philo->global->m_print)); + if (philo->global->dead) + { + pthread_mutex_unlock(&(philo->global->m_print)); + return (1); + } + gettimeofday(&stime, NULL); + time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; + time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; + printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); + pthread_mutex_unlock(&(philo->global->m_print)); + return (0); +}