no datarace

This commit is contained in:
kinou-p 2022-03-18 03:34:44 +01:00
parent 0fccce3acc
commit 5fdc221dd1
5 changed files with 66 additions and 33 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/15 17:40:53 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); usleep(100000);
while (i < data->nb_philo - 1) 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); pthread_mutex_destroy(philo[i].left_fork);
free(philo[i].left_fork); free(philo[i].left_fork);
i++; i++;
@ -28,6 +30,7 @@ void exit_philo(t_philo *philo, t_arg *data)
pthread_mutex_destroy(philo[i].left_fork); pthread_mutex_destroy(philo[i].left_fork);
free(philo[i].left_fork); free(philo[i].left_fork);
pthread_mutex_destroy(data->display); pthread_mutex_destroy(data->display);
pthread_mutex_destroy(&data->life_check);
free(data->display); free(data->display);
free(philo); free(philo);
free(data); free(data);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/15 17:38:58 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; pthread_mutex_t *display;
display = malloc(sizeof(pthread_mutex_t)); display = malloc(sizeof(pthread_mutex_t));
if (!display) data = malloc(sizeof(t_arg));
if (!display || !data)
return (0); return (0);
pthread_mutex_init(display, 0); pthread_mutex_init(display, 0);
data = malloc(sizeof(t_arg));
pthread_mutex_init(&data->life_check, 0); pthread_mutex_init(&data->life_check, 0);
data->display = display; data->display = display;
data->death = 1; data->death = 1;
@ -54,15 +54,15 @@ t_philo *create_philo(t_arg *data)
philo[i].data = data; philo[i].data = data;
philo[i].nb_eat = 0; philo[i].nb_eat = 0;
philo[i].left_fork = malloc(sizeof(pthread_mutex_t)); 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].left_fork, 0);
pthread_mutex_init(&philo[i].eat_check, 0); pthread_mutex_init(&philo[i].eat_check, 0);
pthread_mutex_init(&philo[i].check_nb_eat, 0);
i++; i++;
} }
i = 0; i = -1;
while (i < data->nb_philo) while (++i < data->nb_philo)
{
philo[i].right_fork = philo[(i + 1) % data->nb_philo].left_fork; philo[i].right_fork = philo[(i + 1) % data->nb_philo].left_fork;
i++;
}
return (philo); return (philo);
} }

View File

@ -26,51 +26,59 @@ void philo_eat(t_philo *philo)
philo->last_eat = get_time(); philo->last_eat = get_time();
pthread_mutex_unlock(&philo->eat_check); pthread_mutex_unlock(&philo->eat_check);
usleep(data->time_to_eat * 1000); usleep(data->time_to_eat * 1000);
philo->nb_eat++;
pthread_mutex_unlock(philo->left_fork); pthread_mutex_unlock(philo->left_fork);
pthread_mutex_unlock(philo->right_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) void death_checker(t_philo *philo, t_arg *data)
{ {
int i; int i;
int nb_full;
int death; int death;
death = 1; death = 1;
while (!data->full_philo && death) while (!data->full_philo && death)
{ {
i = -1; i = -1;
while (++i < data->nb_philo - 1 && death) while (++i < data->nb_philo && death)
{ {
pthread_mutex_lock(&philo[i].eat_check); nb_full = check_death(i, philo, data, &death);
if ((get_time() - philo[i].last_eat) > data->time_to_die) if (nb_full == data->nb_philo)
{ {
put_event(data, philo[i].philo_id, "is died");
pthread_mutex_lock(&data->life_check); pthread_mutex_lock(&data->life_check);
data->death = 0; data->death = 0;
pthread_mutex_unlock(&data->life_check);
death = 0; 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; data->full_philo = 1;
pthread_mutex_unlock(&data->life_check); pthread_mutex_unlock(&data->life_check);
printf("End: all philo are full\n"); printf("End: all philo are full\n");
} }
} }
} }
printf("return death\n");
} }
void *routine(void *tmp) void *routine(void *tmp)
@ -110,14 +118,13 @@ int start_philo(t_philo *philo, t_arg *data)
i++; i++;
} }
death_checker(philo, data); death_checker(philo, data);
//printf(" after d_checker\n");
i = 0; i = 0;
while (i < data->nb_philo) 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); pthread_join(philo[i++].thread, 0);
printf("i de join= %d\n", i);
} }
// printf("after join\n");
exit_philo(philo, data); exit_philo(philo, data);
return (0); return (0);
} }

View File

@ -40,6 +40,7 @@ typedef struct t_list
t_arg *data; t_arg *data;
int nb_eat; int nb_eat;
long last_eat; long last_eat;
pthread_mutex_t check_nb_eat;
pthread_mutex_t eat_check; pthread_mutex_t eat_check;
pthread_mutex_t *right_fork; pthread_mutex_t *right_fork;
pthread_mutex_t *left_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_philo *create_philo(t_arg *data);
t_arg *create_data(int argc, char **argv); t_arg *create_data(int argc, char **argv);
void exit_philo(t_philo *philo, t_arg *data); void exit_philo(t_philo *philo, t_arg *data);
int is_philo_full(t_philo *philo, t_arg *data);
//philo.c //philo.c
int check_arg(int argc, char **argv); int check_arg(int argc, char **argv);

View File

@ -77,5 +77,26 @@ int check_arg(int argc, char **argv)
} }
i++; 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); 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);
}