From: Lukas Jiriste Date: Thu, 9 May 2024 07:41:44 +0000 (+0200) Subject: Implement the philo actions X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=84c8caee19f5472812749700c9bf5faaf1242c60;p=42%2Fphilosophers.git Implement the philo actions --- diff --git a/philo/Makefile b/philo/Makefile index 33a9055..2679ee8 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -7,6 +7,8 @@ NAME := philo INCDIR := . SRCDIR := . SRCS := main.c \ + philo.c \ + philo_helpers.c \ parsing.c \ parsing_misc.c \ pars_arg.c \ diff --git a/philo/philo.c b/philo/philo.c new file mode 100644 index 0000000..ec61597 --- /dev/null +++ b/philo/philo.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */ +/* Updated: 2024/05/09 09:53:45 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include + +static void philo_think(t_philo *philo) +{ + print_timestamp(philo, "is thinking"); + return ; +} + +static void philo_eat(t_philo *philo) +{ + mutex_lock(philo->forks[0]); + if (print_timestamp(philo, "has taken a fork")) + { + mutex_unlock(philo->forks[0]); + return ; + } + mutex_lock(philo->forks[1]); + print_timestamp(philo, "has taken a fork"); + mutex_lock(&philo->philo_lock); + last_eaten = usecs_since_start(philo->settings->start); + ++philo->num_eaten; + mutex_unlock(&philo->philo_lock); + if (!print_timestamp(philo, "is eating")) + usleep(philo->settings->time_to_eat); + mutex_unlock(philo->forks[0]); + mutex_unlock(philo->forks[1]); + return ; +} + +static void philo_sleep(t_philo *philo) +{ + if (!print_timestamp(philo, "is sleeping")) + usleep(philo->settings->time_to_sleep); + return ; +} + +void *be_a_philosopher(void *void_philo) +{ + t_philo *const philo = void_philo; + + while (!end(philo->settings)) + { + philo_think(philo); + philo_eat(philo); + philo_sleep(philo); + } + return (NULL); +} diff --git a/philo/philo.h b/philo/philo.h index f3f2776..62dd21e 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/03/28 15:14:17 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 09:38:31 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,6 +69,9 @@ int mutex_lock(t_mutex *mutex); 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); + void *be_a_philosopher(void *philo); int seat_philosophers(t_diner *diner, pthread_t *threads); diff --git a/philo/philo_helpers.c b/philo/philo_helpers.c new file mode 100644 index 0000000..4e5a625 --- /dev/null +++ b/philo/philo_helpers.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */ +/* Updated: 2024/05/09 09:40:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include +#include + +int end(t_settings *set) +{ + int res; + + mutex_lock(&set->end_lock); + res = set->end; + mutex_unlock(&set->end_lock); + return (res); +} + +int print_timestamp(t_philo *philo, const char *str) +{ + 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; + 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); + return (0); + } + return (1); +}