diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-30 08:46:00 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-30 08:46:00 +0200 |
| commit | 608ae732eccfe50f2727823f9aebe1f32681edfb (patch) | |
| tree | 01d62708d1c4b0aa444e272c7f19022b5cd70821 | |
| parent | 42360f381cebd6376c261b16038a7f12971f925e (diff) | |
| download | philosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.tar.gz philosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.tar.bz2 philosophers-608ae732eccfe50f2727823f9aebe1f32681edfb.zip | |
Refactoring philo_two to work with new common lib (not working)
| -rw-r--r-- | common/Makefile | 8 | ||||
| -rw-r--r-- | common/common.h | 4 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 4 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 4 | ||||
| -rw-r--r-- | philo_two/Makefile | 25 | ||||
| -rw-r--r-- | philo_two/src/io.c | 77 | ||||
| -rw-r--r-- | philo_two/src/main.c | 38 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 28 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 48 |
9 files changed, 142 insertions, 94 deletions
diff --git a/common/Makefile b/common/Makefile index 082d66e..62d75c9 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2020/02/14 00:49:18 by cacharle ### ########.fr # +# Updated: 2020/09/30 08:22:35 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ LIB = ar rcs RM = rm -rf CC = gcc -CCFLAGS = -Wall -Wextra #-Werror +CCFLAGS = -O2 -Wall -Wextra -Werror NAME = libphilocommon.a @@ -31,8 +31,8 @@ $(NAME): $(OBJ) clean: $(RM) $(OBJ) - -fclean: clean $(RM) $(NAME) re: fclean all + +.PHONY: all clean re diff --git a/common/common.h b/common/common.h index b6f21c2..0b64e92 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/09/30 07:57:32 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:40:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ typedef struct long int meal_num; } t_philo_args; -typedef void (*t_routine)(void *arg); +typedef void *(*t_routine)(void *arg); /* ** common.c diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 3e21490..fd14674 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:10:55 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:38:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) while (++i < num) { if (pthread_create(&philos[i].thread, NULL, - (void *(*)(void*))routine_philo, (void*)(routine_args + i)) == -1) + (t_routine)routine_philo, (void*)(routine_args + i)) == -1) return (false); } return (true); diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 1803b7d..4b829f0 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:17:21 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:38:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void *routine_philo(t_routine_arg *arg) if (!arg->conf->all_alive) return (NULL); arg->philo->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, (void *(*)(void*))routine_death, arg) != 0) + if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); io_think(arg); while (arg->conf->all_alive) diff --git a/philo_two/Makefile b/philo_two/Makefile index b1cd2ed..4561afc 100644 --- a/philo_two/Makefile +++ b/philo_two/Makefile @@ -6,17 +6,18 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/29 15:21:43 by cacharle ### ########.fr # +# Updated: 2020/09/30 08:23:03 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f +MAKE = make --no-print-directory -# COMMON_DIR = ../common +COMMONDIR = ../common CC = gcc -CCFLAGS = -Wall -Wextra #-I$(COMMON_DIR) -Werror -LDFLAGS = -lpthread #-L$(COMMON_DIR) -lphilocommon +CCFLAGS = -Wall -Wextra -I$(COMMONDIR) #-Werror +LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon NAME = philo_two @@ -26,7 +27,7 @@ OBJDIR = obj SRC = $(shell find $(SRCDIR) -type f -name '*.c') OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) -all: prebuild $(NAME) +all: prebuild common_all $(NAME) prebuild: @mkdir -p $(OBJDIR) @@ -37,7 +38,7 @@ $(NAME): $(OBJ) $(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: +clean: common_clean $(RM) $(OBJ) fclean: clean @@ -45,8 +46,10 @@ fclean: clean re: fclean all -# common_all: -# make -C $(COMMON_DIR) all -# -# common_fclean: -# make -C $(COMMON_DIR) fclean +common_all: + $(MAKE) -C $(COMMONDIR) all + +common_clean: + $(MAKE) -C $(COMMONDIR) clean + +.PHONY: all prebuild clean fclean re common_all common_clean diff --git a/philo_two/src/io.c b/philo_two/src/io.c index 20aec03..771d9ee 100644 --- a/philo_two/src/io.c +++ b/philo_two/src/io.c @@ -5,41 +5,74 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 23:37:50 by cacharle #+# #+# */ -/* Updated: 2020/02/14 23:50:10 by cacharle ### ########.fr */ +/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ +/* Updated: 2020/09/30 08:43:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -void io_eat(t_routine_arg *arg) +void io_take_fork(t_routine_arg *arg) { - pthread_mutex_lock(&arg->args->mutex_stdout); - if (arg->args->all_alive) - philo_eat(arg->id, arg->args->timeout_eat); - pthread_mutex_unlock(&arg->args->mutex_stdout); + pthread_mutex_lock(&arg->conf->mutex_all_alive); + if (!arg->conf->all_alive) + return ; + sem_wait(arg->forks); + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_FORK); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); } -void io_think(t_routine_arg *arg) +void io_eat(t_routine_arg *arg) { - pthread_mutex_lock(&arg->args->mutex_stdout); - if (arg->args->all_alive) - philo_think(arg->id); - pthread_mutex_unlock(&arg->args->mutex_stdout); + int eat_counter; + + eat_counter = 0; + while (eat_counter < arg->conf->meal_num) + { + pthread_mutex_lock(&arg->conf->mutex_all_alive); + if (!arg->conf->all_alive) + return ; + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_EAT); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); + usleep(arg->conf->timeout_eat * 1000); + eat_counter++; + } +} + +void io_think(t_routine_arg *arg) +{ + pthread_mutex_lock(&arg->conf->mutex_all_alive); + if (!arg->conf->all_alive) + return ; + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_THINK); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); } -void io_sleep(t_routine_arg *arg) +void io_sleep(t_routine_arg *arg) { - pthread_mutex_lock(&arg->args->mutex_stdout); - if (arg->args->all_alive) - philo_sleep(arg->id, arg->args->timeout_sleep); - pthread_mutex_unlock(&arg->args->mutex_stdout); + pthread_mutex_lock(&arg->conf->mutex_all_alive); + if (!arg->conf->all_alive) + return ; + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_SLEEP); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); + sem_post(arg->forks); + sem_post(arg->forks); + usleep(arg->conf->timeout_sleep * 1000); } -void io_die(t_routine_arg *arg) +void io_die(t_routine_arg *arg) { - pthread_mutex_lock(&arg->args->mutex_stdout); - if (arg->args->all_alive) - philo_die(arg->id); - pthread_mutex_unlock(&arg->args->mutex_stdout); + if (!arg->conf->all_alive) + return ; + pthread_mutex_lock(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_DIE); + pthread_mutex_unlock(&arg->conf->mutex_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 5442d1a..27dce34 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,49 +6,51 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2020/09/29 15:27:05 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:44:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" +#define PHILO_SEM_NAME "philo_two" + int main(int argc, char **argv) { int i; - t_philo_args philo_args; + t_philo_conf conf; t_routine_arg *routine_args; sem_t *forks; pthread_t *threads; - if (!parse_args(&philo_args, argc, argv)) + if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); - sem_unlink("philo_two"); - forks = sem_open("philo_two", O_CREAT, 0700, philo_args.philo_num); + sem_unlink(PHILO_SEM_NAME); + forks = sem_open(PHILO_SEM_NAME, O_CREAT, 0700, conf.philo_num); if (forks == SEM_FAILED) return (1); - if ((routine_args = routine_args_create(&philo_args, forks)) == NULL) + if ((routine_args = routine_args_create(&conf, forks)) == NULL) return (1); - if ((threads = malloc(sizeof(pthread_t) * philo_args.philo_num)) == NULL) + if ((threads = malloc(sizeof(pthread_t) * conf.philo_num)) == NULL) return (1); - philo_args.all_alive = TRUE; - pthread_mutex_init(&philo_args.mutex_all_alive, NULL); - pthread_mutex_init(&philo_args.mutex_stdout, NULL); + conf.all_alive = true; + pthread_mutex_init(&conf.mutex_all_alive, NULL); + pthread_mutex_init(&conf.mutex_stdout, NULL); i = -1; - while (++i < philo_args.philo_num) - if (pthread_create(threads + i, NULL, routine_philo, routine_args + i) < 0) + while (++i < conf.philo_num) + if (pthread_create(threads + i, NULL, (t_routine)routine_philo, routine_args + i) < 0) return (1); - while (philo_args.all_alive) + while (conf.all_alive) ; i = -1; - while (++i < philo_args.philo_num) - pthread_join(threads[i], NULL); + while (++i < conf.philo_num) + pthread_detach(threads[i]); sem_close(forks); - sem_unlink("philo_two"); + sem_unlink(PHILO_SEM_NAME); - pthread_mutex_destroy(&philo_args.mutex_stdout); - pthread_mutex_destroy(&philo_args.mutex_all_alive); + pthread_mutex_destroy(&conf.mutex_stdout); + pthread_mutex_destroy(&conf.mutex_all_alive); free(threads); free(routine_args); return (0); diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index fb42628..5318510 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,21 +6,36 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2020/09/29 15:21:32 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:41:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PHILO_TWO_H # define PHILO_TWO_H +# include <unistd.h> +# include <stdbool.h> # include <pthread.h> # include <semaphore.h> -// # include "common.h" + +# include "common.h" + +typedef struct +{ + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; + bool all_alive; + pthread_mutex_t mutex_all_alive; + pthread_mutex_t mutex_stdout; +} t_philo_conf; typedef struct { int id; - t_philo_args *args; + t_philo_conf *conf; t_time time_last_eat; sem_t *forks; } t_routine_arg; @@ -29,14 +44,15 @@ typedef struct ** routine.c */ -void *routine_philo(void *void_arg); -void *routine_death(void *void_arg); -t_routine_arg *routine_args_create(t_philo_args *philo_args, sem_t *forks); +void *routine_philo(t_routine_arg *arg); +void *routine_death(t_routine_arg *arg); +t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks); /* ** io.c */ +void io_take_fork(t_routine_arg *arg); void io_eat(t_routine_arg *arg); void io_think(t_routine_arg *arg); void io_sleep(t_routine_arg *arg); diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index fefeb34..cfce645 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,31 +6,27 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2020/02/15 00:35:26 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 08:41:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -void *routine_philo(void *void_arg) +void *routine_philo(t_routine_arg *arg) { - t_routine_arg *arg; pthread_t thread_death; - arg = (t_routine_arg*)void_arg; io_think(arg); - if (!arg->args->all_alive) + if (!arg->conf->all_alive) return (NULL); arg->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, routine_death, arg) != 0) + if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); - while (arg->args->all_alive) + while (arg->conf->all_alive) { - sem_wait(arg->forks); - sem_wait(arg->forks); + io_take_fork(arg); + io_take_fork(arg); io_eat(arg); - sem_post(arg->forks); - sem_post(arg->forks); arg->time_last_eat = h_time_now(); io_sleep(arg); io_think(arg); @@ -39,36 +35,34 @@ void *routine_philo(void *void_arg) return (NULL); } -void *routine_death(void *void_arg) +void *routine_death(t_routine_arg *arg) { - t_routine_arg *arg; t_time current; - arg = (t_routine_arg*)void_arg; current = h_time_now(); - while (arg->args->all_alive && - current - arg->time_last_eat < arg->args->timeout_death) + while (arg->conf->all_alive && + current - arg->time_last_eat < arg->conf->timeout_death) current = h_time_now(); + pthread_mutex_lock(&arg->conf->mutex_all_alive); io_die(arg); - pthread_mutex_lock(&arg->args->mutex_all_alive); - arg->args->all_alive = FALSE; - pthread_mutex_unlock(&arg->args->mutex_all_alive); + arg->conf->all_alive = false; + pthread_mutex_unlock(&arg->conf->mutex_all_alive); return (NULL); } -t_routine_arg *routine_args_create(t_philo_args *philo_args, sem_t *forks) +t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks) { int i; - t_routine_arg *routine_args; + t_routine_arg *routine_conf; - if ((routine_args = malloc(sizeof(t_routine_arg) * philo_args->philo_num)) == NULL) + if ((routine_conf = malloc(sizeof(t_routine_arg) * conf->philo_num)) == NULL) return (NULL); i = -1; - while (++i < philo_args->philo_num) + while (++i < conf->philo_num) { - routine_args[i].id = i + 1; - routine_args[i].forks = forks; - routine_args[i].args = philo_args; + routine_conf[i].id = i + 1; + routine_conf[i].forks = forks; + routine_conf[i].conf = conf; } - return (routine_args); + return (routine_conf); } |
