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 --- philo_two/Makefile | 30 ++++++++------------ philo_two/args.c | 35 +++++++++++++++++++++++ philo_two/common.h | 67 ++++++++++++++++++++++++++++++++++++++++++++ philo_two/helper.c | 50 +++++++++++++++++++++++++++++++++ philo_two/io.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 246 insertions(+), 18 deletions(-) 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 (limited to 'philo_two') 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); +} -- cgit