From 2baa67fd7ad31fe53ab21d257a104478e787fb62 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 24 Oct 2020 13:06:05 +0200 Subject: Replacing mutex stdout by semaphore in philo two, Compilation on Linux --- philo_two/src/event.c | 24 ++++++++++++------------ philo_two/src/main.c | 24 +++++++++++++++--------- philo_two/src/philo_two.h | 5 +++-- 3 files changed, 30 insertions(+), 23 deletions(-) (limited to 'philo_two/src') diff --git a/philo_two/src/event.c b/philo_two/src/event.c index d86f6aa..8060b2b 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* io.c :+: :+: :+: */ +/* event.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:56:53 by cacharle ### ########.fr */ +/* Updated: 2020/10/24 13:02:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,11 @@ void event_take_fork(t_philo *arg) { sem_wait(arg->forks); - pthread_mutex_lock(&arg->conf->mutex_stdout); + sem_wait(arg->conf->sem_stdout); if (!arg->conf->all_alive) return ; philo_put(arg->id, EVENT_FORK); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->conf->sem_stdout); } void event_eat(t_philo *arg) @@ -29,11 +29,11 @@ void event_eat(t_philo *arg) eat_counter = 0; while (eat_counter < arg->conf->meal_num) { - pthread_mutex_lock(&arg->conf->mutex_stdout); + sem_wait(arg->conf->sem_stdout); if (!arg->conf->all_alive) return ; philo_put(arg->id, EVENT_EAT); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->conf->sem_stdout); usleep(arg->conf->timeout_eat * 1000); eat_counter++; } @@ -41,20 +41,20 @@ void event_eat(t_philo *arg) void event_think(t_philo *arg) { - pthread_mutex_lock(&arg->conf->mutex_stdout); + sem_wait(arg->conf->sem_stdout); if (!arg->conf->all_alive) return ; philo_put(arg->id, EVENT_THINK); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->conf->sem_stdout); } void event_sleep(t_philo *arg) { - pthread_mutex_lock(&arg->conf->mutex_stdout); + sem_wait(arg->conf->sem_stdout); if (!arg->conf->all_alive) return ; philo_put(arg->id, EVENT_SLEEP); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->conf->sem_stdout); sem_post(arg->forks); sem_post(arg->forks); usleep(arg->conf->timeout_sleep * 1000); @@ -62,10 +62,10 @@ void event_sleep(t_philo *arg) void event_die(t_philo *arg) { - pthread_mutex_lock(&arg->conf->mutex_stdout); + sem_wait(arg->conf->sem_stdout); if (!arg->conf->all_alive) return ; philo_put(arg->id, EVENT_DIE); arg->conf->all_alive = false; - pthread_mutex_unlock(&arg->conf->mutex_stdout); + sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index b86dc51..788122b 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,27 +6,30 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2020/10/05 16:23:19 by cacharle ### ########.fr */ +/* Updated: 2020/10/24 13:02:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -#define PHILO_SEM_NAME "semaphore_philo_two" +#define PHILO_SEM_NAME "semaphore_philo_two" +#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" static int st_destroy( sem_t *forks, t_philo *philos, pthread_t *threads, - int philo_num) + t_philo_conf *conf) { int i; i = -1; - while (++i < philo_num) + while (++i < conf->philo_num) sem_post(forks); sem_close(forks); sem_unlink(PHILO_SEM_NAME); + sem_close(conf->sem_stdout); + sem_unlink(PHILO_SEM_STDOUT_NAME); free(philos); free(threads); return (1); @@ -44,11 +47,14 @@ static int st_setup( *forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, conf->philo_num); if (*forks == SEM_FAILED) return (1); + sem_unlink(PHILO_SEM_STDOUT_NAME); + conf->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1); + if (conf->sem_stdout == SEM_FAILED) + return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || - (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL || - pthread_mutex_init(&conf->mutex_stdout, NULL) != 0) - return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) + return (st_destroy(*forks, *philos, *threads, conf)); conf->all_alive = true; i = -1; while (++i < conf->philo_num) @@ -57,7 +63,7 @@ static int st_setup( { while (--i >= 0) pthread_detach((*threads)[i]); - return (st_destroy(*forks, *philos, *threads, conf->philo_num)); + return (st_destroy(*forks, *philos, *threads, conf)); } return (0); } @@ -81,6 +87,6 @@ int main(int argc, char **argv) i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); - st_destroy(forks, philos, threads, conf.philo_num); + st_destroy(forks, philos, threads, &conf); return (0); } diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index b3f08ed..b947684 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: 2020/10/05 14:29:40 by cacharle ### ########.fr */ +/* Updated: 2020/10/24 13:03:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define PHILO_TWO_H # include +# include # include # include # include @@ -28,7 +29,7 @@ typedef struct t_time timeout_sleep; long int meal_num; bool all_alive; - pthread_mutex_t mutex_stdout; + sem_t *sem_stdout; } t_philo_conf; typedef struct -- cgit