aboutsummaryrefslogtreecommitdiff
path: root/philo_three
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-03 14:42:46 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-03 14:47:11 +0100
commitcc2adbf09541c0d9309d66f719c86e7fe5d351d7 (patch)
treef9d61b11e57dcdfebc9092554df1a64d0d4958c5 /philo_three
parente874ad94a31a8259b8eb7ad2865767c081bcd279 (diff)
downloadphilosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.tar.gz
philosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.tar.bz2
philosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.zip
Updated philo directories for correctionrendu
Diffstat (limited to 'philo_three')
-rw-r--r--philo_three/Makefile30
-rw-r--r--philo_three/args.c35
-rw-r--r--philo_three/common.h67
-rw-r--r--philo_three/helper.c50
-rw-r--r--philo_three/io.c82
5 files changed, 246 insertions, 18 deletions
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 <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <unistd.h>
+# include <stdlib.h>
+# include <stdbool.h>
+# include <pthread.h>
+# include <sys/time.h>
+# include <limits.h>
+
+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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}