WIP geen idee
This commit is contained in:
parent
dee0a058e4
commit
eb85c4c385
@ -55,6 +55,7 @@ typedef struct s_philo
|
||||
pthread_mutex_t *l_fork;
|
||||
pthread_mutex_t *r_fork;
|
||||
int death;
|
||||
pthread_mutex_t *death_lock;
|
||||
int last_meal;
|
||||
t_rules *rules;
|
||||
int n_eat;
|
||||
|
||||
@ -31,6 +31,23 @@ int get_finished(t_rules *rules)
|
||||
return (finished);
|
||||
}
|
||||
|
||||
void set_death(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(philo->death_lock);
|
||||
philo->death = 1;
|
||||
pthread_mutex_unlock(philo->death_lock);
|
||||
}
|
||||
|
||||
int get_death(t_philo *philo)
|
||||
{
|
||||
int death;
|
||||
|
||||
pthread_mutex_lock(philo->death_lock);
|
||||
death = philo->death;
|
||||
pthread_mutex_unlock(philo->death_lock);
|
||||
return (death);
|
||||
}
|
||||
|
||||
void philo_die(t_philo *philo)
|
||||
{
|
||||
print_status(philo, "died");
|
||||
@ -40,17 +57,17 @@ void philo_die(t_philo *philo)
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
}
|
||||
|
||||
int philo_died_while_sleeping(t_philo *philo, int time)
|
||||
int philo_sleep(t_philo *philo, int time)
|
||||
{
|
||||
const int start = get_time();
|
||||
|
||||
while (get_time() - start < time)
|
||||
{
|
||||
if (philo->last_meal - get_time() >= philo->rules->time_to_die)
|
||||
return (SUCCESS);
|
||||
usleep(500);
|
||||
}
|
||||
if (philo->death)
|
||||
return (FAILURE);
|
||||
usleep(50);
|
||||
}
|
||||
return (SUCCESS);
|
||||
}
|
||||
int philo_eat(t_philo *philo)
|
||||
{
|
||||
@ -66,8 +83,8 @@ int philo_eat(t_philo *philo)
|
||||
pthread_mutex_lock(philo->r_fork);
|
||||
print_status(philo, "has taken a fork");
|
||||
}
|
||||
if (check_death(philo))
|
||||
return (philo_die(philo), FAILURE);
|
||||
if (philo->death)
|
||||
return (FAILURE);
|
||||
print_status(philo, "is eating");
|
||||
philo->last_meal = get_time();
|
||||
philo->n_eat++;
|
||||
@ -93,19 +110,19 @@ void *philo_routine(void *arg)
|
||||
synchronize_philos(philo);
|
||||
while (!philo->death && !get_finished(philo->rules))
|
||||
{
|
||||
if (check_death(philo))
|
||||
return (philo_die(philo), NULL);
|
||||
if (philo->death)
|
||||
break ;
|
||||
if (!philo_eat(philo))
|
||||
return (NULL);
|
||||
if (philo_died_while_sleeping(philo, philo->rules->time_to_eat))
|
||||
return (philo_die(philo), NULL);
|
||||
break ;
|
||||
if (!philo_sleep(philo, philo->rules->time_to_eat))
|
||||
break ;
|
||||
pthread_mutex_unlock(philo->l_fork);
|
||||
pthread_mutex_unlock(philo->r_fork);
|
||||
if (!philo->rules->finished)
|
||||
if (!philo->death)
|
||||
print_status(philo, "is sleeping");
|
||||
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep))
|
||||
return (philo_die(philo), NULL);
|
||||
if(!philo->rules->finished)
|
||||
if (!philo_sleep(philo, philo->rules->time_to_sleep))
|
||||
break;
|
||||
if(!philo->death)
|
||||
print_status(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
@ -14,6 +14,8 @@
|
||||
|
||||
void print_status(t_philo *philo, char *status)
|
||||
{
|
||||
if (philo->death)
|
||||
return ;
|
||||
pthread_mutex_lock(philo->rules->print_lock);
|
||||
printf("%d %d %s\n",
|
||||
get_time() - philo->rules->start_time,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user