From: Lukas Jiriste Date: Thu, 9 May 2024 08:27:02 +0000 (+0200) Subject: Implement checking for the death of philosopher X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=c4f2b8f2984123fabee1df823a73c78608e773e3;p=42%2Fphilosophers.git Implement checking for the death of philosopher Also minor refactoring --- diff --git a/philo/Makefile b/philo/Makefile index 2679ee8..3c4245c 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -8,7 +8,7 @@ INCDIR := . SRCDIR := . SRCS := main.c \ philo.c \ - philo_helpers.c \ + helpers.c \ parsing.c \ parsing_misc.c \ pars_arg.c \ diff --git a/philo/philo_helpers.c b/philo/helpers.c similarity index 68% rename from philo/philo_helpers.c rename to philo/helpers.c index 4e5a625..09b71c6 100644 --- a/philo/philo_helpers.c +++ b/philo/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -24,18 +24,27 @@ int end(t_settings *set) 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); diff --git a/philo/main.c b/philo/main.c index 0226bee..97ef11b 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -38,6 +38,39 @@ int seat_philosophers(t_diner *diner, pthread_t *threads) 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; @@ -57,7 +90,7 @@ int main(int argc, char **argv) cleanup(&diner, threads); return (3); } - report_end(watch_philosophers(&diner)); + watch_philosophers(&diner); cleanup(&diner, threads); return (0); } diff --git a/philo/philo.c b/philo/philo.c index ec61597..6dfa0fb 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -15,25 +15,25 @@ 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]); @@ -42,7 +42,7 @@ static void philo_eat(t_philo *philo) 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 ; } diff --git a/philo/philo.h b/philo/philo.h index 62dd21e..b63b290 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -70,12 +70,13 @@ int mutex_unlock(t_mutex *mutex); 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