From 81f0ded5e38574f4ad3a4b3883973144fddf22b6 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 10 Jan 2022 15:44:30 +0100 Subject: [PATCH] introduction mutex mais erreur --- headers/philo_struct.h | 1 + srcs/exec.c | 37 ++++++++++++++++++++++++++++++++----- srcs/init.c | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 662eb5b..dc2c9cb 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -14,6 +14,7 @@ typedef struct s_philo { t_conditions *conditions; int philo_nbr; + int fork; struct s_philo *prev; struct s_philo *next; } t_philo; diff --git a/srcs/exec.c b/srcs/exec.c index acdaa15..f4a385d 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -8,12 +8,39 @@ void *philo_exec(void *arg) philo = (t_philo*)arg; nbr = philo->philo_nbr; - pthread_mutex_lock(&mutex); - write(1, "-", 1); - ft_putnbr_fd(nbr, 1); - write(1, "\n", 1); - pthread_mutex_unlock(&mutex); +// eat + // "has taken a fork" + // "is eating" +// sleep + // "is sleeping" +// think + // "is thinking" +// die + // "died" +int i = 0; + while (i < 2) + { + pthread_mutex_lock(&mutex); + if (philo->fork == 1 && philo->next->fork == 1) + { + philo->fork = 0; + philo->next->fork = 0; + ft_putnbr_fd(philo->philo_nbr, 1); + ft_putstr_fd(" has taken a fork\n", 1); + } + pthread_mutex_unlock(&mutex); + usleep(philo->conditions->t_eat); + + pthread_mutex_lock(&mutex); + philo->fork = 1; + philo->next->fork = 1; + ft_printf("%i is sleeping\n", philo->philo_nbr); + pthread_mutex_unlock(&mutex); + + usleep(philo->conditions->t_slp); +i++; + } return (NULL); } diff --git a/srcs/init.c b/srcs/init.c index a2512a6..6c26bbd 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -4,6 +4,7 @@ t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i) { new->conditions = conditions; new->philo_nbr = i + 1; + new->fork = 1; return (new); }