introduction mutex mais erreur
This commit is contained in:
@@ -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;
|
||||
|
||||
37
srcs/exec.c
37
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user