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

@@ -11,8 +11,8 @@ void *philo_exec(void *arg);
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 diff_time(t_time old, struct timeval stime); int diff_time(t_time old, struct timeval stime);
int is_dead(t_philo *philo); int is_dead(t_philo *philo, struct timeval stime);
void print_message(t_philo *philo, char *msg); int print_message(t_philo *philo, char *clr, char *msg);
#endif #endif

View File

@@ -5,51 +5,6 @@ void go_sleep(t_philo *philo, int action_time)
struct timeval stime; struct timeval stime;
int time_to_death; 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); gettimeofday(&stime, NULL);
time_to_death = diff_time(philo->t_last_meal, stime); 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 - time_to_death;
@@ -59,30 +14,27 @@ void go_sleep(t_philo *philo, int action_time)
usleep(philo->params->t_die * 1000); 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); pthread_mutex_unlock(&philo->m_fork);
if (nbr_fork == 2) if (nbr_fork == 2)
pthread_mutex_unlock(&philo->next->m_fork); 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)); pthread_mutex_lock(&(philo->m_fork));
if (is_dead(philo)) if (print_message(philo, WHITE, "has taken a fork") == 1)
return (ret_unlock(philo, 1, 1)); return (ret_err_unlock(philo, 1));
print_message(philo, "has taken a fork");
pthread_mutex_lock(&(philo->next->m_fork)); pthread_mutex_lock(&(philo->next->m_fork));
if (is_dead(philo)) if (print_message(philo, WHITE, "has taken a fork") == 1)
return (ret_unlock(philo, 2, 1)); return (ret_err_unlock(philo, 2));
print_message(philo, "has taken a fork");
update_time(philo); 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++; 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); go_sleep(philo, philo->params->t_eat);
pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->next->m_fork));
@@ -100,15 +52,13 @@ void *philo_exec(void *arg)
usleep(10 * 1000); usleep(10 * 1000);
while (1) while (1)
{ {
if (take_forks(philo) != 0) if (eat(philo) != 0)
break ; break ;
if (is_dead(philo)) if (print_message(philo, B_BLUE, "is sleeping") == 1)
break ; break ;
print_message(philo, "is sleeping");
go_sleep(philo, philo->params->t_slp); go_sleep(philo, philo->params->t_slp);
if (is_dead(philo)) if (print_message(philo, B_GREEN, "is thinking") == 1)
break ; break ;
print_message(philo, "is thinking");
} }
return (NULL); return (NULL);
} }

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); 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; int time;
if (philo->global->dead) if (philo->global->dead)
return (1); return (-1);
gettimeofday(&stime, NULL); if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat)
return (-1);
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)
{ {
philo->global->dead = 1; philo->global->dead = 1;
print_message(philo, "died"); return (philo->p_nbr);
return (1);
} }
return (0); return (0);
} }
void print_message(t_philo *philo, char *msg) int print_message(t_philo *philo, char *clr, char *msg)
{ {
long int time_stamp; long int time_stamp;
struct timeval stime; struct timeval stime;
char *color; char p_dead;
pthread_mutex_lock(&(philo->global->m_print));
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
p_dead = is_dead(philo, stime);
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;
color = WHITE; if (p_dead != 0)
if (ft_strnstr(msg, "eating", ft_strlen(msg))) {
color = B_YELLOW; if (p_dead > 0)
else if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET);
color = B_BLUE; return (1);
else if (ft_strnstr(msg, "thinking", ft_strlen(msg))) }
color = B_GREEN; else
pthread_mutex_lock(&(philo->global->m_print)); ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print)); pthread_mutex_unlock(&(philo->global->m_print));
return (0);
} }

View File

@@ -55,6 +55,7 @@ t_params *init_params(int ac, char **av)
params->t_die = ft_atoi(av[2]); params->t_die = ft_atoi(av[2]);
params->t_eat = ft_atoi(av[3]); params->t_eat = ft_atoi(av[3]);
params->t_slp = ft_atoi(av[4]); params->t_slp = ft_atoi(av[4]);
params->n_eat = -1;
if (ac == 6) if (ac == 6)
params->n_eat = ft_atoi(av[5]); params->n_eat = ft_atoi(av[5]);
} }

View File

@@ -13,32 +13,21 @@ void launch_threads(t_philo *philo, pthread_t *id)
} }
} }
void watch_threads(t_philo *philo, pthread_t *id)
{
int i;
i = 0;
while (1)
{
if (is_dead(philo) == 1)
break ;
}
while (i < philo->params->n_phi)
{
pthread_join(id[i], NULL);
i++;
}
}
int main(int ac, char **av) int main(int ac, char **av)
{ {
pthread_t *id; pthread_t *id;
t_philo *philo; t_philo *philo;
int i;
philo = init(ac, av, &id); philo = init(ac, av, &id);
if (philo == NULL) if (philo == NULL)
return (0); return (0);
launch_threads(philo, id); launch_threads(philo, id);
watch_threads(philo, id); i = 0;
while (i < philo->params->n_phi)
{
pthread_join(id[i], NULL);
i++;
}
return (0); return (0);
} }