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_two/src/event.c | 4 +++- philo_two/src/main.c | 17 ++++++++++++----- philo_two/src/philo_two.h | 3 ++- philo_two/src/routine.c | 11 ++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) (limited to 'philo_two/src') diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 25fcb0e..4eafd90 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:53:35 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:15:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,11 @@ void event_take_fork(t_philo *arg) { + sem_wait(arg->forks); sem_wait(arg->forks); sem_wait(arg->conf->sem_stdout); philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 0f823b5..61f0410 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:54:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:14:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #define PHILO_SEM_NAME "semaphore_philo_two" #define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" #define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish" +#define PHILO_SEM_START_NAME "semaphore_philo_two_start" static int st_destroy( t_philo *philos, @@ -23,6 +24,7 @@ static int st_destroy( sem_unlink(PHILO_SEM_NAME); sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_FINISH_NAME); + sem_unlink(PHILO_SEM_START_NAME); free(philos); free(threads); return (1); @@ -46,16 +48,19 @@ static int st_setup( if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) || !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || !st_sem_create(PHILO_SEM_FINISH_NAME, - conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish)) + conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish) || + !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start)) return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) return (st_destroy(*philos, *threads)); + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_start); conf->initial_time = h_time_now(); i = -1; while (++i < conf->philo_num) - { if (pthread_create(*threads + i, NULL, (t_routine)routine_philo, *philos + i) != 0) { @@ -63,8 +68,6 @@ static int st_setup( pthread_detach((*threads)[i]); return (st_destroy(*philos, *threads)); } - usleep(200); - } return (0); } @@ -102,7 +105,11 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); + i = -1; + while (++i < conf.philo_num) + sem_post(conf.sem_start); st_wait(&conf); + philo_put_flush(); i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index 8261b0c..bde41c1 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2021/01/03 13:55:04 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 09:35:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ typedef struct t_time initial_time; sem_t *sem_stdout; sem_t *sem_finish; + sem_t *sem_start; } t_philo_conf; typedef struct diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 3c04817..d6e1df3 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/03 12:57:27 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:17:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,18 @@ void *routine_philo(t_philo *arg) long int eat_counter; pthread_t thread_death; + eat_counter = 0; + sem_wait(arg->conf->sem_start); + if (arg->id % 2 == 0) + usleep(500); 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 (true) { event_take_fork(arg); - event_take_fork(arg); - event_eat(arg); arg->time_last_eat = h_time_now(); + event_eat(arg); if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) { sem_wait(arg->conf->sem_stdout); -- cgit