introduction mutex mais erreur

This commit is contained in:
hugogogo
2022-01-10 15:44:30 +01:00
parent b93e8c5e22
commit 81f0ded5e3
3 changed files with 34 additions and 5 deletions

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}