diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-24 13:06:05 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-24 13:06:05 +0200 |
| commit | 2baa67fd7ad31fe53ab21d257a104478e787fb62 (patch) | |
| tree | ac38bdf7aecd7056e1b7dfdd51f5403547966380 | |
| parent | 47e1a7b4af69e1998182126310e42af83cf214ed (diff) | |
| download | philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.tar.gz philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.tar.bz2 philosophers-2baa67fd7ad31fe53ab21d257a104478e787fb62.zip | |
Replacing mutex stdout by semaphore in philo two, Compilation on Linux
| -rw-r--r-- | Makefile | 16 | ||||
| -rw-r--r-- | philo_one/Makefile | 4 | ||||
| -rw-r--r-- | philo_three/Makefile | 6 | ||||
| -rw-r--r-- | philo_three/src/philo_three.h | 3 | ||||
| -rw-r--r-- | philo_two/Makefile | 6 | ||||
| -rw-r--r-- | philo_two/src/event.c | 24 | ||||
| -rw-r--r-- | philo_two/src/main.c | 24 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 5 |
8 files changed, 47 insertions, 41 deletions
@@ -6,12 +6,11 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 03:31:28 by cacharle #+# #+# # -# Updated: 2020/09/27 09:04:22 by charles ### ########.fr # +# Updated: 2020/10/24 13:05:33 by charles ### ########.fr # # # # **************************************************************************** # -MAKE = make -MAKE_ARGS = --no-print-directory +MAKE = make --no-print-directory PHILO_ONE_DIR = philo_one PHILO_TWO_DIR = philo_two @@ -22,14 +21,13 @@ help: @echo "make philo_two - compile philo_two" @echo "make philo_three - compile philo_three" -.PHONY: philo_one philo_one: - $(MAKE) $(MAKE_ARGS) -C $(PHILO_ONE_DIR) + $(MAKE) -C $(PHILO_ONE_DIR) -.PHONY: philo_two philo_two: - $(MAKE) $(MAKE_ARGS) -C $(PHILO_TWO_DIR) + $(MAKE) -C $(PHILO_TWO_DIR) -.PHONY: philo_three philo_three: - $(MAKE) $(MAKE_ARGS) -C $(PHILO_THREE_DIR) + $(MAKE) -C $(PHILO_THREE_DIR) + +.PHONY: philo_one philo_two philo_three help diff --git a/philo_one/Makefile b/philo_one/Makefile index 391fa4f..9fb6a6a 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/30 10:20:59 by cacharle ### ########.fr # +# Updated: 2020/10/24 13:02:59 by charles ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,7 @@ MAKE = make --no-print-directory COMMONDIR = ../common CC = gcc -CCFLAGS = -g -I$(COMMONDIR) -Wall -Wextra #-Werror +CCFLAGS = -g -I$(COMMONDIR) -Wall -Wextra -Werror LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_one diff --git a/philo_three/Makefile b/philo_three/Makefile index 3687670..32b3982 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/10/05 10:31:22 by cacharle ### ########.fr # +# Updated: 2020/10/24 13:02:46 by charles ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,7 @@ MAKE = make --no-print-directory COMMON_DIR = ../common CC = gcc -CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra #-Werror +CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra -Werror LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon NAME = philo_three @@ -33,7 +33,7 @@ prebuild: @mkdir -p $(OBJDIR) $(NAME): common_all $(OBJ) - $(CC) $(LDFLAGS) -o $@ $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index 1c1b8af..bb41343 100644 --- a/philo_three/src/philo_three.h +++ b/philo_three/src/philo_three.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ -/* Updated: 2020/10/05 16:54:29 by cacharle ### ########.fr */ +/* Updated: 2020/10/24 13:02:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define PHILO_THREE_H # include <unistd.h> +# include <fcntl.h> # include <stdlib.h> # include <signal.h> # include <sys/time.h> diff --git a/philo_two/Makefile b/philo_two/Makefile index 33ae4ea..3135bb4 100644 --- a/philo_two/Makefile +++ b/philo_two/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/10/05 16:21:27 by cacharle ### ########.fr # +# Updated: 2020/10/24 13:02:05 by charles ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,7 @@ MAKE = make --no-print-directory COMMONDIR = ../common CC = gcc -CCFLAGS = -g -Wall -Wextra -I$(COMMONDIR) #-Werror +CCFLAGS = -g -Wall -Wextra -I$(COMMONDIR) -Werror LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_two @@ -33,7 +33,7 @@ prebuild: @mkdir -p $(OBJDIR) $(NAME): common_all $(OBJ) - $(CC) $(LDFLAGS) -o $@ $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <unistd.h> +# include <fcntl.h> # include <stdbool.h> # include <pthread.h> # include <semaphore.h> @@ -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 |
