erreur lors des arrets

This commit is contained in:
Hugo LAMY
2022-01-25 22:05:39 +01:00
parent d8475537e0
commit 475817540c
5 changed files with 40 additions and 99 deletions

View File

@@ -26,41 +26,42 @@ int diff_time(t_time old, struct timeval new)
return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000);
}
int is_dead(t_philo *philo)
int is_dead(t_philo *philo, struct timeval stime)
{
struct timeval stime;
int time;
if (philo->global->dead)
return (1);
gettimeofday(&stime, NULL);
return (-1);
if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat)
return (-1);
time = diff_time(philo->t_last_meal, stime);
if (time >= philo->params->t_die)
{
philo->global->dead = 1;
print_message(philo, "died");
return (1);
return (philo->p_nbr);
}
return (0);
}
void print_message(t_philo *philo, char *msg)
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
char *color;
char p_dead;
pthread_mutex_lock(&(philo->global->m_print));
gettimeofday(&stime, NULL);
p_dead = is_dead(philo, stime);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
color = WHITE;
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
color = B_YELLOW;
else if (ft_strnstr(msg, "sleeping", ft_strlen(msg)))
color = B_BLUE;
else if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
color = B_GREEN;
pthread_mutex_lock(&(philo->global->m_print));
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);
if (p_dead != 0)
{
if (p_dead > 0)
ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET);
return (1);
}
else
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
}