From 5fdc221dd1bd15517b8e27fd62062f4238ae5110 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Fri, 18 Mar 2022 03:34:44 +0100 Subject: [PATCH] no datarace --- srcs/exit.c | 5 ++++- srcs/init.c | 16 ++++++------- srcs/philo.c | 55 +++++++++++++++++++++++++-------------------- srcs/philosophers.h | 2 ++ srcs/utils.c | 21 +++++++++++++++++ 5 files changed, 66 insertions(+), 33 deletions(-) diff --git a/srcs/exit.c b/srcs/exit.c index 065c5a2..7eb691b 100644 --- a/srcs/exit.c +++ b/srcs/exit.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/15 17:40:53 by apommier #+# #+# */ -/* Updated: 2022/03/17 00:22:47 by apommier ### ########.fr */ +/* Updated: 2022/03/18 00:09:18 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ void exit_philo(t_philo *philo, t_arg *data) usleep(100000); while (i < data->nb_philo - 1) { + pthread_mutex_destroy(&philo[i].check_nb_eat); + pthread_mutex_destroy(&philo[i].eat_check); pthread_mutex_destroy(philo[i].left_fork); free(philo[i].left_fork); i++; @@ -28,6 +30,7 @@ void exit_philo(t_philo *philo, t_arg *data) pthread_mutex_destroy(philo[i].left_fork); free(philo[i].left_fork); pthread_mutex_destroy(data->display); + pthread_mutex_destroy(&data->life_check); free(data->display); free(philo); free(data); diff --git a/srcs/init.c b/srcs/init.c index c20a1a4..e6e13ed 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/15 17:38:58 by apommier #+# #+# */ -/* Updated: 2022/03/16 21:50:42 by apommier ### ########.fr */ +/* Updated: 2022/03/18 00:05:43 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ t_arg *create_data(int argc, char **argv) pthread_mutex_t *display; display = malloc(sizeof(pthread_mutex_t)); - if (!display) + data = malloc(sizeof(t_arg)); + if (!display || !data) return (0); pthread_mutex_init(display, 0); - data = malloc(sizeof(t_arg)); pthread_mutex_init(&data->life_check, 0); data->display = display; data->death = 1; @@ -54,15 +54,15 @@ t_philo *create_philo(t_arg *data) philo[i].data = data; philo[i].nb_eat = 0; philo[i].left_fork = malloc(sizeof(pthread_mutex_t)); + if (!philo[i].left_fork) + return (0); pthread_mutex_init(philo[i].left_fork, 0); pthread_mutex_init(&philo[i].eat_check, 0); + pthread_mutex_init(&philo[i].check_nb_eat, 0); i++; } - i = 0; - while (i < data->nb_philo) - { + i = -1; + while (++i < data->nb_philo) philo[i].right_fork = philo[(i + 1) % data->nb_philo].left_fork; - i++; - } return (philo); } diff --git a/srcs/philo.c b/srcs/philo.c index fbfe678..e4bedcc 100644 --- a/srcs/philo.c +++ b/srcs/philo.c @@ -26,51 +26,59 @@ void philo_eat(t_philo *philo) philo->last_eat = get_time(); pthread_mutex_unlock(&philo->eat_check); usleep(data->time_to_eat * 1000); - philo->nb_eat++; pthread_mutex_unlock(philo->left_fork); pthread_mutex_unlock(philo->right_fork); + pthread_mutex_lock(&philo->check_nb_eat); + philo->nb_eat++; + pthread_mutex_unlock(&philo->check_nb_eat); +} + +int check_death(int i, t_philo *philo, t_arg *data, int *death) +{ + pthread_mutex_lock(&philo[i].eat_check); + if ((get_time() - philo[i].last_eat) > data->time_to_die) + { + pthread_mutex_unlock(&philo[i].eat_check); + put_event(data, philo[i].philo_id, "is died"); + pthread_mutex_lock(&data->life_check); + data->death = 0; + pthread_mutex_unlock(&data->life_check); + *death = 0; + printf("End: one philo died\n"); + } + else + pthread_mutex_unlock(&philo[i].eat_check); + usleep(100); + if (!(*death) || data->must_eat == -1) + return (0); + return (is_philo_full(philo, data)); } void death_checker(t_philo *philo, t_arg *data) { int i; + int nb_full; int death; death = 1; while (!data->full_philo && death) { i = -1; - while (++i < data->nb_philo - 1 && death) + while (++i < data->nb_philo && death) { - pthread_mutex_lock(&philo[i].eat_check); - if ((get_time() - philo[i].last_eat) > data->time_to_die) + nb_full = check_death(i, philo, data, &death); + if (nb_full == data->nb_philo) { - put_event(data, philo[i].philo_id, "is died"); pthread_mutex_lock(&data->life_check); data->death = 0; - pthread_mutex_unlock(&data->life_check); death = 0; - printf("End: one philo died\n"); - printf("time_to_die: %d TIME: %ld start: %ld philo.start: %ld last_eat: %ld condition: %ld\n", data->time_to_die ,get_time(), data->time_start , philo[i].data->time_start, get_time() - philo[i].last_eat ,get_time() - philo[i].last_eat); - } - pthread_mutex_unlock(&philo[i].eat_check); - usleep(100); - if (!death) - break ; - i = 0; - while (data->must_eat != -1 && i < data->nb_philo - && philo[i].nb_eat >= data->must_eat) - i++; - if (i == data->nb_philo) - { - pthread_mutex_lock(&data->life_check); - data->death = 0; data->full_philo = 1; pthread_mutex_unlock(&data->life_check); printf("End: all philo are full\n"); } } } + printf("return death\n"); } void *routine(void *tmp) @@ -110,14 +118,13 @@ int start_philo(t_philo *philo, t_arg *data) i++; } death_checker(philo, data); - //printf(" after d_checker\n"); i = 0; while (i < data->nb_philo) { - //printf("i = %d philo %d\n", i, philo[i].philo_id); + printf("start join\n"); pthread_join(philo[i++].thread, 0); + printf("i de join= %d\n", i); } -// printf("after join\n"); exit_philo(philo, data); return (0); } diff --git a/srcs/philosophers.h b/srcs/philosophers.h index 0a00abe..96581f6 100644 --- a/srcs/philosophers.h +++ b/srcs/philosophers.h @@ -40,6 +40,7 @@ typedef struct t_list t_arg *data; int nb_eat; long last_eat; + pthread_mutex_t check_nb_eat; pthread_mutex_t eat_check; pthread_mutex_t *right_fork; pthread_mutex_t *left_fork; @@ -52,6 +53,7 @@ void put_event(t_arg *data, int philo_id, char *event); t_philo *create_philo(t_arg *data); t_arg *create_data(int argc, char **argv); void exit_philo(t_philo *philo, t_arg *data); +int is_philo_full(t_philo *philo, t_arg *data); //philo.c int check_arg(int argc, char **argv); diff --git a/srcs/utils.c b/srcs/utils.c index 165004b..d0b12a5 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -77,5 +77,26 @@ int check_arg(int argc, char **argv) } i++; } + if (argv[1][0] == '0' || (argv[1][0] == '1' && !argv[1][1])) + return (1); + if (argc == 6 && argv[5][0] == '0') + return (1); return (0); } + +int is_philo_full(t_philo *philo, t_arg *data) +{ + int nb_full; + int i; + + nb_full = 0; + i = -1; + while (++i < data->nb_philo) + { + pthread_mutex_lock(&philo[i].check_nb_eat); + if (philo[i].nb_eat >= data->must_eat) + nb_full++; + pthread_mutex_unlock(&philo[i].check_nb_eat); + } + return (nb_full); +}