WIP geen idee

This commit is contained in:
whaffman 2025-01-24 16:43:46 +01:00
parent dee0a058e4
commit eb85c4c385
3 changed files with 36 additions and 16 deletions

View File

@ -55,6 +55,7 @@ typedef struct s_philo
pthread_mutex_t *l_fork; pthread_mutex_t *l_fork;
pthread_mutex_t *r_fork; pthread_mutex_t *r_fork;
int death; int death;
pthread_mutex_t *death_lock;
int last_meal; int last_meal;
t_rules *rules; t_rules *rules;
int n_eat; int n_eat;

View File

@ -31,6 +31,23 @@ int get_finished(t_rules *rules)
return (finished); 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) void philo_die(t_philo *philo)
{ {
print_status(philo, "died"); print_status(philo, "died");
@ -40,17 +57,17 @@ void philo_die(t_philo *philo)
pthread_mutex_unlock(philo->r_fork); 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(); const int start = get_time();
while (get_time() - start < time) while (get_time() - start < time)
{ {
if (philo->last_meal - get_time() >= philo->rules->time_to_die) if (philo->death)
return (SUCCESS);
usleep(500);
}
return (FAILURE); return (FAILURE);
usleep(50);
}
return (SUCCESS);
} }
int philo_eat(t_philo *philo) int philo_eat(t_philo *philo)
{ {
@ -66,8 +83,8 @@ int philo_eat(t_philo *philo)
pthread_mutex_lock(philo->r_fork); pthread_mutex_lock(philo->r_fork);
print_status(philo, "has taken a fork"); print_status(philo, "has taken a fork");
} }
if (check_death(philo)) if (philo->death)
return (philo_die(philo), FAILURE); return (FAILURE);
print_status(philo, "is eating"); print_status(philo, "is eating");
philo->last_meal = get_time(); philo->last_meal = get_time();
philo->n_eat++; philo->n_eat++;
@ -93,19 +110,19 @@ void *philo_routine(void *arg)
synchronize_philos(philo); synchronize_philos(philo);
while (!philo->death && !get_finished(philo->rules)) while (!philo->death && !get_finished(philo->rules))
{ {
if (check_death(philo)) if (philo->death)
return (philo_die(philo), NULL); break ;
if (!philo_eat(philo)) if (!philo_eat(philo))
return (NULL); break ;
if (philo_died_while_sleeping(philo, philo->rules->time_to_eat)) if (!philo_sleep(philo, philo->rules->time_to_eat))
return (philo_die(philo), NULL); break ;
pthread_mutex_unlock(philo->l_fork); pthread_mutex_unlock(philo->l_fork);
pthread_mutex_unlock(philo->r_fork); pthread_mutex_unlock(philo->r_fork);
if (!philo->rules->finished) if (!philo->death)
print_status(philo, "is sleeping"); print_status(philo, "is sleeping");
if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep)) if (!philo_sleep(philo, philo->rules->time_to_sleep))
return (philo_die(philo), NULL); break;
if(!philo->rules->finished) if(!philo->death)
print_status(philo, "is thinking"); print_status(philo, "is thinking");
} }
return (NULL); return (NULL);

View File

@ -14,6 +14,8 @@
void print_status(t_philo *philo, char *status) void print_status(t_philo *philo, char *status)
{ {
if (philo->death)
return ;
pthread_mutex_lock(philo->rules->print_lock); pthread_mutex_lock(philo->rules->print_lock);
printf("%d %d %s\n", printf("%d %d %s\n",
get_time() - philo->rules->start_time, get_time() - philo->rules->start_time,