fonctionne ok sauf pour 1
This commit is contained in:
@@ -15,7 +15,7 @@ void *philo_exec(void *arg);
|
||||
// generic.c
|
||||
void init_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
|
||||
|
||||
|
||||
12
srcs/exec.c
12
srcs/exec.c
@@ -25,14 +25,14 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
|
||||
int eat(t_philo *philo)
|
||||
{
|
||||
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));
|
||||
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));
|
||||
|
||||
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));
|
||||
philo->eat++;
|
||||
if (philo->eat == philo->params->n_eat)
|
||||
@@ -51,15 +51,15 @@ void *philo_exec(void *arg)
|
||||
philo = (t_philo*)arg;
|
||||
init_time(philo);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10);
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
if (eat(philo) != 0)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead))
|
||||
if (print_message(philo, B_BLUE, "is sleeping"))
|
||||
break ;
|
||||
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 ;
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
@@ -21,20 +21,21 @@ void update_time(t_philo *philo)
|
||||
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;
|
||||
struct timeval stime;
|
||||
|
||||
if (key)
|
||||
return (1);
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
if (philo->global->dead)
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
gettimeofday(&stime, NULL);
|
||||
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 %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
if (philo->global->dead)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ int diff_time(t_time old, struct timeval new)
|
||||
int is_dead(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
long int time_stamp;
|
||||
int time;
|
||||
|
||||
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);
|
||||
if (time >= philo->params->t_die)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
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 (0);
|
||||
|
||||
Reference in New Issue
Block a user