diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-15 01:47:26 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-15 01:47:26 +0100 |
| commit | 2c5abe421b7a1b92081e38f6b1f04d407fcba834 (patch) | |
| tree | c7e7aa9db3a3b84bd80bc8a5a713d5bfeb6a66f7 | |
| parent | 6a1e91750ee43fccb6160af0f44139698c8dfdc3 (diff) | |
| download | philosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.tar.gz philosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.tar.bz2 philosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.zip | |
philo_one small refactoring, philo_three draft
| -rw-r--r-- | common/common.c | 9 | ||||
| -rw-r--r-- | common/helper.c | 2 | ||||
| -rw-r--r-- | philo_one/fork.c | 26 | ||||
| -rw-r--r-- | philo_one/main.c | 3 | ||||
| -rw-r--r-- | philo_one/philo.c | 7 | ||||
| -rw-r--r-- | philo_one/philo_one.h | 88 | ||||
| -rw-r--r-- | philo_one/routine.c | 21 | ||||
| -rw-r--r-- | philo_three/Makefile | 21 | ||||
| -rw-r--r-- | philo_three/child.c | 39 | ||||
| -rw-r--r-- | philo_three/main.c | 42 | ||||
| -rw-r--r-- | philo_three/philo_three.h | 19 |
11 files changed, 176 insertions, 101 deletions
diff --git a/common/common.c b/common/common.c index 381256b..face0b5 100644 --- a/common/common.c +++ b/common/common.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/02/14 19:42:18 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 00:54:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,12 +33,7 @@ t_bool parse_args(t_philo_args *philo_args, int argc, char **argv) void philo_put_state_change(int id, t_philo_event event) // not correct for philo3 { - struct timeval tv; - - if (gettimeofday(&tv, NULL) == -1) - return ; - h_putnbr(tv.tv_sec); - h_putnbr(tv.tv_usec / 1000); + h_putnbr(h_time_now()); h_putchar(' '); h_putnbr(id); if (event == EVENT_FORK) diff --git a/common/helper.c b/common/helper.c index 2826ceb..7799a0c 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/02/14 21:07:54 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 00:53:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_one/fork.c b/philo_one/fork.c index ed1d642..3f91b35 100644 --- a/philo_one/fork.c +++ b/philo_one/fork.c @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */ -/* Updated: 2020/02/14 21:31:40 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 01:23:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -t_fork *forks_new(int num) +t_fork *forks_new(int num) { int i; t_fork *forks; @@ -22,7 +22,7 @@ t_fork *forks_new(int num) i = -1; while (++i < num) { - if (pthread_mutex_init(&forks[i].mutex, NULL) != 0) + if (pthread_mutex_init(&forks[i], NULL) != 0) { forks_destroy(forks, i + 1); return (NULL); @@ -31,22 +31,21 @@ t_fork *forks_new(int num) return (forks); } -void forks_destroy(t_fork *forks, int num) +void forks_destroy(t_fork *forks, int num) { while (num-- > 0) - { - forks[num].used = TRUE; - pthread_mutex_destroy(&forks[num].mutex); - } + pthread_mutex_destroy(&forks[num]); free(forks); } -t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args) +t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, + t_philo_args *args) { int i; t_routine_arg *routine_args; - if ((routine_args = (t_routine_arg*)malloc(sizeof(t_routine_arg) * args->philo_num)) == NULL) + if ((routine_args = (t_routine_arg*)malloc(sizeof(t_routine_arg) + * args->philo_num)) == NULL) return (NULL); i = -1; while (++i < args->philo_num) @@ -58,10 +57,3 @@ t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args } return (routine_args); } - -void fork_switch(t_fork *fork) -{ - pthread_mutex_lock(&fork->mutex); - fork->used = !fork->used; - pthread_mutex_unlock(&fork->mutex); -} diff --git a/philo_one/main.c b/philo_one/main.c index ddc9e52..abf9d9a 100644 --- a/philo_one/main.c +++ b/philo_one/main.c @@ -6,13 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/02/14 20:52:02 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 00:57:24 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" - int main(int argc, char **argv) { t_philo_args philo_args; diff --git a/philo_one/philo.c b/philo_one/philo.c index 84dfd7f..fffbc6c 100644 --- a/philo_one/philo.c +++ b/philo_one/philo.c @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/02/14 20:07:57 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 01:01:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -t_philo *philos_new(int num) +t_philo *philos_new(int num) { int i; t_philo *philos; @@ -46,7 +46,8 @@ t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) while (++i < num) { philos[i].alive = TRUE; - if (pthread_create(&philos[i].thread, NULL, &routine_philo, (void*)(routine_args + i)) == -1) + if (pthread_create(&philos[i].thread, NULL, + &routine_philo, (void*)(routine_args + i)) == -1) return (FALSE); } return (TRUE); diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h index d7c1aef..498cd49 100644 --- a/philo_one/philo_one.h +++ b/philo_one/philo_one.h @@ -6,12 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/02/14 21:37:45 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 01:25:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef PHILO_ONE -# define PHILO_ONE +#ifndef PHILO_ONE_H +# define PHILO_ONE_H # include <unistd.h> # include <sys/time.h> @@ -19,78 +19,60 @@ # include <pthread.h> # include "common.h" -typedef struct -{ - t_bool used; - pthread_mutex_t mutex; -} t_fork; - -// typedef struct -// { -// t_philo *philos; -// t_fork *forks; -// } t_table; - -typedef struct s_philo t_philo; +typedef pthread_mutex_t t_fork; -typedef struct +typedef struct s_philo { - t_philo *watched; - pthread_t thread; -} t_watchdog; - -struct s_philo -{ - int id; - t_bool alive; - t_watchdog watchdog; - t_time time_last_eat; - t_philo_state state; - pthread_t thread; -}; - -typedef struct + int id; + t_bool alive; + t_time time_last_eat; + t_philo_state state; + pthread_t thread; +} t_philo; + +typedef struct s_routine_arg { - t_philo_args *args; - t_philo *philo; - t_fork *fork_left; - t_fork *fork_right; - pthread_mutex_t *mutex_stdout; -} t_routine_arg; + t_philo_args *args; + t_philo *philo; + t_fork *fork_left; + t_fork *fork_right; + pthread_mutex_t *mutex_stdout; +} t_routine_arg; /* ** fork.c */ -t_fork *forks_new(int num); -void forks_destroy(t_fork *forks, int num); -t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args); -void fork_switch(t_fork *fork); +t_fork *forks_new(int num); +void forks_destroy(t_fork *forks, int num); +t_routine_arg *forks_dispatch(t_philo *philos, + t_fork *forks, t_philo_args *args); /* ** philo.c */ -t_philo *philos_new(int num); -void philos_destroy(t_philo *philos, int num); -t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num); -void philos_join(t_philo *philos, int num); -t_bool philos_starved(t_philo *philos, int num); +t_philo *philos_new(int num); +void philos_destroy(t_philo *philos, int num); +t_bool philos_start(t_philo *philos, + t_routine_arg *routine_args, int num); +void philos_join(t_philo *philos, int num); +t_bool philos_starved(t_philo *philos, int num); /* ** routine.c */ -void *routine_philo(void *void_arg); -void *routine_death(void *void_arg); +void *routine_philo(void *void_arg); +void *routine_death(void *void_arg); /* ** io.c */ -void io_eat(t_routine_arg *arg); -void io_think(t_routine_arg *arg); -void io_sleep(t_routine_arg *arg); -void io_die(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); +void io_die(t_routine_arg *arg); #endif diff --git a/philo_one/routine.c b/philo_one/routine.c index 7954174..89fe95e 100644 --- a/philo_one/routine.c +++ b/philo_one/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/02/14 21:43:37 by cacharle ### ########.fr */ +/* Updated: 2020/02/15 01:25:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,16 +25,15 @@ void *routine_philo(void *void_arg) return (NULL); io_think(arg); while (arg->args->all_alive) - if (!arg->fork_left->used && !arg->fork_right->used) - { - fork_switch(arg->fork_left); - fork_switch(arg->fork_right); - io_eat(arg); - fork_switch(arg->fork_left); - fork_switch(arg->fork_right); - io_sleep(arg); - io_think(arg); - } + { + pthread_mutex_lock(arg->fork_left); + pthread_mutex_lock(arg->fork_right); + io_eat(arg); + pthread_mutex_unlock(arg->fork_right); + pthread_mutex_unlock(arg->fork_left); + io_sleep(arg); + io_think(arg); + } pthread_join(thread_death, NULL); return (NULL); } diff --git a/philo_three/Makefile b/philo_three/Makefile index 8fe7c4d..431bf62 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,33 +6,40 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2019/11/24 07:07:21 by cacharle ### ########.fr # +# Updated: 2020/02/15 00:46:58 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f +COMMON_DIR = ../common + CC = gcc -CCFLAGS = -Wall -Wextra #-Werror -LDFLAGS = -lpthread +CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra #-Werror +LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon NAME = philo_three SRC = main.c OBJ = $(SRC:.c=.o) - all: $(NAME) -$(NAME): $(OBJ) +$(NAME): common_all $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) %.o: %.c - $(CC) $(CCFLAGS) -c -o $@ $^ + $(CC) $(CCFLAGS) -c -o $@ $< -clean: +clean: common_fclean $(RM) $(OBJ) fclean: clean $(RM) $(NAME) re: fclean all + +common_all: + make -C $(COMMON_DIR) all + +common_fclean: + make -C $(COMMON_DIR) fclean diff --git a/philo_three/child.c b/philo_three/child.c new file mode 100644 index 0000000..b2c570b --- /dev/null +++ b/philo_three/child.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* child.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 01:40:05 by cacharle #+# #+# */ +/* Updated: 2020/02/15 01:46:20 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +void child_start(t_philo_args *philo_args) +{ + pthread_t thread_death; + + io_think(arg); + if (!arg->args->all_alive) + return (NULL); + arg->time_last_eat = h_time_now(); + if (pthread_create(&thread_death, NULL, routine_death, arg) != 0) + return (NULL); + + while (arg->running) + { + sem_wait(arg->forks); + sem_wait(arg->forks); + io_eat(arg); + sem_post(arg->forks); + sem_post(arg->forks); + arg->time_last_eat = h_time_now(); + io_sleep(arg); + io_think(arg); + } + pthread_join(thread_death, NULL); + return (NULL); +} diff --git a/philo_three/main.c b/philo_three/main.c new file mode 100644 index 0000000..5c9b869 --- /dev/null +++ b/philo_three/main.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ +/* Updated: 2020/02/15 01:40:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +int main(int argc, char **argv) +{ + t_philo_args philo_args; + sem_t *forks; + + if (!parse_args(&philo_args, argc, argv)) + return (1); + forks = sem_open("philo_two", O_CREAT, 0700, philo_args.philo_num); + if (forks == SEM_FAILED) + return (1); + + i = -1; + while (++i < philo_args.philo_num) + { + pid = fork(); + if (pid == -1) + return (1); + if (pid == 0) + { + child_start(&philo_args); + return (0); + } + } + + sem_close(forks); + sem_unlink("philo_two"); + return (0); +} diff --git a/philo_three/philo_three.h b/philo_three/philo_three.h new file mode 100644 index 0000000..35e0cd3 --- /dev/null +++ b/philo_three/philo_three.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_three.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ +/* Updated: 2020/02/15 00:47:25 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHILO_THREE_H +# define PHILO_THREE_H + +# include <semaphore.h> +# include "common.h" + +#endif |
