introduction mutex mais erreur
This commit is contained in:
@@ -14,6 +14,7 @@ typedef struct s_philo
|
|||||||
{
|
{
|
||||||
t_conditions *conditions;
|
t_conditions *conditions;
|
||||||
int philo_nbr;
|
int philo_nbr;
|
||||||
|
int fork;
|
||||||
struct s_philo *prev;
|
struct s_philo *prev;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|||||||
37
srcs/exec.c
37
srcs/exec.c
@@ -8,12 +8,39 @@ void *philo_exec(void *arg)
|
|||||||
philo = (t_philo*)arg;
|
philo = (t_philo*)arg;
|
||||||
nbr = philo->philo_nbr;
|
nbr = philo->philo_nbr;
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
// eat
|
||||||
write(1, "-", 1);
|
// "has taken a fork"
|
||||||
ft_putnbr_fd(nbr, 1);
|
// "is eating"
|
||||||
write(1, "\n", 1);
|
// sleep
|
||||||
pthread_mutex_unlock(&mutex);
|
// "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);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ t_philo *init_struc_philo(t_philo *new, t_conditions *conditions, int i)
|
|||||||
{
|
{
|
||||||
new->conditions = conditions;
|
new->conditions = conditions;
|
||||||
new->philo_nbr = i + 1;
|
new->philo_nbr = i + 1;
|
||||||
|
new->fork = 1;
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user