fonctionne ok sauf pour 1

This commit is contained in:
Hugo LAMY
2022-01-26 13:52:30 +01:00
parent 6561340fb9
commit 2f747d0563
4 changed files with 20 additions and 13 deletions

View File

@@ -15,7 +15,7 @@ void *philo_exec(void *arg);
// generic.c // generic.c
void init_time(t_philo *philo); void init_time(t_philo *philo);
void update_time(t_philo *philo); void update_time(t_philo *philo);
int print_message(t_philo *philo, char *clr, char *msg, int key); int print_message(t_philo *philo, char *clr, char *msg);
#endif #endif

View File

@@ -25,14 +25,14 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
int eat(t_philo *philo) int eat(t_philo *philo)
{ {
pthread_mutex_lock(&(philo->m_fork)); pthread_mutex_lock(&(philo->m_fork));
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead)) if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(philo, 1)); return (ret_err_unlock(philo, 1));
pthread_mutex_lock(&(philo->next->m_fork)); pthread_mutex_lock(&(philo->next->m_fork));
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead)) if (print_message(philo, WHITE, "has taken a fork"))
return (ret_err_unlock(philo, 2)); return (ret_err_unlock(philo, 2));
update_time(philo); update_time(philo);
if (print_message(philo, B_YELLOW, "is eating", philo->global->dead)) if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(philo, 2)); return (ret_err_unlock(philo, 2));
philo->eat++; philo->eat++;
if (philo->eat == philo->params->n_eat) if (philo->eat == philo->params->n_eat)
@@ -51,15 +51,15 @@ void *philo_exec(void *arg)
philo = (t_philo*)arg; philo = (t_philo*)arg;
init_time(philo); init_time(philo);
if (philo->p_nbr % 2 == 0) if (philo->p_nbr % 2 == 0)
usleep(10); usleep(10 * 1000);
while (1) while (1)
{ {
if (eat(philo) != 0) if (eat(philo) != 0)
break ; break ;
if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead)) if (print_message(philo, B_BLUE, "is sleeping"))
break ; break ;
go_sleep(philo, philo->params->t_slp); go_sleep(philo, philo->params->t_slp);
if (print_message(philo, B_GREEN, "is thinking", philo->global->dead)) if (print_message(philo, B_GREEN, "is thinking"))
break ; break ;
} }
return (NULL); return (NULL);

View File

@@ -21,20 +21,21 @@ void update_time(t_philo *philo)
philo->t_last_meal.tu = stime.tv_usec; philo->t_last_meal.tu = stime.tv_usec;
} }
int print_message(t_philo *philo, char *clr, char *msg, int key) int print_message(t_philo *philo, char *clr, char *msg)
{ {
long int time_stamp; long int time_stamp;
struct timeval stime; struct timeval stime;
if (key)
return (1);
pthread_mutex_lock(&(philo->global->m_print)); pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->dead)
{
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print)); pthread_mutex_unlock(&(philo->global->m_print));
if (philo->global->dead)
return (1);
return (0); return (0);
} }

View File

@@ -8,6 +8,7 @@ int diff_time(t_time old, struct timeval new)
int is_dead(t_philo *philo) int is_dead(t_philo *philo)
{ {
struct timeval stime; struct timeval stime;
long int time_stamp;
int time; int time;
if (philo->global->n_eat == philo->params->n_phi) if (philo->global->n_eat == philo->params->n_phi)
@@ -19,8 +20,13 @@ int is_dead(t_philo *philo)
time = diff_time(philo->t_last_meal, stime); time = diff_time(philo->t_last_meal, stime);
if (time >= philo->params->t_die) if (time >= philo->params->t_die)
{ {
pthread_mutex_lock(&(philo->global->m_print));
philo->global->dead = 1; philo->global->dead = 1;
print_message(philo, B_RED, "died", 0);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
ft_printf("%s%i %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (1); return (1);
} }
return (0); return (0);