From cc2adbf09541c0d9309d66f719c86e7fe5d351d7 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 3 Jan 2021 14:42:46 +0100 Subject: Updated philo directories for correction --- .gitignore | 9 ------ Makefile | 33 --------------------- README.md | 3 -- common/Makefile | 38 ------------------------ common/args.c | 35 ---------------------- common/common.h | 67 ----------------------------------------- common/helper.c | 50 ------------------------------- common/io.c | 82 --------------------------------------------------- correction.png | Bin 180674 -> 0 bytes philo_one/Makefile | 27 +++++++---------- philo_one/args.c | 35 ++++++++++++++++++++++ philo_one/common.h | 67 +++++++++++++++++++++++++++++++++++++++++ philo_one/helper.c | 50 +++++++++++++++++++++++++++++++ philo_one/io.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ philo_three/Makefile | 30 ++++++++----------- philo_three/args.c | 35 ++++++++++++++++++++++ philo_three/common.h | 67 +++++++++++++++++++++++++++++++++++++++++ philo_three/helper.c | 50 +++++++++++++++++++++++++++++++ philo_three/io.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ philo_two/Makefile | 30 ++++++++----------- philo_two/args.c | 35 ++++++++++++++++++++++ philo_two/common.h | 67 +++++++++++++++++++++++++++++++++++++++++ philo_two/helper.c | 50 +++++++++++++++++++++++++++++++ philo_two/io.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ subject.pdf | Bin 1271153 -> 0 bytes 25 files changed, 736 insertions(+), 370 deletions(-) delete mode 100644 .gitignore delete mode 100644 Makefile delete mode 100644 README.md delete mode 100644 common/Makefile delete mode 100644 common/args.c delete mode 100644 common/common.h delete mode 100644 common/helper.c delete mode 100644 common/io.c delete mode 100644 correction.png create mode 100644 philo_one/args.c create mode 100644 philo_one/common.h create mode 100644 philo_one/helper.c create mode 100644 philo_one/io.c create mode 100644 philo_three/args.c create mode 100644 philo_three/common.h create mode 100644 philo_three/helper.c create mode 100644 philo_three/io.c create mode 100644 philo_two/args.c create mode 100644 philo_two/common.h create mode 100644 philo_two/helper.c create mode 100644 philo_two/io.c delete mode 100644 subject.pdf diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 56a2394..0000000 --- a/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -*.o -*.ghc -*.a -*.dSYM -a.out -philo_one/philo_one -philo_two/philo_two -philo_three/philo_three -tags diff --git a/Makefile b/Makefile deleted file mode 100644 index 34f114d..0000000 --- a/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2020/02/09 03:31:28 by cacharle #+# #+# # -# Updated: 2020/12/30 13:48:29 by charles ### ########.fr # -# # -# **************************************************************************** # - -MAKE = make --no-print-directory - -PHILO_ONE_DIR = philo_one -PHILO_TWO_DIR = philo_two -PHILO_THREE_DIR = philo_three - -help: - @echo "make philo_one - compile philo_one" - @echo "make philo_two - compile philo_two" - @echo "make philo_three - compile philo_three" - -philo_one: - $(MAKE) -C $(PHILO_ONE_DIR) - -philo_two: - $(MAKE) -C $(PHILO_TWO_DIR) - -philo_three: - $(MAKE) -C $(PHILO_THREE_DIR) - -.PHONY: philo_one philo_two philo_three help diff --git a/README.md b/README.md deleted file mode 100644 index 7b9d21d..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# philosophers - -philosophers project of school 42 diff --git a/common/Makefile b/common/Makefile deleted file mode 100644 index d23cac9..0000000 --- a/common/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2021/01/02 10:47:14 by cacharle ### ########.fr # -# # -# **************************************************************************** # - -LIB = ar rcs -RM = rm -rf - -CC = gcc -CCFLAGS = -std=c99 -O2 -Wall -Wextra -Werror - -NAME = libphilocommon.a - -SRC = $(shell find . -type f -name "*.c") -OBJ = $(SRC:.c=.o) - -all: $(NAME) - -$(NAME): $(OBJ) - $(LIB) $(NAME) $(OBJ) - -%.o: %.c - $(CC) $(CCFLAGS) -c -o $@ $< - -clean: - $(RM) $(OBJ) - $(RM) $(NAME) - -re: clean all - -.PHONY: all clean re diff --git a/common/args.c b/common/args.c deleted file mode 100644 index 2070e46..0000000 --- a/common/args.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* args.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "common.h" - -bool parse_args(t_philo_args *args, int argc, char **argv) -{ - if (argc != 5 && argc != 6) - { - return (h_err(false, "Usage: %s philosophers_num death_timeout" - "eat_timeout sleep_timeout [meal_num]", argv[0])); - } - if ((args->philo_num = h_atou_strict(argv[1])) == -1 - || (args->timeout_death = h_atou_strict(argv[2])) == -1 - || (args->timeout_eat = h_atou_strict(argv[3])) == -1 - || (args->timeout_sleep = h_atou_strict(argv[4])) == -1) - return (false); - if (argc == 6) - { - if ((args->meal_num = h_atou_strict(argv[5])) == -1) - return (false); - } - else - args->meal_num = -1; - return (true); -} diff --git a/common/common.h b/common/common.h deleted file mode 100644 index 617db1a..0000000 --- a/common/common.h +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* common.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef COMMON_H -# define COMMON_H - -# include -# include -# include -# include -# include -# include - -typedef long int t_time; - -typedef enum e_philo_event -{ - EVENT_FORK, - EVENT_EAT, - EVENT_SLEEP, - EVENT_THINK, - EVENT_DIE -} t_philo_event; - -typedef struct -{ - long int philo_num; - t_time timeout_death; - t_time timeout_eat; - t_time timeout_sleep; - long int meal_num; -} t_philo_args; - -typedef void *(*t_routine)(void *arg); - -/* -** args.c -*/ - -bool parse_args(t_philo_args *args, int argc, char **argv); - -/* -** helper.c -*/ - -long int h_atou_strict(char *s); -t_time h_time_now(void); -int h_err(int ret, const char *format, char *str); - -/* -** io.c -*/ - -void philo_put_set_initial_time(void); -void philo_put( - size_t id, t_philo_event event, t_time initial_time); - -#endif diff --git a/common/helper.c b/common/helper.c deleted file mode 100644 index e1cc1e2..0000000 --- a/common/helper.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* helper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "common.h" - -long int h_atou_strict(char *s) -{ - long int num; - char *origin; - - origin = s; - if (*s < '0' || *s > '9') - return (h_err(-1, "Error: %s: is not a number", origin)); - num = 0; - while (*s >= '0' && *s <= '9') - { - num *= 10; - if (num > UINT_MAX) - return (h_err(-1, "Error: %s: is too big", origin)); - num += *s - '0'; - if (num > UINT_MAX) - return (h_err(-1, "Error: %s: is too big", origin)); - s++; - } - if (*s != '\0') - return (h_err(-1, "Error: %s: is not a number", origin)); - return (num); -} - -/* -** No need to check error of gettimeofday -** (only for settimeofday and passed pointer validity) -*/ - -t_time h_time_now(void) -{ - struct timeval tv; - - gettimeofday(&tv, NULL); - return (tv.tv_sec * 1000 + tv.tv_usec / 1000); -} diff --git a/common/io.c b/common/io.c deleted file mode 100644 index 5d50ccc..0000000 --- a/common/io.c +++ /dev/null @@ -1,82 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* io.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "common.h" - -static size_t st_strlen(char *s) -{ - int counter; - - counter = 0; - while (s[counter]) - counter++; - return (counter); -} - -static char *st_nbrcpy(char *dst, long long int num) -{ - if (num > 9) - dst = st_nbrcpy(dst, num / 10); - dst[0] = num % 10 + '0'; - dst[1] = '\0'; - return (dst + 1); -} - -static void st_strcat(char *dst, char *str) -{ - while (*dst != '\0') - dst++; - while (*str != '\0') - *dst++ = *str++; - *dst = '\0'; -} - -void philo_put(size_t id, t_philo_event event, t_time initial_time) -{ - static char buf[2048]; - - buf[0] = '\0'; - st_nbrcpy(buf, h_time_now() - initial_time); - st_strcat(buf, " "); - st_nbrcpy(buf + st_strlen(buf), id); - if (event == EVENT_FORK) - st_strcat(buf, " has taken fork\n"); - else if (event == EVENT_EAT) - st_strcat(buf, " is eating\n"); - else if (event == EVENT_SLEEP) - st_strcat(buf, " is sleeping\n"); - else if (event == EVENT_THINK) - st_strcat(buf, " is thinking\n"); - else if (event == EVENT_DIE) - st_strcat(buf, " died\n"); - write(STDOUT_FILENO, buf, st_strlen(buf)); -} - -int h_err(int ret, const char *format, char *str) -{ - while (*format != '\0') - { - if (format[0] == '%' && format[1] == 's') - { - if (str != NULL) - write(STDERR_FILENO, str, st_strlen(str)); - format += 2; - } - else - { - write(STDERR_FILENO, format, 1); - format++; - } - } - write(STDERR_FILENO, "\n", 1); - return (ret); -} diff --git a/correction.png b/correction.png deleted file mode 100644 index 3b1558e..0000000 Binary files a/correction.png and /dev/null differ diff --git a/philo_one/Makefile b/philo_one/Makefile index 0026516..44638f5 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,39 +6,38 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/01 15:18:11 by charles ### ########.fr # +# Updated: 2021/01/03 14:38:19 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f MAKE = make --no-print-directory -COMMONDIR = ../common - CC = gcc -CCFLAGS = -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra -Werror -LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon +CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror +LDFLAGS = -lpthread NAME = philo_one SRCDIR = src OBJDIR = obj -SRC = $(shell find $(SRCDIR) -type f -name '*.c') -OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +SRC = args.c helper.c io.c \ + src/event.c src/forks.c src/main.c src/philo.c src/routine.c +OBJ = $(SRC:%.c=%.o) all: prebuild $(NAME) prebuild: @mkdir -pv $(OBJDIR) -$(NAME): common_all $(OBJ) +$(NAME): $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) -$(OBJDIR)/%.o: $(SRCDIR)/%.c +%.o: %.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: common_clean +clean: $(RM) $(OBJ) fclean: clean @@ -46,10 +45,4 @@ fclean: clean re: fclean all -common_all: - $(MAKE) -C $(COMMONDIR) all - -common_clean: - $(MAKE) -C $(COMMONDIR) clean - -.PHONY: all prebuild clean fclean re common_all common_clean +.PHONY: all prebuild clean fclean re diff --git a/philo_one/args.c b/philo_one/args.c new file mode 100644 index 0000000..2070e46 --- /dev/null +++ b/philo_one/args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +bool parse_args(t_philo_args *args, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + { + return (h_err(false, "Usage: %s philosophers_num death_timeout" + "eat_timeout sleep_timeout [meal_num]", argv[0])); + } + if ((args->philo_num = h_atou_strict(argv[1])) == -1 + || (args->timeout_death = h_atou_strict(argv[2])) == -1 + || (args->timeout_eat = h_atou_strict(argv[3])) == -1 + || (args->timeout_sleep = h_atou_strict(argv[4])) == -1) + return (false); + if (argc == 6) + { + if ((args->meal_num = h_atou_strict(argv[5])) == -1) + return (false); + } + else + args->meal_num = -1; + return (true); +} diff --git a/philo_one/common.h b/philo_one/common.h new file mode 100644 index 0000000..617db1a --- /dev/null +++ b/philo_one/common.h @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_H +# define COMMON_H + +# include +# include +# include +# include +# include +# include + +typedef long int t_time; + +typedef enum e_philo_event +{ + EVENT_FORK, + EVENT_EAT, + EVENT_SLEEP, + EVENT_THINK, + EVENT_DIE +} t_philo_event; + +typedef struct +{ + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; +} t_philo_args; + +typedef void *(*t_routine)(void *arg); + +/* +** args.c +*/ + +bool parse_args(t_philo_args *args, int argc, char **argv); + +/* +** helper.c +*/ + +long int h_atou_strict(char *s); +t_time h_time_now(void); +int h_err(int ret, const char *format, char *str); + +/* +** io.c +*/ + +void philo_put_set_initial_time(void); +void philo_put( + size_t id, t_philo_event event, t_time initial_time); + +#endif diff --git a/philo_one/helper.c b/philo_one/helper.c new file mode 100644 index 0000000..e1cc1e2 --- /dev/null +++ b/philo_one/helper.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +long int h_atou_strict(char *s) +{ + long int num; + char *origin; + + origin = s; + if (*s < '0' || *s > '9') + return (h_err(-1, "Error: %s: is not a number", origin)); + num = 0; + while (*s >= '0' && *s <= '9') + { + num *= 10; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + num += *s - '0'; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + s++; + } + if (*s != '\0') + return (h_err(-1, "Error: %s: is not a number", origin)); + return (num); +} + +/* +** No need to check error of gettimeofday +** (only for settimeofday and passed pointer validity) +*/ + +t_time h_time_now(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 + tv.tv_usec / 1000); +} diff --git a/philo_one/io.c b/philo_one/io.c new file mode 100644 index 0000000..5d50ccc --- /dev/null +++ b/philo_one/io.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +static size_t st_strlen(char *s) +{ + int counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} + +static char *st_nbrcpy(char *dst, long long int num) +{ + if (num > 9) + dst = st_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + dst[1] = '\0'; + return (dst + 1); +} + +static void st_strcat(char *dst, char *str) +{ + while (*dst != '\0') + dst++; + while (*str != '\0') + *dst++ = *str++; + *dst = '\0'; +} + +void philo_put(size_t id, t_philo_event event, t_time initial_time) +{ + static char buf[2048]; + + buf[0] = '\0'; + st_nbrcpy(buf, h_time_now() - initial_time); + st_strcat(buf, " "); + st_nbrcpy(buf + st_strlen(buf), id); + if (event == EVENT_FORK) + st_strcat(buf, " has taken fork\n"); + else if (event == EVENT_EAT) + st_strcat(buf, " is eating\n"); + else if (event == EVENT_SLEEP) + st_strcat(buf, " is sleeping\n"); + else if (event == EVENT_THINK) + st_strcat(buf, " is thinking\n"); + else if (event == EVENT_DIE) + st_strcat(buf, " died\n"); + write(STDOUT_FILENO, buf, st_strlen(buf)); +} + +int h_err(int ret, const char *format, char *str) +{ + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + if (str != NULL) + write(STDERR_FILENO, str, st_strlen(str)); + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + write(STDERR_FILENO, "\n", 1); + return (ret); +} diff --git a/philo_three/Makefile b/philo_three/Makefile index 17c5b46..050e2d5 100644 --- a/philo_three/Makefile +++ b/philo_three/Makefile @@ -6,39 +6,39 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/01 16:28:33 by charles ### ########.fr # +# Updated: 2021/01/03 14:40:14 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f MAKE = make --no-print-directory -COMMON_DIR = ../common - CC = gcc -CCFLAGS = -I$(COMMON_DIR) -O2 -std=c99 -Wall -Wextra #-Werror -LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon +CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror +LDFLAGS = -lpthread NAME = philo_three SRCDIR = src OBJDIR = obj -SRC = $(shell find $(SRCDIR) -type f -name '*.c') -OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +SRC = args.c helper.c io.c \ + src/child.c src/event.c src/main.c + +OBJ = $(SRC:%.c=%.o) all: prebuild $(NAME) prebuild: - @mkdir -p $(OBJDIR) + @mkdir -pv $(OBJDIR) -$(NAME): common_all $(OBJ) +$(NAME): $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) -$(OBJDIR)/%.o: $(SRCDIR)/%.c +%.o: %.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: common_clean +clean: $(RM) $(OBJ) fclean: clean @@ -46,10 +46,4 @@ fclean: clean re: fclean all -common_all: - $(MAKE) -C $(COMMON_DIR) all - -common_clean: - $(MAKE) -C $(COMMON_DIR) clean - -.PHONY: all prebuild clean fclean re common_all common_clean +.PHONY: all prebuild clean fclean re diff --git a/philo_three/args.c b/philo_three/args.c new file mode 100644 index 0000000..2070e46 --- /dev/null +++ b/philo_three/args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +bool parse_args(t_philo_args *args, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + { + return (h_err(false, "Usage: %s philosophers_num death_timeout" + "eat_timeout sleep_timeout [meal_num]", argv[0])); + } + if ((args->philo_num = h_atou_strict(argv[1])) == -1 + || (args->timeout_death = h_atou_strict(argv[2])) == -1 + || (args->timeout_eat = h_atou_strict(argv[3])) == -1 + || (args->timeout_sleep = h_atou_strict(argv[4])) == -1) + return (false); + if (argc == 6) + { + if ((args->meal_num = h_atou_strict(argv[5])) == -1) + return (false); + } + else + args->meal_num = -1; + return (true); +} diff --git a/philo_three/common.h b/philo_three/common.h new file mode 100644 index 0000000..617db1a --- /dev/null +++ b/philo_three/common.h @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_H +# define COMMON_H + +# include +# include +# include +# include +# include +# include + +typedef long int t_time; + +typedef enum e_philo_event +{ + EVENT_FORK, + EVENT_EAT, + EVENT_SLEEP, + EVENT_THINK, + EVENT_DIE +} t_philo_event; + +typedef struct +{ + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; +} t_philo_args; + +typedef void *(*t_routine)(void *arg); + +/* +** args.c +*/ + +bool parse_args(t_philo_args *args, int argc, char **argv); + +/* +** helper.c +*/ + +long int h_atou_strict(char *s); +t_time h_time_now(void); +int h_err(int ret, const char *format, char *str); + +/* +** io.c +*/ + +void philo_put_set_initial_time(void); +void philo_put( + size_t id, t_philo_event event, t_time initial_time); + +#endif diff --git a/philo_three/helper.c b/philo_three/helper.c new file mode 100644 index 0000000..e1cc1e2 --- /dev/null +++ b/philo_three/helper.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +long int h_atou_strict(char *s) +{ + long int num; + char *origin; + + origin = s; + if (*s < '0' || *s > '9') + return (h_err(-1, "Error: %s: is not a number", origin)); + num = 0; + while (*s >= '0' && *s <= '9') + { + num *= 10; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + num += *s - '0'; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + s++; + } + if (*s != '\0') + return (h_err(-1, "Error: %s: is not a number", origin)); + return (num); +} + +/* +** No need to check error of gettimeofday +** (only for settimeofday and passed pointer validity) +*/ + +t_time h_time_now(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 + tv.tv_usec / 1000); +} diff --git a/philo_three/io.c b/philo_three/io.c new file mode 100644 index 0000000..5d50ccc --- /dev/null +++ b/philo_three/io.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +static size_t st_strlen(char *s) +{ + int counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} + +static char *st_nbrcpy(char *dst, long long int num) +{ + if (num > 9) + dst = st_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + dst[1] = '\0'; + return (dst + 1); +} + +static void st_strcat(char *dst, char *str) +{ + while (*dst != '\0') + dst++; + while (*str != '\0') + *dst++ = *str++; + *dst = '\0'; +} + +void philo_put(size_t id, t_philo_event event, t_time initial_time) +{ + static char buf[2048]; + + buf[0] = '\0'; + st_nbrcpy(buf, h_time_now() - initial_time); + st_strcat(buf, " "); + st_nbrcpy(buf + st_strlen(buf), id); + if (event == EVENT_FORK) + st_strcat(buf, " has taken fork\n"); + else if (event == EVENT_EAT) + st_strcat(buf, " is eating\n"); + else if (event == EVENT_SLEEP) + st_strcat(buf, " is sleeping\n"); + else if (event == EVENT_THINK) + st_strcat(buf, " is thinking\n"); + else if (event == EVENT_DIE) + st_strcat(buf, " died\n"); + write(STDOUT_FILENO, buf, st_strlen(buf)); +} + +int h_err(int ret, const char *format, char *str) +{ + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + if (str != NULL) + write(STDERR_FILENO, str, st_strlen(str)); + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + write(STDERR_FILENO, "\n", 1); + return (ret); +} diff --git a/philo_two/Makefile b/philo_two/Makefile index 88c0f8b..224809b 100644 --- a/philo_two/Makefile +++ b/philo_two/Makefile @@ -6,39 +6,39 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2021/01/02 11:27:33 by cacharle ### ########.fr # +# Updated: 2021/01/03 14:39:09 by cacharle ### ########.fr # # # # **************************************************************************** # RM = rm -f MAKE = make --no-print-directory -COMMONDIR = ../common - CC = gcc -CCFLAGS = -g -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra #-Werror -LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon +CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror +LDFLAGS = -lpthread NAME = philo_two SRCDIR = src OBJDIR = obj -SRC = $(shell find $(SRCDIR) -type f -name '*.c') -OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +SRC = args.c helper.c io.c \ + src/event.c src/main.c src/routine.c + +OBJ = $(SRC:%.c=%.o) all: prebuild $(NAME) prebuild: - @mkdir -p $(OBJDIR) + @mkdir -pv $(OBJDIR) -$(NAME): common_all $(OBJ) +$(NAME): $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) -$(OBJDIR)/%.o: $(SRCDIR)/%.c +%.o: %.c $(CC) $(CCFLAGS) -c -o $@ $< -clean: common_clean +clean: $(RM) $(OBJ) fclean: clean @@ -46,10 +46,4 @@ fclean: clean re: fclean all -common_all: - $(MAKE) -C $(COMMONDIR) all - -common_clean: - $(MAKE) -C $(COMMONDIR) clean - -.PHONY: all prebuild clean fclean re common_all common_clean +.PHONY: all prebuild clean fclean re diff --git a/philo_two/args.c b/philo_two/args.c new file mode 100644 index 0000000..2070e46 --- /dev/null +++ b/philo_two/args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +bool parse_args(t_philo_args *args, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + { + return (h_err(false, "Usage: %s philosophers_num death_timeout" + "eat_timeout sleep_timeout [meal_num]", argv[0])); + } + if ((args->philo_num = h_atou_strict(argv[1])) == -1 + || (args->timeout_death = h_atou_strict(argv[2])) == -1 + || (args->timeout_eat = h_atou_strict(argv[3])) == -1 + || (args->timeout_sleep = h_atou_strict(argv[4])) == -1) + return (false); + if (argc == 6) + { + if ((args->meal_num = h_atou_strict(argv[5])) == -1) + return (false); + } + else + args->meal_num = -1; + return (true); +} diff --git a/philo_two/common.h b/philo_two/common.h new file mode 100644 index 0000000..617db1a --- /dev/null +++ b/philo_two/common.h @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_H +# define COMMON_H + +# include +# include +# include +# include +# include +# include + +typedef long int t_time; + +typedef enum e_philo_event +{ + EVENT_FORK, + EVENT_EAT, + EVENT_SLEEP, + EVENT_THINK, + EVENT_DIE +} t_philo_event; + +typedef struct +{ + long int philo_num; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; + long int meal_num; +} t_philo_args; + +typedef void *(*t_routine)(void *arg); + +/* +** args.c +*/ + +bool parse_args(t_philo_args *args, int argc, char **argv); + +/* +** helper.c +*/ + +long int h_atou_strict(char *s); +t_time h_time_now(void); +int h_err(int ret, const char *format, char *str); + +/* +** io.c +*/ + +void philo_put_set_initial_time(void); +void philo_put( + size_t id, t_philo_event event, t_time initial_time); + +#endif diff --git a/philo_two/helper.c b/philo_two/helper.c new file mode 100644 index 0000000..e1cc1e2 --- /dev/null +++ b/philo_two/helper.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +long int h_atou_strict(char *s) +{ + long int num; + char *origin; + + origin = s; + if (*s < '0' || *s > '9') + return (h_err(-1, "Error: %s: is not a number", origin)); + num = 0; + while (*s >= '0' && *s <= '9') + { + num *= 10; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + num += *s - '0'; + if (num > UINT_MAX) + return (h_err(-1, "Error: %s: is too big", origin)); + s++; + } + if (*s != '\0') + return (h_err(-1, "Error: %s: is not a number", origin)); + return (num); +} + +/* +** No need to check error of gettimeofday +** (only for settimeofday and passed pointer validity) +*/ + +t_time h_time_now(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 + tv.tv_usec / 1000); +} diff --git a/philo_two/io.c b/philo_two/io.c new file mode 100644 index 0000000..5d50ccc --- /dev/null +++ b/philo_two/io.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +static size_t st_strlen(char *s) +{ + int counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} + +static char *st_nbrcpy(char *dst, long long int num) +{ + if (num > 9) + dst = st_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + dst[1] = '\0'; + return (dst + 1); +} + +static void st_strcat(char *dst, char *str) +{ + while (*dst != '\0') + dst++; + while (*str != '\0') + *dst++ = *str++; + *dst = '\0'; +} + +void philo_put(size_t id, t_philo_event event, t_time initial_time) +{ + static char buf[2048]; + + buf[0] = '\0'; + st_nbrcpy(buf, h_time_now() - initial_time); + st_strcat(buf, " "); + st_nbrcpy(buf + st_strlen(buf), id); + if (event == EVENT_FORK) + st_strcat(buf, " has taken fork\n"); + else if (event == EVENT_EAT) + st_strcat(buf, " is eating\n"); + else if (event == EVENT_SLEEP) + st_strcat(buf, " is sleeping\n"); + else if (event == EVENT_THINK) + st_strcat(buf, " is thinking\n"); + else if (event == EVENT_DIE) + st_strcat(buf, " died\n"); + write(STDOUT_FILENO, buf, st_strlen(buf)); +} + +int h_err(int ret, const char *format, char *str) +{ + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + if (str != NULL) + write(STDERR_FILENO, str, st_strlen(str)); + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + write(STDERR_FILENO, "\n", 1); + return (ret); +} diff --git a/subject.pdf b/subject.pdf deleted file mode 100644 index d80790b..0000000 Binary files a/subject.pdf and /dev/null differ -- cgit