aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-30 07:59:27 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-30 07:59:27 +0200
commitd7aea773431926cefb6430b948329da1662d1dee (patch)
tree984fd10acf619ee8da84e7111742399a55a02770
parentb6f4db572d7d40c178ec286373422faa2172f135 (diff)
downloadphilosophers-d7aea773431926cefb6430b948329da1662d1dee.tar.gz
philosophers-d7aea773431926cefb6430b948329da1662d1dee.tar.bz2
philosophers-d7aea773431926cefb6430b948329da1662d1dee.zip
Refactoring common lib
-rw-r--r--common/common.c34
-rw-r--r--common/common.h55
-rw-r--r--common/helper.c81
-rw-r--r--common/philo.c37
-rw-r--r--philo_one/Makefile18
-rw-r--r--philo_one/src/args.c32
-rw-r--r--philo_one/src/helper.c99
-rw-r--r--philo_one/src/io.c34
-rw-r--r--philo_one/src/philo_one.h50
9 files changed, 119 insertions, 321 deletions
diff --git a/common/common.c b/common/common.c
index face0b5..62fd382 100644
--- a/common/common.c
+++ b/common/common.c
@@ -6,44 +6,44 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/02/15 00:54:03 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:56:12 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-t_bool parse_args(t_philo_args *philo_args, int argc, char **argv)
+bool parse_args(t_philo_args *args, int argc, char **argv)
{
if (argc != 5 && argc != 6)
- return (FALSE);
- if ((philo_args->philo_num = h_strtoposint(argv[1])) == -1 ||
- (philo_args->timeout_death = h_strtoposint(argv[2])) == -1 ||
- (philo_args->timeout_eat = h_strtoposint(argv[3])) == -1 ||
- (philo_args->timeout_sleep = h_strtoposint(argv[4])) == -1)
- return (FALSE);
+ 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 ((philo_args->meal_num = h_strtoposint(argv[5])) == -1)
- return (FALSE);
+ if ((args->meal_num = h_atou_strict(argv[5])) == -1)
+ return (false);
}
else
- philo_args->meal_num = -1;
- return (TRUE);
+ args->meal_num = 1;
+ return (true);
}
-void philo_put_state_change(int id, t_philo_event event) // not correct for philo3
+void philo_put(size_t id, t_philo_event event)
{
h_putnbr(h_time_now());
h_putchar(' ');
h_putnbr(id);
if (event == EVENT_FORK)
h_putstr(" has taken fork\n");
- else if (event == EVENT_EATING)
+ else if (event == EVENT_EAT)
h_putstr(" is eating\n");
- else if (event == EVENT_SLEEPING)
+ else if (event == EVENT_SLEEP)
h_putstr(" is sleeping\n");
- else if (event == EVENT_THINKING)
+ else if (event == EVENT_THINK)
h_putstr(" is thinking\n");
- else if (event == EVENT_DIED)
+ else if (event == EVENT_DIE)
h_putstr(" died\n");
}
diff --git a/common/common.h b/common/common.h
index a838750..b6f21c2 100644
--- a/common/common.h
+++ b/common/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
-/* Updated: 2020/02/14 20:45:33 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:57:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,73 +15,50 @@
# include <unistd.h>
# include <stdlib.h>
+# include <stdbool.h>
# include <pthread.h>
# include <sys/time.h>
+# include <limits.h>
-# define FALSE 0
-# define TRUE 1
-
-typedef int t_bool;
typedef long int t_time;
typedef enum
{
- PSTATE_EATING = 0,
- PSTATE_SLEEPING,
- PSTATE_THINKING,
- PSTATE_NUM
-} t_philo_state;
-
-typedef enum
-{
EVENT_FORK,
- EVENT_EATING,
- EVENT_SLEEPING,
- EVENT_THINKING,
- EVENT_DIED
-} t_philo_event;
+ EVENT_EAT,
+ EVENT_SLEEP,
+ EVENT_THINK,
+ EVENT_DIE
+} t_philo_event;
typedef struct
{
- int philo_num;
+ long int philo_num;
t_time timeout_death;
t_time timeout_eat;
t_time timeout_sleep;
- int meal_num;
- t_bool all_alive;
- pthread_mutex_t mutex_stdout;
- pthread_mutex_t mutex_all_alive;
+ long int meal_num;
} t_philo_args;
-typedef void (*t_philo_routine)(void *arg);
+typedef void (*t_routine)(void *arg);
/*
** common.c
*/
-t_bool parse_args(t_philo_args *philo_args, int argc, char **argv);
-void philo_put_state_change(int id, t_philo_event event);
-
-/*
-** philo.c
-*/
-
-void philo_eat(int id, t_time timeout);
-void philo_sleep(int id, t_time timeout);
-void philo_think(int id);
-void philo_die(int id);
+bool parse_args(t_philo_args *args, int argc, char **argv);
+void philo_put(size_t id, t_philo_event event);
/*
** helper.c
*/
-long int h_strtoposint(char *s);
-int h_strlen(char *s);
+long int h_atou_strict(char *s);
+size_t h_strlen(char *s);
void h_putnbr(unsigned long num);
void h_putchar(char c);
void h_putstr(char *s);
-void *h_calloc(int count, int size);
-t_time h_timeval_to_time(struct timeval *tp);
t_time h_time_now(void);
+int h_err(int ret, const char *format, char *str);
#endif
diff --git a/common/helper.c b/common/helper.c
index 7799a0c..61b85a1 100644
--- a/common/helper.c
+++ b/common/helper.c
@@ -6,83 +6,88 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/02/15 00:53:47 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:57:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-long int h_strtoposint(char *s)
+size_t h_strlen(char *s)
+{
+ int counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
+
+long int h_atou_strict(char *s)
{
long int num;
+ char *origin;
+ origin = s;
if (*s < '0' || *s > '9')
- return (-1);
+ 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 (-1);
- return num;
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ return (num);
}
-int h_strlen(char *s)
-{
- int counter;
-
- counter = 0;
- while (s[counter])
- counter++;
- return (counter);
-}
-
-void h_putnbr(unsigned long int num)
+void h_putnbr(unsigned long int num)
{
if (num > 9)
h_putnbr(num / 10);
h_putchar(num % 10 + '0');
}
-void h_putchar(char c)
+void h_putchar(char c)
{
write(STDOUT_FILENO, &c, 1);
}
-void h_putstr(char *s)
+void h_putstr(char *s)
{
write(STDOUT_FILENO, s, h_strlen(s));
}
-void *h_calloc(int count, int size)
+int h_err(int ret, const char *format, char *str)
{
- int i;
- void *ptr;
-
- if ((ptr = malloc(count * size)) == NULL)
- return (NULL);
- i = count * size;
- while (i-- > 0)
- ((unsigned char*)ptr)[i] = 0x0;
- return (ptr);
-}
-
-t_time h_timeval_to_time(struct timeval *tp)
-{
- t_time t;
-
- t = tp->tv_sec * 1000 + tp->tv_usec / 1000;
- return (t);
+ while (*format != '\0')
+ {
+ if (format[0] == '%' && format[1] == 's')
+ {
+ if (str != NULL)
+ write(STDERR_FILENO, str, h_strlen(str));
+ format += 2;
+ }
+ else
+ {
+ write(STDERR_FILENO, format, 1);
+ format++;
+ }
+ }
+ write(STDERR_FILENO, "\n", 1);
+ return (ret);
}
-t_time h_time_now(void)
+t_time h_time_now(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
return (-1);
- return (h_timeval_to_time(&tv));
+ return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
diff --git a/common/philo.c b/common/philo.c
deleted file mode 100644
index d342802..0000000
--- a/common/philo.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* philo.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */
-/* Updated: 2020/02/14 21:21:02 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "common.h"
-
-void philo_eat(int id, t_time timeout)
-{
- philo_put_state_change(id, EVENT_EATING);
- usleep(timeout * 1000);
-}
-
-void philo_sleep(int id, t_time timeout)
-{
- philo_put_state_change(id, EVENT_SLEEPING);
- usleep(timeout * 1000);
-}
-
-void philo_think(int id)
-{
- philo_put_state_change(id, EVENT_THINKING);
-}
-
-void philo_die(int id)
-{
- philo_put_state_change(id, EVENT_DIED);
-}
-
-
diff --git a/philo_one/Makefile b/philo_one/Makefile
index da3f82e..091bd56 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,15 +6,15 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2020/09/29 11:30:07 by cacharle ### ########.fr #
+# Updated: 2020/09/30 07:45:51 by cacharle ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
CC = gcc
-CCFLAGS = -Wall -Wextra #-Werror
-LDFLAGS = -lpthread
+CCFLAGS = -I$(COMMONDIR) -Wall -Wextra #-Werror
+LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
NAME = philo_one
@@ -24,7 +24,7 @@ OBJDIR = obj
SRC = $(shell find $(SRCDIR) -type f -name '*.c')
OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
-all: prebuild $(NAME)
+all: prebuild common_all $(NAME)
prebuild:
@mkdir -pv $(OBJDIR)
@@ -35,7 +35,7 @@ $(NAME): $(OBJ)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CCFLAGS) -c -o $@ $<
-clean:
+clean: common_clean
$(RM) $(OBJ)
fclean: clean
@@ -43,4 +43,10 @@ fclean: clean
re: fclean all
-.PHONY: all prebuild clean fclean re
+common_all:
+ $(MAKE) -C $(COMMONDIR) all
+
+common_clean:
+ $(MAKE) -C $(COMMONDIR) clean
+
+.PHONY: all prebuild clean fclean re common_all common_clean
diff --git a/philo_one/src/args.c b/philo_one/src/args.c
deleted file mode 100644
index b7277dd..0000000
--- a/philo_one/src/args.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* args.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/09/29 11:08:06 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "philo_one.h"
-
-bool parse_args(t_philo_conf *philo_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 ((philo_args->philo_num = h_atou_strict(argv[1])) == -1
- || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1
- || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1
- || (philo_args->timeout_sleep = h_atou_strict(argv[4])) == -1)
- return (false);
- if (argc == 6)
- {
- if ((philo_args->meal_num = h_atou_strict(argv[5])) == -1)
- return (false);
- }
- else
- philo_args->meal_num = 1;
- return (true);
-}
diff --git a/philo_one/src/helper.c b/philo_one/src/helper.c
deleted file mode 100644
index fa4c7ff..0000000
--- a/philo_one/src/helper.c
+++ /dev/null
@@ -1,99 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* helper.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/09/29 14:15:50 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "philo_one.h"
-
-static int h_strlen(char *s)
-{
- int counter;
-
- counter = 0;
- while (s[counter])
- counter++;
- return (counter);
-}
-
-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);
-}
-
-void h_putnbr(unsigned long int num)
-{
- if (num > 9)
- h_putnbr(num / 10);
- h_putchar(num % 10 + '0');
-}
-
-void h_putchar(char c)
-{
- write(STDOUT_FILENO, &c, 1);
-}
-
-void h_putstr(char *s)
-{
- write(STDOUT_FILENO, s, h_strlen(s));
-}
-
-int h_err(int ret, const char *format, ...)
-{
- char *str;
- va_list ap;
-
- va_start(ap, format);
- while (*format != '\0')
- {
- if (format[0] == '%' && format[1] == 's')
- {
- str = va_arg(ap, char*);
- if (str != NULL)
- write(STDERR_FILENO, str, h_strlen(str));
- format += 2;
- }
- else
- {
- write(STDERR_FILENO, format, 1);
- format++;
- }
- }
- va_end(ap);
- write(STDERR_FILENO, "\n", 1);
- return (ret);
-}
-
-t_time h_time_now(void)
-{
- struct timeval tv;
-
- if (gettimeofday(&tv, NULL) == -1)
- return (-1);
- return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
-}
diff --git a/philo_one/src/io.c b/philo_one/src/io.c
index 96a8f07..6fb6424 100644
--- a/philo_one/src/io.c
+++ b/philo_one/src/io.c
@@ -6,28 +6,28 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/09/29 14:55:21 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:49:22 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-static void philo_put(int id, t_philo_event event)
-{
- h_putnbr(h_time_now());
- h_putchar(' ');
- h_putnbr(id);
- if (event == EVENT_FORK)
- h_putstr(" has taken fork\n");
- else if (event == EVENT_EAT)
- h_putstr(" is eating\n");
- else if (event == EVENT_SLEEP)
- h_putstr(" is sleeping\n");
- else if (event == EVENT_THINK)
- h_putstr(" is thinking\n");
- else if (event == EVENT_DIE)
- h_putstr(" died\n");
-}
+/* static void philo_put(int id, t_philo_event event) */
+/* { */
+/* h_putnbr(h_time_now()); */
+/* h_putchar(' '); */
+/* h_putnbr(id); */
+/* if (event == EVENT_FORK) */
+/* h_putstr(" has taken fork\n"); */
+/* else if (event == EVENT_EAT) */
+/* h_putstr(" is eating\n"); */
+/* else if (event == EVENT_SLEEP) */
+/* h_putstr(" is sleeping\n"); */
+/* else if (event == EVENT_THINK) */
+/* h_putstr(" is thinking\n"); */
+/* else if (event == EVENT_DIE) */
+/* h_putstr(" died\n"); */
+/* } */
void io_eat(t_routine_arg *arg)
{
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index e92fdcd..2c04316 100644
--- a/philo_one/src/philo_one.h
+++ b/philo_one/src/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/09/29 14:15:47 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:48:36 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,24 +21,22 @@
# include <limits.h>
# include <stdarg.h>
-typedef long int t_time;
+# include "common.h"
-typedef enum
-{
- EVENT_FORK,
- EVENT_EAT,
- EVENT_SLEEP,
- EVENT_THINK,
- EVENT_DIE
-} t_philo_event;
+// typedef long int t_time;
+
+// typedef enum
+// {
+// EVENT_FORK,
+// EVENT_EAT,
+// EVENT_SLEEP,
+// EVENT_THINK,
+// EVENT_DIE
+// } t_philo_event;
typedef struct
{
- int philo_num;
- t_time timeout_death;
- t_time timeout_eat;
- t_time timeout_sleep;
- int meal_num;
+ t_philo_args args;
bool all_alive;
pthread_mutex_t mutex_stdout;
pthread_mutex_t mutex_all_alive;
@@ -82,7 +80,7 @@ bool philos_start(
t_philo *philos,
t_routine_arg *routine_args,
int num);
-void philos_join(t_philo *philos, int num);
+void philos_detach(t_philo *philos, int num);
/*
** routine.c
@@ -100,24 +98,4 @@ void io_think(t_routine_arg *arg);
void io_sleep(t_routine_arg *arg);
void io_die(t_routine_arg *arg);
-/*
-** common.c
-*/
-
-bool parse_args(
- t_philo_conf *philo_args,
- int argc,
- char **argv);
-
-/*
-** helper.c
-*/
-
-long int h_atou_strict(char *s);
-void h_putnbr(unsigned long num);
-void h_putchar(char c);
-void h_putstr(char *s);
-int h_err(int ret, const char *format, ...);
-t_time h_time_now(void);
-
#endif