1 philo ok, deplacement diff_time dans generic, renomme go_sleep, ferme lock en fin
This commit is contained in:
11
srcs/exec.c
11
srcs/exec.c
@@ -1,13 +1,12 @@
|
||||
#include "philo.h"
|
||||
|
||||
void go_sleep(t_philo *philo, int action_time)
|
||||
void action_delay(t_philo *philo, int action_time)
|
||||
{
|
||||
struct timeval stime;
|
||||
int time_to_death;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
time_to_death = diff_time(philo->t_last_meal, stime);
|
||||
time_to_death = philo->params->t_die - time_to_death;
|
||||
time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime);
|
||||
if (time_to_death > action_time)
|
||||
usleep(action_time * 1000);
|
||||
else if (time_to_death > 0)
|
||||
@@ -30,15 +29,13 @@ int eat(t_philo *philo)
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
philo->eat++;
|
||||
if (philo->eat == philo->params->n_eat)
|
||||
philo->global->n_eat++;
|
||||
go_sleep(philo, philo->params->t_eat);
|
||||
|
||||
action_delay(philo, philo->params->t_eat);
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
return (0);
|
||||
@@ -58,7 +55,7 @@ void *philo_exec(void *arg)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping"))
|
||||
break ;
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
action_delay(philo, philo->params->t_slp);
|
||||
if (print_message(philo, B_GREEN, "is thinking"))
|
||||
break ;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user