From 907debbb7d1e7ccc4914805cfe4acbed92b82bcc Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 4 Jan 2021 12:20:25 +0100 Subject: Added buffered output, Added waiting for all to be started --- philo_one/Makefile | 2 +- philo_one/src/event.c | 17 ++++++++++------- philo_one/src/main.c | 3 ++- philo_one/src/philo.c | 8 ++++++-- philo_one/src/philo_one.h | 5 +++-- philo_one/src/routine.c | 11 ++++++----- 6 files changed, 28 insertions(+), 18 deletions(-) (limited to 'philo_one') diff --git a/philo_one/Makefile b/philo_one/Makefile index 0026516..6c6a97a 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/01 15:18:11 by charles ### ########.fr # +# Updated: 2021/01/04 10:23:35 by cacharle ### ########.fr # # # # **************************************************************************** # diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 8ceab65..2e24772 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,21 +6,25 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:48:00 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:08:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void event_take_fork(t_philo *arg, pthread_mutex_t *fork) +void event_take_fork(t_philo *arg) { if (philo_finished(arg->conf)) return ; - pthread_mutex_lock(fork); + pthread_mutex_lock(arg->fork_left); + pthread_mutex_lock(arg->fork_right); pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); + if (philo_finished(arg->conf)) + return ; + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -32,6 +36,8 @@ void event_eat(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); + if (arg->conf->philo_num < 30) + philo_put_flush(); pthread_mutex_unlock(&arg->conf->mutex_stdout); h_sleep(arg->conf->timeout_eat); } @@ -69,9 +75,6 @@ void event_die(t_philo *arg) if (philo_finished(arg->conf)) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); - if (philo_finished(arg->conf)) - return ; - arg->conf->all_alive = false; philo_put(arg->id, EVENT_DIE, arg->conf->initial_time); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + arg->conf->all_alive = false; } diff --git a/philo_one/src/main.c b/philo_one/src/main.c index 55e70d3..c1bbe1c 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:57:04 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:04:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ int main(int argc, char **argv) } while (!philo_finished(&conf)) ; + philo_put_flush(); philos_detach(philos, conf.philo_num); forks_destroy(forks, conf.philo_num); pthread_mutex_destroy(&conf.mutex_stdout); diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 870857b..f86dfb6 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:49:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:42:53 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks) philos[i].conf = conf; philos[i].fork_left = forks + i % conf->philo_num; philos[i].fork_right = forks + (i + 1) % conf->philo_num; + pthread_mutex_init(&philos[i].mutex_start, NULL); + pthread_mutex_lock(&philos[i].mutex_start); } return (philos); } @@ -49,8 +51,10 @@ bool philos_start(t_philo *philos, long int num) pthread_detach(philos[i].thread); return (false); } - usleep(200); } + i = -1; + while (++i < num) + pthread_mutex_unlock(&philos[i].mutex_start); return (true); } diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index de5f2c4..2ebe708 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:56:16 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:38:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ typedef struct s_philo pthread_mutex_t *fork_left; pthread_mutex_t *fork_right; pthread_mutex_t *mutex_stdout; + pthread_mutex_t mutex_start; } t_philo; /* @@ -81,7 +82,7 @@ void *routine_death(t_philo *arg); ** io.c */ -void event_take_fork(t_philo *arg, pthread_mutex_t *fork); +void event_take_fork(t_philo *arg); void event_eat(t_philo *arg); void event_think(t_philo *arg); void event_sleep( diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index d8291ef..c43d9c3 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:42:36 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 10:42:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,19 +35,20 @@ void *routine_philo(t_philo *arg) pthread_t thread_death; long int eat_counter; + pthread_mutex_lock(&arg->mutex_start); if (philo_finished(arg->conf)) return (NULL); + if (arg->id % 2 == 0) + usleep(1000); arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); eat_counter = 0; - event_think(arg); while (!philo_finished(arg->conf)) { - event_take_fork(arg, arg->fork_left); - event_take_fork(arg, arg->fork_right); - event_eat(arg); + event_take_fork(arg); arg->time_last_eat = h_time_now(); + event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); event_sleep(arg, arg->fork_right, arg->fork_left); -- cgit