From 5215c0e3f44ecf837bb2994665d611451af08470 Mon Sep 17 00:00:00 2001 From: EniumRaphael Date: Thu, 11 Jul 2024 18:11:14 +0200 Subject: [PATCH 01/10] Adding something working --- philo/sources/ft_actions.c | 9 +++++--- philo/sources/main.c | 25 +++++++++++++++++---- philo/sources/parsing_args.c | 4 ++-- philo/sources/routine.c | 43 ++++++++++++++++++------------------ 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index ea23dbb..a6b6243 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/07/04 11:48:33 by rparodi ### ########.fr */ +/* Updated: 2024/07/07 20:00:00 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,14 @@ void ft_logs(t_str msg, t_philo *philo) { + pthread_mutex_lock(philo->print_lock); 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); + GREEN, msg, END); + } + pthread_mutex_unlock(philo->print_lock); } t_error ft_thinking(t_philo *philo) diff --git a/philo/sources/main.c b/philo/sources/main.c index 22f3b28..aa9d50a 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/04 15:27:12 by rparodi ### ########.fr */ +/* Updated: 2024/07/11 17:34:21 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ void *ft_routine(void *ptr) philo = (t_philo *) ptr; if (philo == NULL) return (NULL); + pthread_mutex_lock(philo->print_lock); + printf("\n\nEntre dans ft_routine\n\n"); + pthread_mutex_unlock(philo->print_lock); while (!dead_loop(philo)) { ft_start_eating(philo); @@ -30,11 +33,11 @@ void *ft_routine(void *ptr) t_error ft_init_thread(t_program *prog, t_mutex *forks) { - t_thread o_block; + t_thread watch_dogs; t_usize i; i = 0; - if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos)) + if (pthread_create(&watch_dogs, 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) @@ -47,7 +50,7 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks) } while (--i > 0) pthread_join(prog->philos[i].thread, NULL); - pthread_join(o_block, NULL); + pthread_join(watch_dogs, NULL); return (NO_ERROR); } @@ -64,9 +67,23 @@ t_i32 main(t_i32 argc, t_str *argv) { t_philo philo[MAXSUPPORT]; t_program prog; + t_i32 i; + t_i32 j; + j = 1; if (argc != 6 && argc != 5) ft_exit(ARGS, 1); + while (j < argc) + { + i = 0; + while (argv[j][i] != '\0') + { + if (argv[j][i] < '0' || argv[j][i] > '9') + ft_exit("I have to take only numeric arguments !\n", 1); + i++; + } + j++; + } if (ft_atou_return(argv[1]) > MAXSUPPORT) ft_exit("Please update the max support !\n", 1); if (ft_init(argc, argv, &prog, philo)) diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 91d1c8c..c602f08 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/07/04 11:52:27 by rparodi ### ########.fr */ +/* Updated: 2024/07/11 17:31:53 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) philo->nb_eat = tmp; } else - philo->t_eat = -1; + philo->t_eat = ~0; ft_atou(argv[1], &tmp); philo->nb_philo = tmp; ft_atou(argv[2], &tmp); diff --git a/philo/sources/routine.c b/philo/sources/routine.c index ed8139f..8f875ce 100644 --- a/philo/sources/routine.c +++ b/philo/sources/routine.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ -/* */ -/* ::: :::::::: */ +/* */ +/* ::: :::::::: */ /* routine.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ +/* +:+ +:+ +:+ */ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ -/* Updated: 2024/07/04 13:29:39 by rparodi ### ########.fr */ +/* Updated: 2024/07/11 18:10:49 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,10 +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); + pthread_mutex_lock(philo[i].dead_lock); ft_logs("died\n", &philo[i]); philo[i].dead_check = true; - pthread_mutex_lock(philo[i].dead_lock); + pthread_mutex_unlock(philo[i].dead_lock); return (true); } i++; @@ -60,37 +60,27 @@ void counting_launch(t_philo *philo, t_u8 *counter) t_bool check_eat(t_philo *philo) { - t_usize i; t_u8 check; - i = 0; check = 0; - if (philo[0].nb_eat == -1) - return (false); 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); return (true); - } return (false); } void *ft_watch_dogs(void *ptr) { t_philo *philo; - + t_usize i; + philo = (t_philo *) ptr; if (philo == NULL) return (NULL); + i = philo[0].nb_philo; + pthread_mutex_lock(philo->print_lock); + printf("\n\nEntre dans ft_watch_dogs\n\n"); + pthread_mutex_unlock(philo->print_lock); while (true) { if (check_eat(philo) == true) @@ -98,5 +88,14 @@ void *ft_watch_dogs(void *ptr) if (check_dead(philo) == true) break ; } + pthread_mutex_lock(philo->print_lock); + printf("\n\nSortie de ft_watch_dogs\n\n"); + pthread_mutex_unlock(philo->print_lock); + while (i-- != 0) + { + pthread_mutex_lock(philo[i].dead_lock); + philo[i].dead_check = true; + pthread_mutex_unlock(philo[i].dead_lock); + } return (philo); } From d948c639ece8e2f499350b40030bf0be31d32008 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 18 Jul 2024 18:43:38 +0200 Subject: [PATCH 02/10] Miaou --- philo/sources/ft_actions.c | 14 +++++++++----- philo/sources/parsing_args.c | 4 ++-- philo/sources/routine.c | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index a6b6243..4314795 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/07/07 20:00:00 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 18:42:02 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,10 @@ void ft_logs(t_str msg, t_philo *philo) { + pthread_mutex_lock(philo->dead_lock); + if (philo->dead_check == true) + return ((void) pthread_mutex_unlock(philo->dead_lock)); + pthread_mutex_unlock(philo->dead_lock); pthread_mutex_lock(philo->print_lock); const t_usize time = ft_time() - philo->start_time; if (msg != NULL) @@ -34,17 +38,17 @@ t_error ft_start_eating(t_philo *philo) { if (philo->id % 2 == 0) { - pthread_mutex_lock(philo->l_fork); - ft_logs("has taken a fork\n", philo); pthread_mutex_lock(philo->r_fork); ft_logs("has taken a fork\n", philo); + pthread_mutex_lock(philo->l_fork); + ft_logs("has taken a fork\n", philo); } else { - pthread_mutex_lock(philo->r_fork); - ft_logs("has taken a fork\n", philo); pthread_mutex_lock(philo->l_fork); ft_logs("has taken a fork\n", philo); + pthread_mutex_lock(philo->r_fork); + ft_logs("has taken a fork\n", philo); } return (ft_ending_eating(philo)); } diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index c602f08..5803a59 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/07/11 17:31:53 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 18:37:43 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) philo->nb_eat = tmp; } else - philo->t_eat = ~0; + philo->nb_eat = -1; ft_atou(argv[1], &tmp); philo->nb_philo = tmp; ft_atou(argv[2], &tmp); diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 8f875ce..2429d59 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/07/11 18:10:49 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 18:38:04 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,8 @@ void counting_launch(t_philo *philo, t_u8 *counter) t_usize i; i = 0; + if (philo->nb_eat == -1) + return ; while (i < philo[0].nb_philo) { pthread_mutex_lock(philo[i].check_eating_count); From 501b45d763a94fab36d20e04159e2443f3f3d1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Thu, 18 Jul 2024 20:01:39 +0200 Subject: [PATCH 03/10] Finish thx to garfi the goat --- philo/.clangd | 5 +++++ philo/.vscode/settings.json | 5 +++++ philo/Makefile | 4 +++- philo/sources/ft_actions.c | 7 ++++++- philo/sources/main.c | 4 ++-- philo/sources/parsing_args.c | 7 +++++-- philo/sources/routine.c | 7 +++++-- 7 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 philo/.clangd create mode 100644 philo/.vscode/settings.json diff --git a/philo/.clangd b/philo/.clangd new file mode 100644 index 0000000..1180cb1 --- /dev/null +++ b/philo/.clangd @@ -0,0 +1,5 @@ +CompilerFlags: + Add: + - "-xc" + - "-I/usr/include" + - "-I/home/rparodi/Documents/philo-perso/philo/includes" diff --git a/philo/.vscode/settings.json b/philo/.vscode/settings.json new file mode 100644 index 0000000..73b9486 --- /dev/null +++ b/philo/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "philo.h": "c" + } +} \ No newline at end of file diff --git a/philo/Makefile b/philo/Makefile index 5b319b9..532308f 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/07/04 15:20:22 by rparodi ### ########.fr # +# Updated: 2024/07/18 18:51:45 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -23,6 +23,8 @@ RM = rm -rf # Flags CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD +# CFLAGS += -fsanitize=thread + INCLUDES = ./includes/philo.h \ ./includes/types.h diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index 4314795..bc59454 100644 --- a/philo/sources/ft_actions.c +++ b/philo/sources/ft_actions.c @@ -6,11 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ -/* Updated: 2024/07/18 18:42:02 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 19:29:14 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" +#include void ft_logs(t_str msg, t_philo *philo) { @@ -47,6 +48,8 @@ t_error ft_start_eating(t_philo *philo) { pthread_mutex_lock(philo->l_fork); ft_logs("has taken a fork\n", philo); + if (philo->nb_philo == 1) + return ((void)pthread_mutex_unlock(philo->l_fork), NO_ERROR); pthread_mutex_lock(philo->r_fork); ft_logs("has taken a fork\n", philo); } @@ -59,7 +62,9 @@ t_error ft_ending_eating(t_philo *philo) philo->eating = true; ft_logs("is eating\n", philo); + pthread_mutex_lock(philo->meal_lock); philo->t_last_eat = time; + pthread_mutex_unlock(philo->meal_lock); pthread_mutex_lock(philo->check_eating_count); philo->eating_count++; pthread_mutex_unlock(philo->check_eating_count); diff --git a/philo/sources/main.c b/philo/sources/main.c index aa9d50a..019d4cf 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/11 17:34:21 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 19:59:24 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks) ERROR, prog, forks); i++; } - while (--i > 0) + while (i-- > 0) pthread_join(prog->philos[i].thread, NULL); pthread_join(watch_dogs, NULL); return (NO_ERROR); diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 5803a59..cb11361 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/07/18 18:37:43 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 20:00:09 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,9 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) t_usize tmp; tmp = 0; + ft_atou(argv[1], &tmp); + if (tmp == 0) + return (ERROR); if (argc == 6) { ft_atou(argv[5], &tmp); @@ -24,7 +27,6 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) } else philo->nb_eat = -1; - ft_atou(argv[1], &tmp); philo->nb_philo = tmp; ft_atou(argv[2], &tmp); philo->t_death = tmp; @@ -65,6 +67,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) { prog->philos[i].id = i + 1; prog->philos[i].eating = 0; + prog->philos[i].eating_count = 0; ft_parsing_args(argc, argv, &prog->philos[i]); prog->philos[i].start_time = ft_time(); prog->philos[i].t_last_eat = 0; diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 2429d59..4d08670 100644 --- a/philo/sources/routine.c +++ b/philo/sources/routine.c @@ -29,15 +29,18 @@ t_bool check_dead(t_philo *philo) i = 0; while (i < philo[0].nb_philo) { + pthread_mutex_lock(philo->meal_lock); timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat; - if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death) + if (timecheck >= philo[i].t_death) { - pthread_mutex_lock(philo[i].dead_lock); + pthread_mutex_unlock(philo->meal_lock); ft_logs("died\n", &philo[i]); + pthread_mutex_lock(philo[i].dead_lock); philo[i].dead_check = true; pthread_mutex_unlock(philo[i].dead_lock); return (true); } + pthread_mutex_unlock(philo->meal_lock); i++; } return (false); From 3d145fb1e3931060b2c3d414e33e3990b65bbd31 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 18 Jul 2024 21:19:26 +0200 Subject: [PATCH 04/10] Editing to be normed \! --- philo/Makefile | 4 ++-- philo/includes/philo.h | 2 +- philo/sources/ft_actions.c | 7 ++++--- philo/sources/main.c | 5 +---- philo/sources/parsing_args.c | 6 +++--- philo/sources/routine.c | 18 ++++++------------ 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/philo/Makefile b/philo/Makefile index 532308f..38a1688 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/07/18 18:51:45 by rparodi ### ########.fr # +# Updated: 2024/07/18 20:55:39 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -23,7 +23,7 @@ RM = rm -rf # Flags CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -# CFLAGS += -fsanitize=thread +CFLAGS += -fsanitize=thread INCLUDES = ./includes/philo.h \ ./includes/types.h diff --git a/philo/includes/philo.h b/philo/includes/philo.h index 4bd7dc7..57e7eee 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/07/04 14:11:50 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 21:17:07 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index bc59454..859990e 100644 --- a/philo/sources/ft_actions.c +++ b/philo/sources/ft_actions.c @@ -6,21 +6,22 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ -/* Updated: 2024/07/18 19:29:14 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 21:11:30 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" -#include void ft_logs(t_str msg, t_philo *philo) { + t_usize time; + pthread_mutex_lock(philo->dead_lock); if (philo->dead_check == true) return ((void) pthread_mutex_unlock(philo->dead_lock)); pthread_mutex_unlock(philo->dead_lock); pthread_mutex_lock(philo->print_lock); - const t_usize time = ft_time() - philo->start_time; + time = ft_time() - philo->start_time; if (msg != NULL) { printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, \ diff --git a/philo/sources/main.c b/philo/sources/main.c index 019d4cf..d472c76 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/18 19:59:24 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 21:07:01 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,6 @@ void *ft_routine(void *ptr) philo = (t_philo *) ptr; if (philo == NULL) return (NULL); - pthread_mutex_lock(philo->print_lock); - printf("\n\nEntre dans ft_routine\n\n"); - pthread_mutex_unlock(philo->print_lock); while (!dead_loop(philo)) { ft_start_eating(philo); diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index cb11361..bf4075c 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/07/18 20:00:09 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 21:18:10 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) t_usize tmp; tmp = 0; + philo->eating = 0; + philo->eating_count = 0; ft_atou(argv[1], &tmp); if (tmp == 0) return (ERROR); @@ -66,8 +68,6 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) while (i < ft_atou_return(argv[1])) { prog->philos[i].id = i + 1; - prog->philos[i].eating = 0; - prog->philos[i].eating_count = 0; ft_parsing_args(argc, argv, &prog->philos[i]); prog->philos[i].start_time = ft_time(); prog->philos[i].t_last_eat = 0; diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 4d08670..02594e0 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/07/18 18:38:04 by rparodi ### ########.fr */ +/* Updated: 2024/07/18 21:18:49 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,14 +78,11 @@ void *ft_watch_dogs(void *ptr) { t_philo *philo; t_usize i; - + philo = (t_philo *) ptr; if (philo == NULL) return (NULL); i = philo[0].nb_philo; - pthread_mutex_lock(philo->print_lock); - printf("\n\nEntre dans ft_watch_dogs\n\n"); - pthread_mutex_unlock(philo->print_lock); while (true) { if (check_eat(philo) == true) @@ -93,14 +90,11 @@ void *ft_watch_dogs(void *ptr) if (check_dead(philo) == true) break ; } - pthread_mutex_lock(philo->print_lock); - printf("\n\nSortie de ft_watch_dogs\n\n"); - pthread_mutex_unlock(philo->print_lock); while (i-- != 0) { - pthread_mutex_lock(philo[i].dead_lock); - philo[i].dead_check = true; - pthread_mutex_unlock(philo[i].dead_lock); - } + pthread_mutex_lock(philo[i].dead_lock); + philo[i].dead_check = true; + pthread_mutex_unlock(philo[i].dead_lock); + } return (philo); } From c2db9acc15e5738d382554ecad02b92b6fdb5a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Tue, 23 Jul 2024 20:03:57 +0200 Subject: [PATCH 05/10] Adding some stuff --- philo/sources/main.c | 11 +++-- philo/sources/parsing_args.c | 14 +++--- philo/sources/routine.c | 4 +- philo/test.c | 84 ------------------------------------ 4 files changed, 16 insertions(+), 97 deletions(-) delete mode 100644 philo/test.c diff --git a/philo/sources/main.c b/philo/sources/main.c index d472c76..84645ca 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/18 21:07:01 by rparodi ### ########.fr */ +/* Updated: 2024/07/23 19:51:07 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,12 @@ void *ft_routine(void *ptr) return (NULL); while (!dead_loop(philo)) { - ft_start_eating(philo); - ft_sleeping(philo); - ft_thinking(philo); + if (check_dead(philo) == false) + ft_start_eating(philo); + if (check_dead(philo) == false) + ft_sleeping(philo); + if (check_dead(philo) == false) + ft_thinking(philo); } return (philo); } diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index bf4075c..f5653cc 100644 --- a/philo/sources/parsing_args.c +++ b/philo/sources/parsing_args.c @@ -6,11 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */ -/* Updated: 2024/07/18 21:18:10 by rparodi ### ########.fr */ +/* Updated: 2024/07/23 20:03:47 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" +#include t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) { @@ -28,7 +29,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) philo->nb_eat = tmp; } else - philo->nb_eat = -1; + philo->nb_eat = ~0u; philo->nb_philo = tmp; ft_atou(argv[2], &tmp); philo->t_death = tmp; @@ -92,10 +93,11 @@ t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) { static t_mutex forks[MAXSUPPORT] = {0}; - pthread_mutex_init(&prog->print_lock, NULL); - pthread_mutex_init(&prog->dead_lock, NULL); - pthread_mutex_init(&prog->meal_lock, NULL); - pthread_mutex_init(&prog->check_eating_count, NULL); + prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + ft_init_fork(prog->philos, forks); prog->philos = philo; prog->dead_flag = false; if (ft_init_philo(argc, argv, prog, forks)) diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 02594e0..1656413 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/07/18 21:18:49 by rparodi ### ########.fr */ +/* Updated: 2024/07/23 19:49:21 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,8 +51,6 @@ void counting_launch(t_philo *philo, t_u8 *counter) t_usize i; i = 0; - if (philo->nb_eat == -1) - return ; while (i < philo[0].nb_philo) { pthread_mutex_lock(philo[i].check_eating_count); diff --git a/philo/test.c b/philo/test.c deleted file mode 100644 index 3181d44..0000000 --- a/philo/test.c +++ /dev/null @@ -1,84 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* test.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/06/09 15:08:23 by rparodi #+# #+# */ -/* Updated: 2024/06/17 12:24:16 by rparodi ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include -#include -#include - -// printf derriere un mutex pour le printf -// zero printf apres une mort -// mort shared avec un mutex -// compte le nombre de ois manger par philo, et chec via un thread si tous a manger X fois - -pthread_t philosopher[15]; -pthread_mutex_t chopstick[15]; - -int ft_sleep(size_t milliseconds) -{ - struct timeval start; - struct timeval tempo; - - gettimeofday(&tempo, NULL); - gettimeofday(&start, NULL); - while ((tempo - start) < milliseconds) - { - usleep(500); - gettimeofday(&tempo, NULL); - } - return (0); -} - -void *func(void *arg) { - int n = *((int *)arg); - printf("Philosopher %d is thinking\n", n); - - pthread_mutex_lock(&chopstick[n]); - pthread_mutex_lock(&chopstick[(n + 1) % 15]); - printf("Philosopher %d is eating\n", n); - ft_sleep(3000000); // sleep for 3 seconds to simulate eating - pthread_mutex_unlock(&chopstick[n]); - pthread_mutex_unlock(&chopstick[(n + 1) % 15]); - printf("Philosopher %d finished eating\n", n); - - free(arg); // Free the allocated memory - return NULL; -} - -int main() { - int i; - - for (i = 0; i < 15; i++) - pthread_mutex_init(&chopstick[i], NULL); - - for (i = 0; i < 15; i++) { - int *arg = malloc(sizeof(*arg)); - if (arg == NULL) { - fprintf(stderr, "Couldn't allocate memory for thread arg.\n"); - exit(EXIT_FAILURE); - } - *arg = i; - pthread_create(&philosopher[i], NULL, func, arg); - } - - for (i = 0; i < 15; i++) - pthread_join(philosopher[i], NULL); - - for (i = 0; i < 15; i++) - pthread_mutex_destroy(&chopstick[i]); - - return 0; -} - From 6f7b3a44d2916aaa99bd89f4e3371f9567a962da Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 24 Jul 2024 12:33:15 +0200 Subject: [PATCH 06/10] Pushing from the laptop the sources --- philo/includes/philo.h | 8 ++++---- philo/sources/parsing_args.c | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/philo/includes/philo.h b/philo/includes/philo.h index 57e7eee..3429142 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/07/18 21:17:07 by rparodi ### ########.fr */ +/* Updated: 2024/07/23 21:04:56 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,11 +42,11 @@ typedef struct s_philo t_usize t_eat; t_usize t_death; t_usize t_sleep; - t_i32 nb_eat; + t_usize nb_eat; pthread_t thread; t_i32 id; t_i32 eating; - t_i32 eating_count; + t_usize eating_count; t_usize t_last_eat; t_usize start_time; t_bool dead_check; @@ -87,7 +87,7 @@ 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); +t_error ft_init_fork(t_mutex *fork, t_usize nb_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); diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index f5653cc..3495d51 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/07/23 20:03:47 by rparodi ### ########.fr */ +/* Updated: 2024/07/23 21:07:50 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,14 +40,15 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) return (NO_ERROR); } -t_error ft_init_fork(t_philo *philo, t_mutex *fork) +t_error ft_init_fork(t_mutex *fork, t_usize nb_fork) { t_usize i; i = 0; - while (i < philo->nb_philo) + while (i < nb_fork) { - pthread_mutex_init(&fork[i], NULL); + printf("\tforks[%zu]\n", i + 1); + fork[i] = (t_mutex)PTHREAD_MUTEX_INITIALIZER; i++; } return (NO_ERROR); @@ -82,6 +83,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; else prog->philos[i].r_fork = &forks[i - 1]; + printf("\tphilo[%zu]\n", i + 1); i++; } if (ft_parsing_args(argc, argv, prog->philos)) @@ -97,9 +99,9 @@ t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER; - ft_init_fork(prog->philos, forks); prog->philos = philo; prog->dead_flag = false; + ft_init_fork(forks, ft_atou_return(argv[1])); if (ft_init_philo(argc, argv, prog, forks)) return (ERROR); if (philo->nb_philo != 0 && philo->nb_philo > MAXSUPPORT) From 9285a20e3527e3820992d8470f6d9dde6eae69d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Wed, 24 Jul 2024 14:46:47 +0200 Subject: [PATCH 07/10] Finished and normed --- philo/Makefile | 5 +++-- philo/sources/ft_actions.c | 9 ++++++++- philo/sources/main.c | 10 ++++++---- philo/sources/parsing_args.c | 27 +++++++++++++-------------- philo/sources/routine.c | 7 +++++-- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/philo/Makefile b/philo/Makefile index 38a1688..7a6aa0b 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/07/18 20:55:39 by rparodi ### ########.fr # +# Updated: 2024/07/24 14:12:44 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -23,7 +23,8 @@ RM = rm -rf # Flags CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -CFLAGS += -fsanitize=thread +# CFLAGS += -fsanitize=address +# CFLAGS += -fsanitize=thread INCLUDES = ./includes/philo.h \ ./includes/types.h diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index 859990e..79a8cff 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/07/18 21:11:30 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 14:16:00 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,13 @@ t_error ft_thinking(t_philo *philo) t_error ft_start_eating(t_philo *philo) { + if (philo->nb_philo == 1) + { + ft_logs("has taken a fork\n", philo); + while (!dead_loop(philo)) + usleep(50); + return (NO_ERROR); + } if (philo->id % 2 == 0) { pthread_mutex_lock(philo->r_fork); diff --git a/philo/sources/main.c b/philo/sources/main.c index 84645ca..901110a 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/23 19:51:07 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,15 @@ void *ft_routine(void *ptr) philo = (t_philo *) ptr; if (philo == NULL) return (NULL); + while (philo->start_time > ft_time()) + usleep(500); while (!dead_loop(philo)) { - if (check_dead(philo) == false) + if (dead_loop(philo) == false) ft_start_eating(philo); - if (check_dead(philo) == false) + if (dead_loop(philo) == false) ft_sleeping(philo); - if (check_dead(philo) == false) + if (dead_loop(philo) == false) ft_thinking(philo); } return (philo); diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 3495d51..8440900 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/07/23 21:07:50 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 14:23:03 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,14 +23,15 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo) ft_atou(argv[1], &tmp); if (tmp == 0) return (ERROR); + philo->nb_philo = tmp; + philo->nb_eat = ~0u; if (argc == 6) { ft_atou(argv[5], &tmp); + if (tmp == 0) + return (ERROR); philo->nb_eat = tmp; } - else - philo->nb_eat = ~0u; - philo->nb_philo = tmp; ft_atou(argv[2], &tmp); philo->t_death = tmp; ft_atou(argv[3], &tmp); @@ -47,7 +48,6 @@ t_error ft_init_fork(t_mutex *fork, t_usize nb_fork) i = 0; while (i < nb_fork) { - printf("\tforks[%zu]\n", i + 1); fork[i] = (t_mutex)PTHREAD_MUTEX_INITIALIZER; i++; } @@ -65,13 +65,15 @@ void ft_pause(size_t milliseconds) t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) { t_usize i; + t_usize start_time; + start_time = ft_time() + 500; i = 0; while (i < ft_atou_return(argv[1])) { prog->philos[i].id = i + 1; ft_parsing_args(argc, argv, &prog->philos[i]); - prog->philos[i].start_time = ft_time(); + prog->philos[i].start_time = start_time; prog->philos[i].t_last_eat = 0; prog->philos[i].print_lock = &prog->print_lock; prog->philos[i].dead_lock = &prog->dead_lock; @@ -83,22 +85,19 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; else prog->philos[i].r_fork = &forks[i - 1]; - printf("\tphilo[%zu]\n", i + 1); i++; } - if (ft_parsing_args(argc, argv, prog->philos)) - return (ERROR); - return (NO_ERROR); + return (ft_parsing_args(argc, argv, prog->philos)); } t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) { static t_mutex forks[MAXSUPPORT] = {0}; - prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; - prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; - prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; - prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER; + prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER; prog->philos = philo; prog->dead_flag = false; ft_init_fork(forks, ft_atou_return(argv[1])); diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 1656413..e3ec46e 100644 --- a/philo/sources/routine.c +++ b/philo/sources/routine.c @@ -6,16 +6,17 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ -/* Updated: 2024/07/23 19:49:21 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" +#include t_bool dead_loop(t_philo *philo) { pthread_mutex_lock(philo->dead_lock); - if (philo[0].dead_check == true) + if (philo->dead_check == true) return (pthread_mutex_unlock(philo->dead_lock), true); pthread_mutex_unlock(philo->dead_lock); return (false); @@ -80,6 +81,8 @@ void *ft_watch_dogs(void *ptr) philo = (t_philo *) ptr; if (philo == NULL) return (NULL); + while (philo[0].start_time > ft_time()) + usleep(500); i = philo[0].nb_philo; while (true) { From 07dae4049c286ca62654f424c885072a3ae5e68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Wed, 24 Jul 2024 16:18:49 +0200 Subject: [PATCH 08/10] Miaouuuu --- philo/sources/ft_actions.c | 7 +++---- philo/sources/main.c | 8 +++++++- philo/sources/parsing_args.c | 9 +++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index 79a8cff..010f0c6 100644 --- a/philo/sources/ft_actions.c +++ b/philo/sources/ft_actions.c @@ -6,11 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ -/* Updated: 2024/07/24 14:16:00 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 16:09:21 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/philo.h" +#include void ft_logs(t_str msg, t_philo *philo) { @@ -45,7 +46,7 @@ t_error ft_start_eating(t_philo *philo) usleep(50); return (NO_ERROR); } - if (philo->id % 2 == 0) + if (philo->id % 2 == 1) { pthread_mutex_lock(philo->r_fork); ft_logs("has taken a fork\n", philo); @@ -56,8 +57,6 @@ t_error ft_start_eating(t_philo *philo) { pthread_mutex_lock(philo->l_fork); ft_logs("has taken a fork\n", philo); - if (philo->nb_philo == 1) - return ((void)pthread_mutex_unlock(philo->l_fork), NO_ERROR); pthread_mutex_lock(philo->r_fork); ft_logs("has taken a fork\n", philo); } diff --git a/philo/sources/main.c b/philo/sources/main.c index 901110a..da01864 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/24 14:07:27 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 16:15:16 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,12 +15,18 @@ void *ft_routine(void *ptr) { t_philo *philo; + t_usize nb_group; + nb_group = 2; philo = (t_philo *) ptr; if (philo == NULL) return (NULL); + if (philo->nb_philo % 2) + nb_group = 3; while (philo->start_time > ft_time()) usleep(500); + if (philo->id % nb_group) + usleep(philo->t_eat * 800); while (!dead_loop(philo)) { if (dead_loop(philo) == false) diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 8440900..f60d109 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/07/24 14:23:03 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 16:05:12 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,11 +80,16 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) prog->philos[i].meal_lock = &prog->meal_lock; prog->philos[i].dead_check = false; prog->philos[i].check_eating_count = &prog->check_eating_count; - prog->philos[i].l_fork = &forks[i]; if (i == 0) prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; else prog->philos[i].r_fork = &forks[i - 1]; + if (prog->philos[0].nb_philo % 2 == 1 && i == prog->philos[0].nb_philo) + { + prog->philos[i].l_fork = &forks[0]; + prog->philos[i].r_fork = &forks[i]; + } + prog->philos[i].l_fork = &forks[i]; i++; } return (ft_parsing_args(argc, argv, prog->philos)); From ba0b395a0529db3d8b7a69942004f2eec61cfc2d Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 24 Jul 2024 20:24:56 +0200 Subject: [PATCH 09/10] Normed all the stuff --- philo/sources/main.c | 4 ++-- philo/sources/parsing_args.c | 7 +------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/philo/sources/main.c b/philo/sources/main.c index da01864..6d75a06 100644 --- a/philo/sources/main.c +++ b/philo/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */ -/* Updated: 2024/07/24 16:15:16 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 20:22:29 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ void *ft_routine(void *ptr) { t_philo *philo; - t_usize nb_group; + t_usize nb_group; nb_group = 2; philo = (t_philo *) ptr; diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index f60d109..2e499c7 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/07/24 16:05:12 by rparodi ### ########.fr */ +/* Updated: 2024/07/24 20:23:09 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,11 +84,6 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; else prog->philos[i].r_fork = &forks[i - 1]; - if (prog->philos[0].nb_philo % 2 == 1 && i == prog->philos[0].nb_philo) - { - prog->philos[i].l_fork = &forks[0]; - prog->philos[i].r_fork = &forks[i]; - } prog->philos[i].l_fork = &forks[i]; i++; } From e6f8fc4c16c4ce7d93e9ad606297e7b47b52bc46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= <35407363+EniumRaphael@users.noreply.github.com> Date: Tue, 5 Aug 2025 16:15:29 +0200 Subject: [PATCH 10/10] Delete philo/.vscode directory --- philo/.vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 philo/.vscode/settings.json diff --git a/philo/.vscode/settings.json b/philo/.vscode/settings.json deleted file mode 100644 index 73b9486..0000000 --- a/philo/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "philo.h": "c" - } -} \ No newline at end of file