From: Lukas Jiriste Date: Thu, 28 Mar 2024 13:56:54 +0000 (+0100) Subject: Implement seat_philosophers X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=25acf8068921fc6abde98dbfdfc2e6684621519e;p=42%2Fphilosophers.git Implement seat_philosophers Thanks to the change of how the program keeps track of time it is possible to set the "last_eaten" member of philo in advance (to zero - the start) without slowing the seating. Thanks to this, no delay is necessary before the program checks whether philos died of hunger. (It was needed in old branch, because philosophers initialized their own time at the start of their existance, and the check could happen before the time was set) --- diff --git a/philo/main.c b/philo/main.c index c4a9e7c..0226bee 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 10:00:52 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 15:13:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,30 @@ #include #include +int seat_philosophers(t_diner *diner, pthread_t *threads) +{ + size_t i; + + gettimeofday(&diner->setting.start, NULL); + i = 0; + while (i < diner->setting.philo_count) + { + if (pthread_create(threads + i, NULL, be_a_philosopher, diner->philos + i)) + { + mutex_lock(&diner->setting.end_lock); + diner->setting.end = 1; + mutex_unlock(&diner->setting.end_lock); + while (i > 0) + { + pthread_join(threads[--i], NULL); + } + return (1); + } + ++i; + } + return (0); +} + int main(int argc, char **argv) { t_diner diner; @@ -28,8 +52,11 @@ int main(int argc, char **argv) cleanup(&diner, threads); return (2); } - seat_philosophers(&diner, threads); - wait_till_death_possible(diner.setting.time_to_die, diner.setting.start); + if (seat_philosophers(&diner, threads)) + { + cleanup(&diner, threads); + return (3); + } report_end(watch_philosophers(&diner)); cleanup(&diner, threads); return (0); diff --git a/philo/mutex.c b/philo/mutex.c index 54ae20b..6073e71 100644 --- a/philo/mutex.c +++ b/philo/mutex.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 10:35:16 by ljiriste #+# #+# */ -/* Updated: 2024/03/28 10:49:35 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 15:12:37 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,20 @@ void destroy_mutex(t_mutex *mutex) mutex->is_init = 0; } +int mutex_lock(t_mutex *mutex) +{ + if (!mutex->is_init) + return (1); + return (pthread_mutex_lock(&mutex->mutex)); +} + +int mutex_unlock(t_mutex *mutex) +{ + if (!mutex->is_init) + return (1); + return (pthread_mutex_unlock(&mutex->mutex)); +} + int all_mutexes_initialized(t_diner *diner) { int res; diff --git a/philo/philo.h b/philo/philo.h index 8076801..f3f2776 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 11:27:15 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 15:14:17 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,11 +65,13 @@ void cleanup(t_diner *diner, pthread_t *threads); int init_mutex(t_mutex *mutex, pthread_mutexattr_t *attr); void destroy_mutex(t_mutex *mutex); +int mutex_lock(t_mutex *mutex); +int mutex_unlock(t_mutex *mutex); int all_mutexes_initialized(t_diner *diner); +void *be_a_philosopher(void *philo); + int seat_philosophers(t_diner *diner, pthread_t *threads); -void wait_till_death_possible(useconds_t time_to_die, - struct timeval start); void report_end(int end); int watch_philosophers(t_diner *diner);