diff options
| author | yctct <yctct@yctct.com> | 2026-09-13 18:58:14 +0200 |
|---|---|---|
| committer | yctct <yctct@yctct.com> | 2026-09-13 18:58:14 +0200 |
| commit | 47beba686ff9d2aab63738fba7176a5235703d05 (patch) | |
| tree | f839ed36a9e9d4476fcdc59379b17903abfa35c8 /src/monitoring.c | |
Add all files, first commit
Diffstat (limited to 'src/monitoring.c')
| -rw-r--r-- | src/monitoring.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/monitoring.c b/src/monitoring.c new file mode 100644 index 0000000..7c74b4d --- /dev/null +++ b/src/monitoring.c @@ -0,0 +1,44 @@ +#include "codexion.h" +# include <unistd.h> // for usleep() + +static int check_if_philos_done(t_sim *sim) +{ + t_philo **philos; + t_philo *philo; + int number_of_philos; + int diff; + + (void)pthread_mutex_lock(&sim->mutex_philos); + philos = sim->philos; + (void)pthread_mutex_unlock(&sim->mutex_philos); + (void)pthread_mutex_lock(&sim->mutex_number_of_philos); + number_of_philos = sim->number_of_philos; + (void)pthread_mutex_unlock(&sim->mutex_number_of_philos); + while (number_of_philos-- > 0) + { + philo = *philos++; + (void)pthread_mutex_lock(&sim->mutex_number_of_eat_required); + (void)pthread_mutex_lock(&philo->mutex_number_of_eats_done); + diff = philo->number_of_eats_done + != sim->number_of_eat_required; + (void)pthread_mutex_unlock(&philo->mutex_number_of_eats_done); + (void)pthread_mutex_unlock(&sim->mutex_number_of_eat_required); + if (diff != 0) + return (0); + } + return (1); +} + +void *monitoring_routine(void *arg) +{ + t_sim *sim; + + sim = (t_sim *)arg; + while (check_if_philos_done(sim) == 0) + { + if (check_for_starves(sim) == 1) + return (NULL); + usleep(100); + } + return (NULL); +} |
