aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-22 13:43:00 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-22 13:43:00 +0200
commit3c2c3a07396c001f51eac8d0fd0fc84e83eb127b (patch)
tree7c646f128d97784fba9fd76cf8bb758a9b965c76
parent2c5abe421b7a1b92081e38f6b1f04d407fcba834 (diff)
downloadphilosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.tar.gz
philosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.tar.bz2
philosophers-3c2c3a07396c001f51eac8d0fd0fc84e83eb127b.zip
philo_one refactoring without common lib
-rw-r--r--Makefile8
-rw-r--r--philo_one/Makefile24
-rw-r--r--philo_one/common.c30
-rw-r--r--philo_one/forks.c (renamed from philo_one/fork.c)33
-rw-r--r--philo_one/helper.c80
-rw-r--r--philo_one/io.c65
-rw-r--r--philo_one/main.c8
-rw-r--r--philo_one/philo.c20
-rw-r--r--philo_one/philo_one.h98
-rw-r--r--philo_one/routine.c17
10 files changed, 277 insertions, 106 deletions
diff --git a/Makefile b/Makefile
index 3a9fd90..73b011a 100644
--- a/Makefile
+++ b/Makefile
@@ -6,28 +6,22 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/09 03:31:28 by cacharle #+# #+# #
-# Updated: 2020/02/14 22:44:40 by cacharle ### ########.fr #
+# Updated: 2020/04/22 13:41:57 by charles ### ########.fr #
# #
# **************************************************************************** #
MAKE = make
MAKE_ARGS = --no-print-directory
-COMMON_DIR = common
PHILO_ONE_DIR = philo_one
PHILO_TWO_DIR = philo_two
PHILO_THREE_DIR = philo_three
help:
- @echo "make common - build common lib"
@echo "make philo_one - compile philo_one"
@echo "make philo_two - compile philo_one"
@echo "make philo_three - compile philo_one"
-.PHONY: common
-common:
- $(MAKE) $(MAKE_ARGS) -C $(COMMON_DIR)
-
.PHONY: philo_one
philo_one:
$(MAKE) $(MAKE_ARGS) -C $(PHILO_ONE_DIR)
diff --git a/philo_one/Makefile b/philo_one/Makefile
index 1bab8d6..8b1213b 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,46 +6,40 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2020/02/14 22:48:10 by cacharle ### ########.fr #
+# Updated: 2020/04/22 13:02:38 by charles ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
-COMMON_DIR = ../common
-
CC = gcc
-CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra #-Werror
-LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon
+CCFLAGS = -Wall -Wextra -Werror
+LDFLAGS = -lpthread
NAME = philo_one
SRC = main.c \
philo.c \
- fork.c \
+ forks.c \
routine.c \
- io.c
+ io.c \
+ common.c \
+ helper.c
OBJ = $(SRC:.c=.o)
all: $(NAME)
-$(NAME): common_all $(OBJ)
+$(NAME): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CCFLAGS) -c -o $@ $<
-clean: common_fclean
+clean:
$(RM) $(OBJ)
fclean: clean
$(RM) $(NAME)
re: fclean all
-
-common_all:
- make -C $(COMMON_DIR) all
-
-common_fclean:
- make -C $(COMMON_DIR) fclean
diff --git a/philo_one/common.c b/philo_one/common.c
new file mode 100644
index 0000000..b9d6e9a
--- /dev/null
+++ b/philo_one/common.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* common.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
+/* Updated: 2020/04/22 13:22:51 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_one.h"
+
+bool parse_args(t_philo_conf *philo_args, int argc, char **argv)
+{
+ if (argc != 5 && argc != 6)
+ return (false);
+ if ((philo_args->philo_num = h_atoi_strict_unsigned(argv[1])) == -1
+ || (philo_args->timeout_death = h_atoi_strict_unsigned(argv[2])) == -1
+ || (philo_args->timeout_eat = h_atoi_strict_unsigned(argv[3])) == -1
+ || (philo_args->timeout_sleep = h_atoi_strict_unsigned(argv[4])) == -1)
+ return (false);
+ if (argc == 6
+ && (philo_args->meal_num = h_atoi_strict_unsigned(argv[5])) == -1)
+ return (false);
+ else
+ philo_args->meal_num = -1;
+ return (true);
+}
diff --git a/philo_one/fork.c b/philo_one/forks.c
index 3f91b35..065c7c6 100644
--- a/philo_one/fork.c
+++ b/philo_one/forks.c
@@ -1,23 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* fork.c :+: :+: :+: */
+/* forks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */
-/* Updated: 2020/02/15 01:23:25 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:26:58 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-t_fork *forks_new(int num)
+pthread_mutex_t *forks_new(int num)
{
- int i;
- t_fork *forks;
+ int i;
+ pthread_mutex_t *forks;
- if ((forks = (t_fork*)malloc(sizeof(t_fork) * num)) == NULL)
+ if ((forks = (pthread_mutex_t*)malloc(
+ sizeof(pthread_mutex_t) * num)) == NULL)
return (NULL);
i = -1;
while (++i < num)
@@ -31,29 +32,31 @@ t_fork *forks_new(int num)
return (forks);
}
-void forks_destroy(t_fork *forks, int num)
+void forks_destroy(pthread_mutex_t *forks, int num)
{
while (num-- > 0)
pthread_mutex_destroy(&forks[num]);
free(forks);
}
-t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks,
- t_philo_args *args)
+t_routine_arg *forks_dispatch(
+ t_philo *philos,
+ pthread_mutex_t *forks,
+ t_philo_conf *conf)
{
int i;
t_routine_arg *routine_args;
- if ((routine_args = (t_routine_arg*)malloc(sizeof(t_routine_arg)
- * args->philo_num)) == NULL)
+ if ((routine_args = (t_routine_arg*)malloc(
+ sizeof(t_routine_arg) * conf->philo_num)) == NULL)
return (NULL);
i = -1;
- while (++i < args->philo_num)
+ while (++i < conf->philo_num)
{
- routine_args[i].args = args;
+ routine_args[i].conf = conf;
routine_args[i].philo = philos + i;
- routine_args[i].fork_left = forks + i % args->philo_num;
- routine_args[i].fork_right = forks + (i + 1) % args->philo_num;
+ routine_args[i].fork_left = forks + i % conf->philo_num;
+ routine_args[i].fork_right = forks + (i + 1) % conf->philo_num;
}
return (routine_args);
}
diff --git a/philo_one/helper.c b/philo_one/helper.c
new file mode 100644
index 0000000..cd4b8a0
--- /dev/null
+++ b/philo_one/helper.c
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
+/* Updated: 2020/04/22 11:57:08 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_one.h"
+
+static int h_strlen(char *s)
+{
+ int counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
+
+long int h_atoi_strict_unsigned(char *s)
+{
+ long int num;
+
+ if (*s < '0' || *s > '9')
+ return (-1);
+ num = 0;
+ while (*s >= '0' && *s <= '9')
+ {
+ num *= 10;
+ num += *s - '0';
+ s++;
+ }
+ if (*s != '\0')
+ return (-1);
+ 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));
+}
+
+void *h_calloc(int count, int size)
+{
+ 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_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/io.c b/philo_one/io.c
index 846c830..b4d9949 100644
--- a/philo_one/io.c
+++ b/philo_one/io.c
@@ -6,40 +6,63 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/02/14 21:42:30 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:37:40 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_one.h"
-void io_eat(t_routine_arg *arg)
+static void philo_put(int id, t_philo_event event)
{
- pthread_mutex_lock(&arg->args->mutex_stdout);
- if (arg->args->all_alive)
- philo_eat(arg->philo->id, arg->args->timeout_eat);
- pthread_mutex_unlock(&arg->args->mutex_stdout);
+ 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_think(t_routine_arg *arg)
+void io_eat(t_routine_arg *arg)
{
- pthread_mutex_lock(&arg->args->mutex_stdout);
- if (arg->args->all_alive)
- philo_think(arg->philo->id);
- pthread_mutex_unlock(&arg->args->mutex_stdout);
+ if (!arg->conf->all_alive)
+ return ;
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ philo_put(arg->philo->id, EVENT_EAT);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
+ usleep(arg->conf->timeout_eat * 1000);
}
-void io_sleep(t_routine_arg *arg)
+void io_think(t_routine_arg *arg)
{
- pthread_mutex_lock(&arg->args->mutex_stdout);
- if (arg->args->all_alive)
- philo_sleep(arg->philo->id, arg->args->timeout_sleep);
- pthread_mutex_unlock(&arg->args->mutex_stdout);
+ if (!arg->conf->all_alive)
+ return ;
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ philo_put(arg->philo->id, EVENT_THINK);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
-void io_die(t_routine_arg *arg)
+void io_sleep(t_routine_arg *arg)
{
- pthread_mutex_lock(&arg->args->mutex_stdout);
- if (arg->args->all_alive)
- philo_die(arg->philo->id);
- pthread_mutex_unlock(&arg->args->mutex_stdout);
+ if (!arg->conf->all_alive)
+ return ;
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ philo_put(arg->philo->id, EVENT_SLEEP);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
+ usleep(arg->conf->timeout_sleep * 1000);
+}
+
+void io_die(t_routine_arg *arg)
+{
+ if (!arg->conf->all_alive)
+ return ;
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
+ philo_put(arg->philo->id, EVENT_DIE);
+ pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
diff --git a/philo_one/main.c b/philo_one/main.c
index abf9d9a..4739c0b 100644
--- a/philo_one/main.c
+++ b/philo_one/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2020/02/15 00:57:24 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:07:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,9 +14,9 @@
int main(int argc, char **argv)
{
- t_philo_args philo_args;
+ t_philo_conf philo_args;
t_philo *philos;
- t_fork *forks;
+ pthread_mutex_t *forks;
t_routine_arg *routine_args;
if (!parse_args(&philo_args, argc, argv))
@@ -27,7 +27,7 @@ int main(int argc, char **argv)
return (1);
if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL)
return (1);
- philo_args.all_alive = TRUE;
+ philo_args.all_alive = true;
pthread_mutex_init(&philo_args.mutex_all_alive, NULL);
pthread_mutex_init(&philo_args.mutex_stdout, NULL);
if (!philos_start(philos, routine_args, philo_args.philo_num))
diff --git a/philo_one/philo.c b/philo_one/philo.c
index fffbc6c..97a0616 100644
--- a/philo_one/philo.c
+++ b/philo_one/philo.c
@@ -25,7 +25,7 @@ t_philo *philos_new(int num)
while (++i < num)
{
philos[i].id = i + 1;
- philos[i].alive = FALSE;
+ philos[i].alive = false;
}
return (philos);
}
@@ -38,19 +38,19 @@ void philos_destroy(t_philo *philos, int num)
free(philos);
}
-t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
+bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num)
{
int i;
i = -1;
while (++i < num)
{
- philos[i].alive = TRUE;
+ philos[i].alive = true;
if (pthread_create(&philos[i].thread, NULL,
&routine_philo, (void*)(routine_args + i)) == -1)
- return (FALSE);
+ return (false);
}
- return (TRUE);
+ return (true);
}
void philos_join(t_philo *philos, int num)
@@ -62,13 +62,13 @@ void philos_join(t_philo *philos, int num)
{
if (philos[i].alive)
{
- philos[i].alive = FALSE;
+ philos[i].alive = false;
pthread_join(philos[i].thread, NULL);
}
}
}
-t_bool philos_starved(t_philo *philos, int num)
+bool philos_starved(t_philo *philos, int num)
{
int i;
@@ -79,9 +79,9 @@ t_bool philos_starved(t_philo *philos, int num)
{
i = -1;
while (++i < num)
- philos[i].alive = FALSE;
- return (TRUE);
+ philos[i].alive = false;
+ return (true);
}
}
- return (FALSE);
+ return (false);
}
diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h
index 498cd49..7badd71 100644
--- a/philo_one/philo_one.h
+++ b/philo_one/philo_one.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */
-/* Updated: 2020/02/15 01:25:40 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:26:06 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,36 +17,60 @@
# include <sys/time.h>
# include <stdlib.h>
# include <pthread.h>
-# include "common.h"
+# include <stdbool.h>
-typedef pthread_mutex_t t_fork;
+typedef long int t_time;
-typedef struct s_philo
+typedef enum
{
- int id;
- t_bool alive;
- t_time time_last_eat;
- t_philo_state state;
- pthread_t thread;
-} t_philo;
-
-typedef struct s_routine_arg
+ 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;
+ bool all_alive;
+ pthread_mutex_t mutex_stdout;
+ pthread_mutex_t mutex_all_alive;
+} t_philo_conf;
+
+typedef struct s_philo
{
- t_philo_args *args;
- t_philo *philo;
- t_fork *fork_left;
- t_fork *fork_right;
- pthread_mutex_t *mutex_stdout;
-} t_routine_arg;
+ int id;
+ bool alive;
+ t_time time_last_eat;
+ pthread_t thread;
+} t_philo;
+
+typedef struct s_routine_arg
+{
+ t_philo_conf *conf;
+ t_philo *philo;
+ pthread_mutex_t *fork_left;
+ pthread_mutex_t *fork_right;
+ pthread_mutex_t *mutex_stdout;
+} t_routine_arg;
+
+typedef void (*t_philo_routine)(void *arg);
/*
-** fork.c
+** forks.c
*/
-t_fork *forks_new(int num);
-void forks_destroy(t_fork *forks, int num);
-t_routine_arg *forks_dispatch(t_philo *philos,
- t_fork *forks, t_philo_args *args);
+pthread_mutex_t *forks_new(int num);
+void forks_destroy(pthread_mutex_t *forks, int num);
+t_routine_arg *forks_dispatch(
+ t_philo *philos,
+ pthread_mutex_t *forks,
+ t_philo_conf *conf);
/*
** philo.c
@@ -54,10 +78,12 @@ t_routine_arg *forks_dispatch(t_philo *philos,
t_philo *philos_new(int num);
void philos_destroy(t_philo *philos, int num);
-t_bool philos_start(t_philo *philos,
- t_routine_arg *routine_args, int num);
+bool philos_start(
+ t_philo *philos,
+ t_routine_arg *routine_args,
+ int num);
void philos_join(t_philo *philos, int num);
-t_bool philos_starved(t_philo *philos, int num);
+bool philos_starved(t_philo *philos, int num);
/*
** routine.c
@@ -75,4 +101,24 @@ 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_atoi_strict_unsigned(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_time_now(void);
+
#endif
diff --git a/philo_one/routine.c b/philo_one/routine.c
index 89fe95e..69952b1 100644
--- a/philo_one/routine.c
+++ b/philo_one/routine.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */
-/* Updated: 2020/02/15 01:25:41 by cacharle ### ########.fr */
+/* Updated: 2020/04/22 13:39:48 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,16 +18,17 @@ void *routine_philo(void *void_arg)
pthread_t thread_death;
arg = (t_routine_arg*)void_arg;
- if (!arg->args->all_alive)
+ if (!arg->conf->all_alive)
return (NULL);
arg->philo->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, routine_death, arg) != 0)
return (NULL);
io_think(arg);
- while (arg->args->all_alive)
+ while (arg->conf->all_alive)
{
pthread_mutex_lock(arg->fork_left);
pthread_mutex_lock(arg->fork_right);
+ arg->philo->time_last_eat = h_time_now();
io_eat(arg);
pthread_mutex_unlock(arg->fork_right);
pthread_mutex_unlock(arg->fork_left);
@@ -45,12 +46,12 @@ void *routine_death(void *void_arg)
arg = (t_routine_arg*)void_arg;
current = h_time_now();
- while (arg->args->all_alive &&
- current - arg->philo->time_last_eat < arg->args->timeout_death)
+ while (arg->conf->all_alive &&
+ current - arg->philo->time_last_eat < arg->conf->timeout_death)
current = h_time_now();
io_die(arg);
- pthread_mutex_lock(&arg->args->mutex_all_alive);
- arg->args->all_alive = FALSE;
- pthread_mutex_unlock(&arg->args->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ arg->conf->all_alive = false;
+ pthread_mutex_unlock(&arg->conf->mutex_all_alive);
return (NULL);
}