From f57ec76fd1be738d7b9d82c1f7548883efa15d0c Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 14:48:12 +0200 Subject: Added philo_three draft --- common/common.h | 2 +- philo_three/Makefile | 28 +++++++++++++------- philo_three/child.c | 39 --------------------------- philo_three/main.c | 42 ----------------------------- philo_three/philo_three.h | 19 -------------- philo_three/src/child.c | 53 +++++++++++++++++++++++++++++++++++++ philo_three/src/event.c | 61 +++++++++++++++++++++++++++++++++++++++++++ philo_three/src/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++ philo_three/src/philo_three.h | 45 +++++++++++++++++++++++++++++++ philo_two/Makefile | 6 ++--- 10 files changed, 243 insertions(+), 113 deletions(-) delete mode 100644 philo_three/child.c delete mode 100644 philo_three/main.c delete mode 100644 philo_three/philo_three.h create mode 100644 philo_three/src/child.c create mode 100644 philo_three/src/event.c create mode 100644 philo_three/src/main.c create mode 100644 philo_three/src/philo_three.h diff --git a/common/common.h b/common/common.h index cb71172..f810b6f 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/09/30 10:15:10 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 10:36:03 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_three/Makefile b/philo_three/Makefile index 431bf62..6428673 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,11 +6,12 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/02/15 00:46:58 by cacharle ### ########.fr # +# Updated: 2020/09/30 08:48:37 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f +MAKE = --no-print-directory COMMON_DIR = ../common @@ -19,18 +20,25 @@ 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) +SRCDIR = src +OBJDIR = obj + +SRC = $(shell find $(SRCDIR) -type f -name '*.c') +OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) + +all: prebuild $(NAME) + +prebuild: + @mkdir -p $(OBJDIR) $(NAME): common_all $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) -%.o: %.c +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: common_fclean +clean: common_clean $(RM) $(OBJ) fclean: clean @@ -39,7 +47,9 @@ fclean: clean re: fclean all common_all: - make -C $(COMMON_DIR) all + $(MAKE) -C $(COMMON_DIR) all + +common_clean: + $(MAKE) -C $(COMMON_DIR) clean -common_fclean: - make -C $(COMMON_DIR) fclean +.PHONY: all prebuild clean fclean re common_all common_clean diff --git a/philo_three/child.c b/philo_three/child.c deleted file mode 100644 index b2c570b..0000000 --- a/philo_three/child.c +++ /dev/null @@ -1,39 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* child.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 deleted file mode 100644 index 5c9b869..0000000 --- a/philo_three/main.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 deleted file mode 100644 index 35e0cd3..0000000 --- a/philo_three/philo_three.h +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* philo_three.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 -# include "common.h" - -#endif diff --git a/philo_three/src/child.c b/philo_three/src/child.c new file mode 100644 index 0000000..f0e7ebd --- /dev/null +++ b/philo_three/src/child.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* child.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ +/* Updated: 2020/09/30 14:39:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +void *routine_death(t_philo_args *arg) +{ + t_time current; + + current = h_time_now(); + while (current - arg->time_last_eat < arg->conf->timeout_death) + current = h_time_now(); + if (!arg->conf->all_alive) + return (NULL); + event_die(arg); + return (NULL); +} + +pid_t child_start(t_philo_args *arg) +{ + pid_t pid; + sem_t *forks; + sem_t *sem_stdout; + sem_t *sem_alive; + + pid = fork(); + if (pid == -1) + return (-1); + if (pid == 0) + { + forks = sem_open(PHILO_SEM_NAME, 0); + sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); + pthread_create(&death_thread, NULL, (t_routine)routine_death, &args); + event_think(); + while (true) + { + event_eat(); + event_sleep(); + event_think(); + } + exit(0); + } + return pid; +} diff --git a/philo_three/src/event.c b/philo_three/src/event.c new file mode 100644 index 0000000..9ff9012 --- /dev/null +++ b/philo_three/src/event.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* event.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ +/* Updated: 2020/09/30 14:47:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_two.h" + +void event_take_fork(t_philo *arg) +{ + sem_wait(arg->forks); + sem_wait(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_FORK); + sem_post(&arg->conf->mutex_stdout); +} + +void event_eat(t_philo *arg) +{ + int eat_counter; + + eat_counter = 0; + while (eat_counter < arg->conf->meal_num) + { + sem_wait(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_EAT); + sem_post(&arg->conf->mutex_stdout); + usleep(arg->conf->timeout_eat * 1000); + eat_counter++; + } +} + +void event_think(t_philo *arg) +{ + sem_wait(&arg->conf->mutex_stdout); + philo_put(arg->id, EVENT_THINK); + sem_post(&arg->conf->mutex_stdout); +} + +void event_sleep(int id, t_time timeout_sleep, sem_t *sem_stdout, sem_t ) +{ + sem_wait(sem_stdout); + philo_put(id, EVENT_SLEEP); + sem_post(sem_stdout); + sem_post(arg->forks); + sem_post(arg->forks); + usleep(arg->conf->timeout_sleep * 1000); +} + +void event_die(int id, sem_t *sem_stdout, sem_t *sem_dead) +{ + sem_wait(sem_stdout); + philo_put(arg->id, EVENT_DIE); + arg->conf->all_alive = false; + sem_post(sem_dead); +} diff --git a/philo_three/src/main.c b/philo_three/src/main.c new file mode 100644 index 0000000..a69766f --- /dev/null +++ b/philo_three/src/main.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ +/* Updated: 2020/09/30 14:45:20 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_three.h" + +int main(int argc, char **argv) +{ + t_philo_args args; + sem_t *forks; + sem_t *sem_stdout; + sem_t *sem_dead; + + if (!parse_args(&args, argc, argv)) + return (1); + + sem_unlink(PHILO_SEM_NAME); + forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0700, args.philo_num); + if (forks == SEM_FAILED) + return (1); + + sem_unlink(PHILO_SEM_STDOUT_NAME); + sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, O_CREAT | O_EXCL, 0700, 1); + if (sem_stdout == SEM_FAILED) + return (1); + + sem_unlink(PHILO_SEM_ALIVE_NAME); + sem_dead = sem_open(PHILO_SEM_ALIVE_NAME, O_CREAT | O_EXCL, 0700, 1); + if (sem_dead == SEM_FAILED) + return (1); + + pid_t pid; + pid_t *pids; + pids = malloc(sizeof(pid_t) * args.philo_num); + + int i = -1; + while (++i < args.philo_num) + pids[i] = child_start(&args); + + sem_wait(sem_dead); + + i = -1; + while (++i < args.philo_num) + kill(pids[i], SIGKILL); + free(pids); + + sem_close(forks); + sem_close(sem_stdout); + sem_unlink(PHILO_SEM_NAME); + sem_unlink(PHILO_SEM_STDOUT_NAME); + return (0); +} + diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h new file mode 100644 index 0000000..97882d0 --- /dev/null +++ b/philo_three/src/philo_three.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_three.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ +/* Updated: 2020/09/30 14:47:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHILO_THREE_H +# define PHILO_THREE_H + +# include +# include +# include +# include + +# include "common.h" + +# define PHILO_SEM_NAME "semaphore_philo_three" +# define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout" +# define PHILO_SEM_ALIVE_NAME "semaphore_philo_three_alive" + +typedef struct +{ + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; + +} t_philo_conf; + +pid_t child_start(t_philo_args *arg); + +void event_take_fork(t_philo *arg); +void event_eat(t_philo *arg); +void event_think(t_philo *arg); +void event_sleep(t_philo *arg); +void event_die(t_philo *arg); + +#endif diff --git a/philo_two/Makefile b/philo_two/Makefile index 4561afc..c4084a7 100644 --- a/philo_two/Makefile +++ b/philo_two/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/30 08:23:03 by cacharle ### ########.fr # +# Updated: 2020/09/30 10:37:38 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -27,12 +27,12 @@ OBJDIR = obj SRC = $(shell find $(SRCDIR) -type f -name '*.c') OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) -all: prebuild common_all $(NAME) +all: prebuild $(NAME) prebuild: @mkdir -p $(OBJDIR) -$(NAME): $(OBJ) +$(NAME): common_all $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) $(OBJDIR)/%.o: $(SRCDIR)/%.c -- cgit