Adding stuff corrected thx to lgrafi forhelping me
This commit is contained in:
parent
8cc0fbda87
commit
124064556e
4 changed files with 25 additions and 13 deletions
|
|
@ -49,7 +49,7 @@ typedef struct s_philo
|
||||||
t_i32 eating_count;
|
t_i32 eating_count;
|
||||||
t_usize t_last_eat;
|
t_usize t_last_eat;
|
||||||
t_usize start_time;
|
t_usize start_time;
|
||||||
t_i32 *dead_check;
|
t_bool dead_check;
|
||||||
t_mutex *check_eating_count;
|
t_mutex *check_eating_count;
|
||||||
t_mutex *r_fork;
|
t_mutex *r_fork;
|
||||||
t_mutex *l_fork;
|
t_mutex *l_fork;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ t_error ft_start_eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
const t_usize time = ft_time() - philo->start_time;
|
const t_usize time = ft_time() - philo->start_time;
|
||||||
|
|
||||||
printf("\n\nOn as au pheno ?\n\n");
|
|
||||||
if (philo->id % 2 == 0)
|
if (philo->id % 2 == 0)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->l_fork);
|
pthread_mutex_lock(philo->l_fork);
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ t_usize ft_time(void)
|
||||||
|
|
||||||
if (gettimeofday(&time, NULL) == -1)
|
if (gettimeofday(&time, NULL) == -1)
|
||||||
ft_exit("Error of during ft_time !\n", 1);
|
ft_exit("Error of during ft_time !\n", 1);
|
||||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
return (time.tv_sec * 1000 + time.tv_usec * 0.001);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_pause(size_t milliseconds)
|
void ft_pause(size_t milliseconds)
|
||||||
|
|
@ -76,11 +76,11 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
|
||||||
prog->philos[i].eating = 0;
|
prog->philos[i].eating = 0;
|
||||||
ft_parsing_args(argc, argv, &prog->philos[i]);
|
ft_parsing_args(argc, argv, &prog->philos[i]);
|
||||||
prog->philos[i].start_time = ft_time();
|
prog->philos[i].start_time = ft_time();
|
||||||
prog->philos[i].t_last_eat = ft_time();
|
prog->philos[i].t_last_eat = 0;
|
||||||
prog->philos[i].print_lock = &prog->print_lock;
|
prog->philos[i].print_lock = &prog->print_lock;
|
||||||
prog->philos[i].dead_lock = &prog->dead_lock;
|
prog->philos[i].dead_lock = &prog->dead_lock;
|
||||||
prog->philos[i].meal_lock = &prog->meal_lock;
|
prog->philos[i].meal_lock = &prog->meal_lock;
|
||||||
prog->philos[i].dead_check = &prog->dead_flag;
|
prog->philos[i].dead_check = false;
|
||||||
prog->philos[i].check_eating_count = &prog->check_eating_count;
|
prog->philos[i].check_eating_count = &prog->check_eating_count;
|
||||||
prog->philos[i].l_fork = &forks[i];
|
prog->philos[i].l_fork = &forks[i];
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
t_bool dead_loop(t_philo *philo)
|
t_bool dead_loop(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->dead_lock);
|
pthread_mutex_lock(philo->dead_lock);
|
||||||
if (*philo->dead_check == true)
|
if (philo[0].dead_check == true)
|
||||||
return (pthread_mutex_unlock(philo->dead_lock), true);
|
return (pthread_mutex_unlock(philo->dead_lock), true);
|
||||||
pthread_mutex_unlock(philo->dead_lock);
|
pthread_mutex_unlock(philo->dead_lock);
|
||||||
return (false);
|
return (false);
|
||||||
|
|
@ -24,12 +24,17 @@ t_bool dead_loop(t_philo *philo)
|
||||||
t_bool check_dead(t_philo *philo)
|
t_bool check_dead(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
t_usize timecheck;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < philo[0].nb_philo)
|
while (i < philo[0].nb_philo)
|
||||||
{
|
{
|
||||||
if (ft_time() - philo[i].t_last_eat > philo[i].t_eat)
|
timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat;
|
||||||
|
if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death)
|
||||||
|
{
|
||||||
|
ft_logs("died\n", &philo[i]);
|
||||||
return (true);
|
return (true);
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (false);
|
return (false);
|
||||||
|
|
@ -47,14 +52,23 @@ t_bool check_eat(t_philo *philo)
|
||||||
while (i < philo[0].nb_philo)
|
while (i < philo[0].nb_philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo[i].check_eating_count);
|
pthread_mutex_lock(philo[i].check_eating_count);
|
||||||
printf("\n\n\n eating_time for philo n%d = %d\n\n\n", philo[i].id, philo[i].eating_count);
|
|
||||||
if (philo[i].eating_count >= philo[i].nb_eat)
|
if (philo[i].eating_count >= philo[i].nb_eat)
|
||||||
check++;
|
check++;
|
||||||
pthread_mutex_unlock(philo[i].check_eating_count);
|
pthread_mutex_unlock(philo[i].check_eating_count);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
i = 0;
|
||||||
if (check == philo[0].nb_philo)
|
if (check == philo[0].nb_philo)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(philo[0].dead_lock);
|
||||||
|
while (i < philo[0].nb_philo)
|
||||||
|
{
|
||||||
|
philo[i].dead_check = true;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(philo[0].dead_lock);
|
||||||
return (true);
|
return (true);
|
||||||
|
}
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,10 +81,10 @@ void *ft_watch_dogs(void *ptr)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (check_dead(philo) == true)
|
|
||||||
break ;
|
|
||||||
if (check_eat(philo) == true)
|
if (check_eat(philo) == true)
|
||||||
break ;
|
break ;
|
||||||
|
if (check_dead(philo) == true)
|
||||||
|
break ;
|
||||||
}
|
}
|
||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +111,6 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
ft_logs("garfi lpb\n", prog->philos);
|
|
||||||
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
|
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
|
||||||
ft_destroy_exit("Allocation of watch_dogs failed\n", \
|
ft_destroy_exit("Allocation of watch_dogs failed\n", \
|
||||||
ERROR, prog, forks);
|
ERROR, prog, forks);
|
||||||
|
|
@ -109,8 +122,8 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
||||||
ERROR, prog, forks);
|
ERROR, prog, forks);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
pthread_join(o_block, NULL);
|
|
||||||
while (--i > 0)
|
while (--i > 0)
|
||||||
pthread_join(prog->philos[i].thread, NULL);
|
pthread_join(prog->philos[i].thread, NULL);
|
||||||
|
pthread_join(o_block, NULL);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue