From af63bafed034ff45cc06cff4f2cf1511879f056b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sat, 6 Jul 2024 18:43:51 +0200 Subject: [PATCH] Passing the norm on all these files --- philo/.gitignore | 2 + philo/Makefile | 3 +- philo/includes/philo.h | 16 ++++++-- philo/sources/ft_actions.c | 14 +++++-- philo/sources/main.c | 52 ++++++++++++++++++++++++- philo/sources/parsing_args.c | 13 +------ philo/sources/routine.c | 75 ++++++++++++------------------------ 7 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 philo/.gitignore diff --git a/philo/.gitignore b/philo/.gitignore new file mode 100644 index 0000000..2d38ab2 --- /dev/null +++ b/philo/.gitignore @@ -0,0 +1,2 @@ +objects/ +philo diff --git a/philo/Makefile b/philo/Makefile index e669a45..5b319b9 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/06/26 17:25:08 by rparodi ### ########.fr # +# Updated: 2024/07/04 15:20:22 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -120,4 +120,3 @@ footer: .PHONY: all bonus clean fclean re -include ${OBJ:.o=.d} - diff --git a/philo/includes/philo.h b/philo/includes/philo.h index 1b4f95c..4bd7dc7 100644 --- a/philo/includes/philo.h +++ b/philo/includes/philo.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:34:04 by rparodi #+# #+# */ -/* Updated: 2024/06/26 17:15:16 by rparodi ### ########.fr */ +/* Updated: 2024/07/04 14:11:50 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,20 +70,28 @@ typedef struct s_program t_usize ft_time(void); void ft_free(void *ptr); +t_usize ft_strlen(t_str str); +void *ft_routine(void *ptr); +void *ft_watch_dogs(void *ptr); +t_bool dead_loop(t_philo *philo); +t_bool check_eat(t_philo *philo); t_usize ft_atou_return(t_str str); -t_error ft_sleeping(t_philo *philo); +t_bool check_dead(t_philo *philo); t_error ft_thinking(t_philo *philo); +t_error ft_sleeping(t_philo *philo); t_i32 main(t_i32 argc, t_str *argv); void ft_pause(size_t milliseconds); -t_error ft_start_eating(t_philo *philo); void ft_exit(t_str msg, t_u8 status); +t_error ft_start_eating(t_philo *philo); t_error ft_ending_eating(t_philo *philo); +void ft_putchar_fd(t_i32 fd, t_str str); void ft_logs(t_str msg, t_philo *philo); t_error ft_atou(const char *nptr, t_usize *value); t_error ft_init_fork(t_philo *philo, t_mutex *fork); +void counting_launch(t_philo *philo, t_u8 *counter); t_error ft_init_thread(t_program *prog, t_mutex *forks); t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo); -t_error ft_init(int argc, t_str *argv, t_program *program, t_philo *philo); +t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo); t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks); void ft_destroy_exit(t_str msg, t_u8 status, t_program *prog, \ t_mutex *forks); diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index 70186ac..ea23dbb 100644 --- a/philo/sources/ft_actions.c +++ b/philo/sources/ft_actions.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ -/* Updated: 2024/06/26 17:24:47 by rparodi ### ########.fr */ +/* Updated: 2024/07/04 11:48:33 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,8 @@ void ft_logs(t_str msg, t_philo *philo) const t_usize time = ft_time() - philo->start_time; if (msg != NULL) - printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, GREEN, msg, END); + printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, \ + GREEN, msg, END); } t_error ft_thinking(t_philo *philo) @@ -28,8 +29,6 @@ t_error ft_thinking(t_philo *philo) t_error ft_start_eating(t_philo *philo) { - const t_usize time = ft_time() - philo->start_time; - if (philo->id % 2 == 0) { pthread_mutex_lock(philo->l_fork); @@ -44,6 +43,13 @@ t_error ft_start_eating(t_philo *philo) pthread_mutex_lock(philo->l_fork); ft_logs("has taken a fork\n", philo); } + return (ft_ending_eating(philo)); +} + +t_error ft_ending_eating(t_philo *philo) +{ + const t_usize time = ft_time() - philo->start_time; + philo->eating = true; ft_logs("is eating\n", philo); philo->t_last_eat = time; diff --git a/philo/sources/main.c b/philo/sources/main.c index 825ce32..22f3b28 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,12 +6,60 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/06/16 12:22:40 by rparodi ### ########.fr */ +/* Updated: 2024/07/04 15:27:12 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" +void *ft_routine(void *ptr) +{ + t_philo *philo; + + philo = (t_philo *) ptr; + if (philo == NULL) + return (NULL); + while (!dead_loop(philo)) + { + ft_start_eating(philo); + ft_sleeping(philo); + ft_thinking(philo); + } + return (philo); +} + +t_error ft_init_thread(t_program *prog, t_mutex *forks) +{ + t_thread o_block; + t_usize i; + + i = 0; + if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos)) + ft_destroy_exit("Allocation of watch_dogs failed\n", \ + ERROR, prog, forks); + while (i < prog->philos[0].nb_philo) + { + if (pthread_create(&prog->philos[i].thread, NULL, &ft_routine, \ + &prog->philos[i])) + ft_destroy_exit("Allocation of one philo thread failed !\n", \ + ERROR, prog, forks); + i++; + } + while (--i > 0) + pthread_join(prog->philos[i].thread, NULL); + pthread_join(o_block, NULL); + return (NO_ERROR); +} + +t_usize ft_time(void) +{ + struct timeval time; + + if (gettimeofday(&time, NULL) == -1) + ft_exit("Error of during ft_time !\n", 1); + return (time.tv_sec * 1000 + time.tv_usec * 0.001); +} + t_i32 main(t_i32 argc, t_str *argv) { t_philo philo[MAXSUPPORT]; @@ -19,6 +67,8 @@ t_i32 main(t_i32 argc, t_str *argv) if (argc != 6 && argc != 5) ft_exit(ARGS, 1); + if (ft_atou_return(argv[1]) > MAXSUPPORT) + ft_exit("Please update the max support !\n", 1); if (ft_init(argc, argv, &prog, philo)) ft_exit("Issues during initialisation\n", ERROR); return (0); diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 90f0be9..91d1c8c 100644 --- a/philo/sources/parsing_args.c +++ b/philo/sources/parsing_args.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */ -/* Updated: 2024/06/26 17:16:09 by rparodi ### ########.fr */ +/* Updated: 2024/07/04 11:52:27 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,15 +48,6 @@ t_error ft_init_fork(t_philo *philo, t_mutex *fork) return (NO_ERROR); } -t_usize ft_time(void) -{ - struct timeval time; - - if (gettimeofday(&time, NULL) == -1) - ft_exit("Error of during ft_time !\n", 1); - return (time.tv_sec * 1000 + time.tv_usec * 0.001); -} - void ft_pause(size_t milliseconds) { const t_usize start = ft_time(); @@ -96,7 +87,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) { - static t_mutex forks[MAXSUPPORT] = { 0 }; + static t_mutex forks[MAXSUPPORT] = {0}; pthread_mutex_init(&prog->print_lock, NULL); pthread_mutex_init(&prog->dead_lock, NULL); diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 3bd1769..ed8139f 100644 --- a/philo/sources/routine.c +++ b/philo/sources/routine.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ -/* Updated: 2024/06/26 17:23:11 by rparodi ### ########.fr */ +/* Updated: 2024/07/04 13:29:39 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ t_bool dead_loop(t_philo *philo) t_bool check_dead(t_philo *philo) { t_usize i; - t_usize timecheck; + t_usize timecheck; i = 0; while (i < philo[0].nb_philo) @@ -32,7 +32,10 @@ t_bool check_dead(t_philo *philo) timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat; if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death) { + pthread_mutex_unlock(philo[i].dead_lock); ft_logs("died\n", &philo[i]); + philo[i].dead_check = true; + pthread_mutex_lock(philo[i].dead_lock); return (true); } i++; @@ -40,7 +43,22 @@ t_bool check_dead(t_philo *philo) return (false); } -t_bool check_eat(t_philo *philo) +void counting_launch(t_philo *philo, t_u8 *counter) +{ + t_usize i; + + i = 0; + while (i < philo[0].nb_philo) + { + pthread_mutex_lock(philo[i].check_eating_count); + if (philo[i].eating_count >= philo[i].nb_eat) + (*counter)++; + pthread_mutex_unlock(philo[i].check_eating_count); + i++; + } +} + +t_bool check_eat(t_philo *philo) { t_usize i; t_u8 check; @@ -49,21 +67,15 @@ t_bool check_eat(t_philo *philo) check = 0; if (philo[0].nb_eat == -1) return (false); - while (i < philo[0].nb_philo) - { - pthread_mutex_lock(philo[i].check_eating_count); - if (philo[i].eating_count >= philo[i].nb_eat) - check++; - pthread_mutex_unlock(philo[i].check_eating_count); - i++; - } - i = 0; + counting_launch(philo, &check); if (check == philo[0].nb_philo) { pthread_mutex_lock(philo[0].dead_lock); while (i < philo[0].nb_philo) { + pthread_mutex_unlock(philo[i].dead_lock); philo[i].dead_check = true; + pthread_mutex_lock(philo[i].dead_lock); i++; } pthread_mutex_unlock(philo[0].dead_lock); @@ -88,42 +100,3 @@ void *ft_watch_dogs(void *ptr) } return (philo); } - -void *ft_routine(void *ptr) -{ - t_philo *philo; - - philo = (t_philo *) ptr; - if (philo == NULL) - return (NULL); - while (!dead_loop(philo)) - { - ft_start_eating(philo); - ft_sleeping(philo); - ft_thinking(philo); - } - return (philo); -} - -t_error ft_init_thread(t_program *prog, t_mutex *forks) -{ - t_thread o_block; - t_usize i; - - i = 0; - if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos)) - ft_destroy_exit("Allocation of watch_dogs failed\n", \ - ERROR, prog, forks); - while (i < prog->philos[0].nb_philo) - { - if (pthread_create(&prog->philos[i].thread, NULL, &ft_routine, \ - &prog->philos[i])) - ft_destroy_exit("Allocation of one philo thread failed !\n", \ - ERROR, prog, forks); - i++; - } - while (--i > 0) - pthread_join(prog->philos[i].thread, NULL); - pthread_join(o_block, NULL); - return (NO_ERROR); -}