erreur lors des arrets
This commit is contained in:
74
srcs/exec.c
74
srcs/exec.c
@@ -5,51 +5,6 @@ void go_sleep(t_philo *philo, int action_time)
|
||||
struct timeval stime;
|
||||
int time_to_death;
|
||||
|
||||
// now_s
|
||||
// now_u
|
||||
// last_meal_s
|
||||
// last_meal_u
|
||||
//
|
||||
// last_meal now
|
||||
// v v
|
||||
// ------|-----------|------------------------------------------
|
||||
// | |
|
||||
// |_d_e_a_t_h__t_i_m_e__________________|
|
||||
// |
|
||||
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||
// |
|
||||
// |_d_i_f_f___|
|
||||
// |
|
||||
// |_d_e_a_t_h_-_d_i_f_f_____|->time_to_death
|
||||
//
|
||||
//
|
||||
// last_meal now
|
||||
// v v
|
||||
// ------|-----------|------------------------------------------
|
||||
// |
|
||||
// |_d_e_a_t_h__t_i_m_e_______________________________|
|
||||
// |
|
||||
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||
// |
|
||||
// |_d_i_f_f___|
|
||||
// |
|
||||
// |_d_e_a_t_h_-_d_i_f_f__________________|->time_to_death
|
||||
//
|
||||
//
|
||||
// last_meal now
|
||||
// v v
|
||||
// ------|-----------|------------------------------------------
|
||||
// |
|
||||
// |_death___|
|
||||
// |
|
||||
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||
// |
|
||||
// |_d_i_f_f___|
|
||||
// |
|
||||
// |_|->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;
|
||||
@@ -59,30 +14,27 @@ void go_sleep(t_philo *philo, int action_time)
|
||||
usleep(philo->params->t_die * 1000);
|
||||
}
|
||||
|
||||
int ret_unlock(t_philo *philo, int nbr_fork, int ret)
|
||||
int ret_err_unlock(t_philo *philo, int nbr_fork)
|
||||
{
|
||||
pthread_mutex_unlock(&philo->m_fork);
|
||||
if (nbr_fork == 2)
|
||||
pthread_mutex_unlock(&philo->next->m_fork);
|
||||
return (ret);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int take_forks(t_philo *philo)
|
||||
int eat(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (is_dead(philo))
|
||||
return (ret_unlock(philo, 1, 1));
|
||||
print_message(philo, "has taken a fork");
|
||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
||||
return (ret_err_unlock(philo, 1));
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (is_dead(philo))
|
||||
return (ret_unlock(philo, 2, 1));
|
||||
print_message(philo, "has taken a fork");
|
||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
||||
return (ret_err_unlock(philo, 2));
|
||||
|
||||
update_time(philo);
|
||||
print_message(philo, "is eating");
|
||||
if (print_message(philo, B_YELLOW, "is eating") == 1)
|
||||
return (ret_err_unlock(philo, 2));
|
||||
philo->eat++;
|
||||
if (philo->params->n_eat && philo->eat >= philo->params->n_eat)
|
||||
return (ret_unlock(philo, 2, 1));
|
||||
go_sleep(philo, philo->params->t_eat);
|
||||
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
@@ -100,15 +52,13 @@ void *philo_exec(void *arg)
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
if (take_forks(philo) != 0)
|
||||
if (eat(philo) != 0)
|
||||
break ;
|
||||
if (is_dead(philo))
|
||||
if (print_message(philo, B_BLUE, "is sleeping") == 1)
|
||||
break ;
|
||||
print_message(philo, "is sleeping");
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
if (is_dead(philo))
|
||||
if (print_message(philo, B_GREEN, "is thinking") == 1)
|
||||
break ;
|
||||
print_message(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user