From d1200f346b44250402e503905fe9432dbc80cbb4 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Sat, 8 Apr 2023 16:15:26 +0200 Subject: [PATCH] =?UTF-8?q?Correction=20et=20s=C3=A9curisation=20du=20code?= =?UTF-8?q?=20en=20cas=20de=20crash=20et=20d'erreur=20de=20malloc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/main.c | 2 +- philo/manage_threads.c | 41 ++++++++++++++++++++-------------- philo/philo.h | 10 ++++----- philo/threads.c | 12 +++++----- philo/utils_manage_threads.c | 43 +++++++++++++++++++----------------- philo/utils_threads.c | 3 +-- 6 files changed, 61 insertions(+), 50 deletions(-) diff --git a/philo/main.c b/philo/main.c index 715da00..944d30b 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 12:32:25 by erey-bet ### ########.fr */ +/* Updated: 2023/04/07 14:41:17 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/manage_threads.c b/philo/manage_threads.c index 01b621f..4fdfd0e 100644 --- a/philo/manage_threads.c +++ b/philo/manage_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 13:54:03 by erey-bet ### ########.fr */ +/* Updated: 2023/04/08 16:07:07 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,17 +21,19 @@ static t_same *create_struct_same(t_config *config) return (NULL); same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 3); if (!same->mutex) + { + free(same); return (NULL); - init_mutex(same->mutex, config->nbr_philo + 3); + } + if (init_mutex(same->mutex, config->nbr_philo + 3)) + { + free(same->mutex); + free(same); + return (NULL); + } same->config = config; - same->death = ft_calloc(sizeof(int), 1); - if (!same->death) - return (NULL); - *same->death = 0; - same->all_eat = ft_calloc(sizeof(int), 1); - if (!same->all_eat) - return (NULL); - *same->all_eat = 0; + same->death = 0; + same->all_eat = 0; same->time = get_utime(); return (same); } @@ -52,15 +54,22 @@ static t_philo *init_philo(t_same *same, t_only *only) { t_philo *philo; + if (!same) + { + if (only) + free(only); + return (NULL); + } + if (!only) + { + free(same); + return (NULL); + } philo = ft_calloc(sizeof(t_philo), 1); if (!philo) return (NULL); philo->same = same; - if (!philo->same) - return (NULL); philo->only = only; - if (!philo->only) - return (NULL); return (philo); } @@ -76,7 +85,7 @@ int manage_threads(t_config *config) return (1); philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *)); if (philo == NULL) - return (1); + return (free_all(thds, NULL, NULL)); same = create_struct_same(config); i = -1; while (++i < config->nbr_philo) @@ -84,7 +93,7 @@ int manage_threads(t_config *config) philo[i] = init_philo(same, create_struct_only(i)); if (!philo[i] || pthread_create(&thds[i], NULL, (void *)&philosopher, philo[i]) != 0) - return (1); + return (free_all(thds, same, philo)); } philo[i] = NULL; while (--i > -1) diff --git a/philo/philo.h b/philo/philo.h index 6249365..4d80ec7 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 13:59:01 by erey-bet ### ########.fr */ +/* Updated: 2023/04/08 16:09:53 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,8 +35,8 @@ typedef struct s_same { t_config *config; pthread_mutex_t *mutex; - int *all_eat; - int *death; + int all_eat; + int death; long long time; } t_same; @@ -67,9 +67,9 @@ int verif_finish(t_philo *philo, t_only *only); int finish(t_philo *philo, long long last_eat_time); void message_die(t_philo *philo, char *msg); void message(t_philo *philo, char *msg); -void init_mutex(pthread_mutex_t *mutex, int nbr); +int init_mutex(pthread_mutex_t *mutex, int nbr); void destroy_mutex(pthread_mutex_t *mutex, int nbr); -void free_all(pthread_t *threads, t_same *same, t_philo **philo); +int free_all(pthread_t *threads, t_same *same, t_philo **philo); /* THREADS */ void *philosopher(t_philo *philo); diff --git a/philo/threads.c b/philo/threads.c index f9c6284..87ae739 100644 --- a/philo/threads.c +++ b/philo/threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 13:50:41 by erey-bet ### ########.fr */ +/* Updated: 2023/04/07 15:19:34 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,11 +22,11 @@ int finish(t_philo *philo, long long last_eat_time) if (philo->same->config->must_eat > -1) { pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]); - all_eat = *philo->same->all_eat; + all_eat = philo->same->all_eat; pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); } pthread_mutex_lock(&philo->same->mutex[nbr_philo]); - dead = *philo->same->death; + dead = philo->same->death; pthread_mutex_unlock(&philo->same->mutex[nbr_philo]); return (get_utime() - last_eat_time >= philo->same->config->time_die || dead @@ -66,7 +66,7 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only, { only->eat++; pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]); - *philo->same->all_eat += 1; + philo->same->all_eat += 1; pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); } message(philo, "is sleeping\n"); @@ -125,10 +125,10 @@ void *philosopher(t_philo *philo) loop_philosopher(philo, only, config, nbr_philo); pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_lock(&philo->same->mutex[nbr_philo]); - if (!*philo->same->death && (config->must_eat == -1 + if (!philo->same->death && (config->must_eat == -1 || (config->must_eat > -1 && only->eat < config->must_eat))) { - *philo->same->death = 1; + philo->same->death = 1; message_die(philo, "died\n"); } pthread_mutex_unlock(&philo->same->mutex[nbr_philo]); diff --git a/philo/utils_manage_threads.c b/philo/utils_manage_threads.c index d524a14..be29ccf 100644 --- a/philo/utils_manage_threads.c +++ b/philo/utils_manage_threads.c @@ -6,19 +6,21 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 13:57:40 by erey-bet ### ########.fr */ +/* Updated: 2023/04/08 16:10:05 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void init_mutex(pthread_mutex_t *mutex, int nbr) +int init_mutex(pthread_mutex_t *mutex, int nbr) { int i; i = -1; while (++i < nbr) - pthread_mutex_init(&mutex[i], NULL); + if (pthread_mutex_init(&mutex[i], NULL) != 0) + return (1); + return (0); } void destroy_mutex(pthread_mutex_t *mutex, int nbr) @@ -30,31 +32,32 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr) pthread_mutex_destroy(&mutex[i]); } -void free_all(pthread_t *threads, t_same *same, t_philo **philo) +static void free_philo(t_philo **philo) { int i; - destroy_mutex(same->mutex, same->config->nbr_philo + 3); + i = -1; + while (philo[++i]) + { + if (philo[i]->only) + free(philo[i]->only); + if (philo[i]) + free(philo[i]); + } + free(philo); +} + +int free_all(pthread_t *threads, t_same *same, t_philo **philo) +{ + if (same && same->mutex) + destroy_mutex(same->mutex, same->config->nbr_philo + 3); if (threads) free(threads); if (same && same->mutex) free(same->mutex); - if (same && same->mutex) - free(same->death); - if (same && same->mutex) - free(same->all_eat); if (same) free(same); if (philo) - { - i = -1; - while (philo[++i]) - { - if (philo[i]->only) - free(philo[i]->only); - if (philo[i]) - free(philo[i]); - } - free(philo); - } + free_philo(philo); + return (1); } diff --git a/philo/utils_threads.c b/philo/utils_threads.c index 6830fde..1d5b8b8 100644 --- a/philo/utils_threads.c +++ b/philo/utils_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */ -/* Updated: 2023/04/06 13:59:13 by erey-bet ### ########.fr */ +/* Updated: 2023/04/08 16:05:23 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,6 @@ void message_die(t_philo *philo, char *msg) int i; config = philo->same->config; - pthread_mutex_lock(&philo->same->mutex[config->nbr_philo + 2]); i = ft_lltos(send, (get_utime() - philo->same->time) / 1000); send[i++] = ' ';