From: Lukas Jiriste Date: Fri, 10 May 2024 10:13:10 +0000 (+0200) Subject: Implement be_a_philosopher and its helpers X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=0d4721574f95e4e28f011f1ef2898f107f0052a7;p=42%2Fphilosophers.git Implement be_a_philosopher and its helpers Also implement kill_philosophers and other minor changes. --- diff --git a/philo_bonus/helpers.c b/philo_bonus/helpers.c index 35f974a..1c250bd 100644 --- a/philo_bonus/helpers.c +++ b/philo_bonus/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 11:59:36 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 12:11:52 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,6 @@ #include #include -int end(t_settings *set) -{ - int res; - - mutex_lock(&set->end_lock); - res = set->end; - mutex_unlock(&set->end_lock); - return (res); -} - useconds_t usecs_since_start(struct timeval start) { struct timeval time; @@ -35,19 +25,10 @@ useconds_t usecs_since_start(struct timeval start) void print_timestamp(t_philo *philo, const char *str) { + sem_wait(philo->semaphores[term]); mutex_lock(&philo->settings->terminal_lock); - printf("%u %lu %s\n", usecs_since_start(philo->settings->start) + printf("%u %lu %s\n", usecs_since_start(philo->settings.start) / 1000, philo->id, str); - mutex_unlock(&philo->settings->terminal_lock); + sem_post(philo->semaphores[term]); return ; } - -int report(t_philo *philo, const char *str) -{ - if (!end(philo->settings)) - { - print_timestamp(philo, str); - return (0); - } - return (1); -} diff --git a/philo_bonus/main.c b/philo_bonus/main.c index e2667f3..1b72eaa 100644 --- a/philo_bonus/main.c +++ b/philo_bonus/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */ -/* Updated: 2024/05/10 11:42:24 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 11:48:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,32 +39,13 @@ static int seat_philosophers(t_settings *settings, pid_t *philo_pids) return (0); } -static enum e_end watch_philosophers(t_diner *diner) +void kill_philosophers(unsigned int count, pid_t *philo_pids) { - size_t i; - - while (1) + while (count > 0) { - 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 (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); - } + --count; + if (philo_pids[count] != 0) + kill(philo_pids[count], SIGTERM); } } diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 6dfa0fb..7a7877e 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,56 +6,64 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 10:25:46 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 12:12:26 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include +static void open_semaphores(sem_t *semaphores[sem_num]) +{ + semaphores[forks] = sem_open(FORKS_SEM, 0); + semaphores[fed] = sem_open(WELL_FED_SEM, 0); + semaphores[death] = sem_open(DEATH_SEM, 0); + semaphores[term] = sem_open(TERM_SEM, 0); +} + static void philo_think(t_philo *philo) { - report(philo, "is thinking"); + print_timestamp(philo, "is thinking"); return ; } static void philo_eat(t_philo *philo) { - mutex_lock(philo->forks[0]); - if (report(philo, "has taken a fork")) - { - mutex_unlock(philo->forks[0]); - return ; - } - mutex_lock(philo->forks[1]); - report(philo, "has taken a fork"); - mutex_lock(&philo->philo_lock); - philo->last_eaten = usecs_since_start(philo->settings->start); + useconds_t time_now; + + sem_wait(philo->semaphores[forks]); + print_timestamp(philo, "has taken a fork"); + sem_wait(philo->semaphores[forks]); + print_timestamp(philo, "has taken a fork"); + time_now = usecs_since_start(philo->settings.start); + if (time_now - philo->last_eaten > philo->settings.time_to_die) + die(philo); + philo->last_eaten = time_now; ++philo->num_eaten; - mutex_unlock(&philo->philo_lock); - if (!report(philo, "is eating")) - usleep(philo->settings->time_to_eat); - mutex_unlock(philo->forks[0]); - mutex_unlock(philo->forks[1]); + print_timestamp(philo, "is eating"); + if (philo->settings.should_check_hunger && + philo->num_eaten == philo->settings.min_eat_num) + sem_post(philo->semaphores[fed]); + usleep(philo->settings->time_to_eat); + sem_post(philo->semaphores[forks]); + sem_post(philo->semaphores[forks]); return ; } static void philo_sleep(t_philo *philo) { - if (!report(philo, "is sleeping")) - usleep(philo->settings->time_to_sleep); + print_timestamp(philo, "is sleeping"); + usleep(philo->settings->time_to_sleep); return ; } -void *be_a_philosopher(void *void_philo) +void be_a_philosopher(t_philo *philo) { - t_philo *const philo = void_philo; - - while (!end(philo->settings)) + open_semaphores(philo->semaphores); + while (1) { philo_think(philo); philo_eat(philo); philo_sleep(philo); } - return (NULL); } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 548d65e..c1df3c5 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ -/* Updated: 2024/05/10 10:59:31 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 12:12:26 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,10 +59,7 @@ typedef struct s_philosopher unsigned int num_eaten; unsigned int id; useconds_t last_eaten; - sem_t *forks; - sem_t *well_fed_sem; - sem_t *death_sem; - sem_t *terminal_sem; + sem_t *semaphores[sem_num]; t_settings settings; } t_philo;