SRCDIR := .
SRCS := main.c \
philo.c \
- philo_helpers.c \
+ helpers.c \
parsing.c \
parsing_misc.c \
pars_arg.c \
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */
-/* Updated: 2024/05/09 09:40:43 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 10:16:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (res);
}
-int print_timestamp(t_philo *philo, const char *str)
+useconds_t usecs_since_start(struct timeval start)
{
struct timeval time;
- useconds_t since_start;
gettimeofday(&time, NULL);
- since_start = (time.tv_sec - philo->settings->start.tv_sec) * 1000000 + time.tv_usec - philo->settings->start.tv_usec;
+ return ((time.tv_sec - start.tv_sec) * 1000000 + time.tv_usec - start.tv_usec);
+}
+
+void print_timestamp(t_philo *philo, const char *str)
+{
+ mutex_lock(&philo->settings->terminal_lock);
+ printf("%u %lu %s\n", usecs_since_start(philo->settings->start), philo->id, str);
+ mutex_unlock(&philo->settings->terminal_lock);
+ return ;
+}
+
+int report(t_philo *philo, const char *str)
+{
if (!end(philo->settings))
{
- mutex_lock(&philo->settings->terminal_lock);
- printf("%u %lu %s\n", since_start, philo->id, str);
- mutex_unlock(&philo->settings->terminal_lock);
+ print_timestamp(philo, str);
return (0);
}
return (1);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/03/28 15:13:20 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 10:24:12 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (0);
}
+int is_dead(t_philo *philo)
+{
+ mutex_lock(&philo->philo_lock);
+ if (usecs_since_start(philo->settings->start) - philo->last_eaten > philo->settings->time_to_die)
+ {
+ mutex_unlock(&philo->philo_lock);
+ return (1);
+ }
+ mutex_unlock(&philo->philo_lock);
+ return (0);
+}
+
+void watch_philosophers(t_diner *diner)
+{
+ size_t i;
+
+ while (1)
+ {
+ i = 0;
+ while (i < diner->setting.philo_count)
+ {
+ if (is_dead(diner->philos + i))
+ {
+ mutex_lock(&diner->setting.end_lock);
+ diner->setting.end = 1;
+ mutex_unlock(&diner->setting.end_lock);
+ print_timestamp(diner->philos + i, "died");
+ return ;
+ }
+ }
+ }
+}
+
int main(int argc, char **argv)
{
t_diner diner;
cleanup(&diner, threads);
return (3);
}
- report_end(watch_philosophers(&diner));
+ watch_philosophers(&diner);
cleanup(&diner, threads);
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */
-/* Updated: 2024/05/09 09:53:45 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 10:25:46 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
static void philo_think(t_philo *philo)
{
- print_timestamp(philo, "is thinking");
+ report(philo, "is thinking");
return ;
}
static void philo_eat(t_philo *philo)
{
mutex_lock(philo->forks[0]);
- if (print_timestamp(philo, "has taken a fork"))
+ if (report(philo, "has taken a fork"))
{
mutex_unlock(philo->forks[0]);
return ;
}
mutex_lock(philo->forks[1]);
- print_timestamp(philo, "has taken a fork");
+ report(philo, "has taken a fork");
mutex_lock(&philo->philo_lock);
- last_eaten = usecs_since_start(philo->settings->start);
+ philo->last_eaten = usecs_since_start(philo->settings->start);
++philo->num_eaten;
mutex_unlock(&philo->philo_lock);
- if (!print_timestamp(philo, "is eating"))
+ if (!report(philo, "is eating"))
usleep(philo->settings->time_to_eat);
mutex_unlock(philo->forks[0]);
mutex_unlock(philo->forks[1]);
static void philo_sleep(t_philo *philo)
{
- if (!print_timestamp(philo, "is sleeping"))
+ if (!report(philo, "is sleeping"))
usleep(philo->settings->time_to_sleep);
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/09 09:38:31 by ljiriste ### ########.fr */
+/* Updated: 2024/05/09 10:25:30 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int all_mutexes_initialized(t_diner *diner);
int end(t_settings *set);
-int print_timestamp(t_philo *philo, const char *str);
+useconds_t usecs_since_start(struct timeval start);
+int report(t_philo *philo, const char *str);
+void print_timestamp(t_philo *philo, const char *str);
void *be_a_philosopher(void *philo);
int seat_philosophers(t_diner *diner, pthread_t *threads);
-void report_end(int end);
-int watch_philosophers(t_diner *diner);
+void watch_philosophers(t_diner *diner);
#endif //PHILO_H