/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/09 11:58:04 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 12:31:31 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <stddef.h>
#include <pthread.h>
+#include <limits.h>
+#include <stdio.h>
-int seat_philosophers(t_diner *diner, pthread_t *threads)
+static int seat_philosophers(t_diner *diner, pthread_t *threads)
{
size_t i;
return (0);
}
-int is_dead(t_philo *philo)
+static int is_dead(t_philo *philo)
{
mutex_lock(&philo->philo_lock);
if (usecs_since_start(philo->settings->start) - philo->last_eaten
return (0);
}
-void watch_philosophers(t_diner *diner)
+static int all_full(t_diner *diner)
+{
+ size_t i;
+ unsigned int min_eaten;
+
+ if (!diner->setting.should_check_hunger)
+ return (0);
+ i = 0;
+ min_eaten = UINT_MAX;
+ while (i < diner->setting.philo_count)
+ {
+ mutex_lock(&diner->philos[i].philo_lock);
+ if (min_eaten > diner->philos[i].num_eaten)
+ min_eaten = diner->philos[i].num_eaten;
+ mutex_unlock(&diner->philos[i].philo_lock);
+ ++i;
+ }
+ return (min_eaten >= diner->setting.min_eat_num);
+}
+
+static enum e_end watch_philosophers(t_diner *diner)
{
size_t i;
diner->setting.end = 1;
mutex_unlock(&diner->setting.end_lock);
print_timestamp(diner->philos + i, "died");
- return ;
+ return (death);
}
++i;
}
+ if (all_full(diner))
+ {
+ mutex_lock(&diner->setting.end_lock);
+ diner->setting.end = 1;
+ mutex_unlock(&diner->setting.end_lock);
+ return (well_fed);
+ }
}
}
{
t_diner diner;
pthread_t *threads;
+ enum e_end res;
if (parse_input(&diner.setting, argc, argv))
{
cleanup(&diner, threads);
return (3);
}
- watch_philosophers(&diner);
+ res = watch_philosophers(&diner);
cleanup(&diner, threads);
+ if (res == well_fed)
+ printf("All philosophers are well fed.\n");
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 09:31:35 by ljiriste #+# #+# */
-/* Updated: 2024/03/28 11:32:40 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 12:35:58 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdlib.h>
+int is_sane(t_settings *settings)
+{
+ return (settings->philo_count > 0);
+}
+
int parse_input(t_settings *settings, int argc, char **argv)
{
if (argc != 5 && argc != 6)
return (1);
settings->should_check_hunger = 1;
}
- return (0);
+ return (!is_sane(settings));
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/09 10:25:30 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 12:21:57 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
typedef struct s_philosopher
{
- int num_eaten;
- size_t id;
- useconds_t last_eaten;
- t_settings *settings;
- t_mutex *forks[2];
- t_mutex philo_lock;
+ unsigned int num_eaten;
+ size_t id;
+ useconds_t last_eaten;
+ t_settings *settings;
+ t_mutex *forks[2];
+ t_mutex philo_lock;
} t_philo;
typedef struct s_diner
t_mutex *forks;
} t_diner;
+enum e_end
+{
+ death,
+ well_fed,
+};
+
int parse_input(t_settings *settings, int argc, char **argv);
int init(t_diner *diner, pthread_t **threads);
void *be_a_philosopher(void *philo);
-int seat_philosophers(t_diner *diner, pthread_t *threads);
-void watch_philosophers(t_diner *diner);
-
#endif //PHILO_H