Correction et sécurisation du code en cas de crash et d'erreur de malloc
This commit is contained in:
parent
c2f741e949
commit
d1200f346b
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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]);
|
||||
|
|
|
@ -6,19 +6,21 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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,23 +32,10 @@ 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);
|
||||
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])
|
||||
{
|
||||
|
@ -57,4 +46,18 @@ void free_all(pthread_t *threads, t_same *same, t_philo **philo)
|
|||
}
|
||||
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)
|
||||
free(same);
|
||||
if (philo)
|
||||
free_philo(philo);
|
||||
return (1);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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++] = ' ';
|
||||
|
|
Loading…
Reference in a new issue