aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/Makefile6
-rw-r--r--common/args.c (renamed from common/common.c)21
-rw-r--r--common/common.h15
-rw-r--r--common/helper.c49
-rw-r--r--common/io.c82
-rw-r--r--philo_one/Makefile6
-rw-r--r--philo_one/src/event.c (renamed from philo_one/src/io.c)10
-rw-r--r--philo_one/src/philo_one.h10
-rw-r--r--philo_one/src/routine.c14
-rw-r--r--philo_two/src/event.c (renamed from philo_two/src/io.c)33
-rw-r--r--philo_two/src/main.c16
-rw-r--r--philo_two/src/philo_two.h23
-rw-r--r--philo_two/src/routine.c29
13 files changed, 160 insertions, 154 deletions
diff --git a/common/Makefile b/common/Makefile
index 62d75c9..e9c209d 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/09 22:39:08 by cacharle #+# #+# #
-# Updated: 2020/09/30 08:22:35 by cacharle ### ########.fr #
+# Updated: 2020/09/30 10:27:40 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -14,7 +14,7 @@ LIB = ar rcs
RM = rm -rf
CC = gcc
-CCFLAGS = -O2 -Wall -Wextra -Werror
+CCFLAGS = -O2 -Wall -Wextra #-Werror
NAME = libphilocommon.a
@@ -33,6 +33,6 @@ clean:
$(RM) $(OBJ)
$(RM) $(NAME)
-re: fclean all
+re: clean all
.PHONY: all clean re
diff --git a/common/common.c b/common/args.c
index 62fd382..6392f22 100644
--- a/common/common.c
+++ b/common/args.c
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* common.c :+: :+: :+: */
+/* args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/09/30 07:56:12 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 10:14:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,20 +30,3 @@ bool parse_args(t_philo_args *args, int argc, char **argv)
args->meal_num = 1;
return (true);
}
-
-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_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");
-}
diff --git a/common/common.h b/common/common.h
index 0b64e92..cb71172 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/09/30 08:40:38 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 10:15:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,22 +43,23 @@ typedef struct
typedef void *(*t_routine)(void *arg);
/*
-** common.c
+** args.c
*/
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_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);
t_time h_time_now(void);
+
+/*
+** io.c
+*/
+
+void philo_put(size_t id, t_philo_event event);
int h_err(int ret, const char *format, char *str);
#endif
diff --git a/common/helper.c b/common/helper.c
index 61b85a1..b2107ae 100644
--- a/common/helper.c
+++ b/common/helper.c
@@ -6,22 +6,12 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/09/30 07:57:21 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 10:15:14 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-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;
@@ -46,43 +36,6 @@ long int h_atou_strict(char *s)
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)
-{
- 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)
{
struct timeval tv;
diff --git a/common/io.c b/common/io.c
new file mode 100644
index 0000000..7989575
--- /dev/null
+++ b/common/io.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* io.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
+/* Updated: 2020/09/30 10:32:41 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+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)
+{
+ static char buf[2048];
+
+ buf[0] = '\0';
+ st_nbrcpy(buf, h_time_now());
+ 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_one/Makefile b/philo_one/Makefile
index 4ec2e75..391fa4f 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2020/09/30 09:35:22 by cacharle ### ########.fr #
+# Updated: 2020/09/30 10:20:59 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -27,12 +27,12 @@ OBJDIR = obj
SRC = $(shell find $(SRCDIR) -type f -name '*.c')
OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
-all: prebuild common_all $(NAME)
+all: prebuild $(NAME)
prebuild:
@mkdir -pv $(OBJDIR)
-$(NAME): $(OBJ)
+$(NAME): common_all $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
diff --git a/philo_one/src/io.c b/philo_one/src/event.c
index b5bf82e..8952212 100644
--- a/philo_one/src/io.c
+++ b/philo_one/src/event.c
@@ -12,7 +12,7 @@
#include "philo_one.h"
-void io_take_fork(t_philo *arg, pthread_mutex_t *fork)
+void event_take_fork(t_philo *arg, pthread_mutex_t *fork)
{
pthread_mutex_lock(fork);
pthread_mutex_lock(&arg->conf->mutex_stdout);
@@ -22,7 +22,7 @@ void io_take_fork(t_philo *arg, pthread_mutex_t *fork)
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
-void io_eat(t_philo *arg)
+void event_eat(t_philo *arg)
{
int eat_counter;
@@ -39,7 +39,7 @@ void io_eat(t_philo *arg)
}
}
-void io_think(t_philo *arg)
+void event_think(t_philo *arg)
{
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
@@ -48,7 +48,7 @@ void io_think(t_philo *arg)
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
-void io_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left)
+void event_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left)
{
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
@@ -60,7 +60,7 @@ void io_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_
usleep(arg->conf->timeout_sleep * 1000);
}
-void io_die(t_philo *arg)
+void event_die(t_philo *arg)
{
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index 9fc5e5f..cb41b77 100644
--- a/philo_one/src/philo_one.h
+++ b/philo_one/src/philo_one.h
@@ -76,10 +76,10 @@ void *routine_death(t_philo *arg);
** io.c
*/
-void io_take_fork(t_philo *arg, pthread_mutex_t *fork);
-void io_eat(t_philo *arg);
-void io_think(t_philo *arg);
-void io_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left);
-void io_die(t_philo *arg);
+void event_take_fork(t_philo *arg, pthread_mutex_t *fork);
+void event_eat(t_philo *arg);
+void event_think(t_philo *arg);
+void event_sleep(t_philo *arg, pthread_mutex_t *fork_right, pthread_mutex_t *fork_left);
+void event_die(t_philo *arg);
#endif
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 5f76a26..c1a7a4f 100644
--- a/philo_one/src/routine.c
+++ b/philo_one/src/routine.c
@@ -21,15 +21,15 @@ void *routine_philo(t_philo *arg)
arg->time_last_eat = h_time_now();
if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
return (NULL);
- io_think(arg);
+ event_think(arg);
while (arg->conf->all_alive)
{
- io_take_fork(arg, arg->fork_left);
- io_take_fork(arg, arg->fork_right);
+ event_take_fork(arg, arg->fork_left);
+ event_take_fork(arg, arg->fork_right);
arg->time_last_eat = h_time_now();
- io_eat(arg);
- io_sleep(arg, arg->fork_right, arg->fork_left);
- io_think(arg);
+ event_eat(arg);
+ event_sleep(arg, arg->fork_right, arg->fork_left);
+ event_think(arg);
}
pthread_join(thread_death, NULL);
return (NULL);
@@ -45,6 +45,6 @@ void *routine_death(t_philo *arg)
current = h_time_now();
if (!arg->conf->all_alive)
return (NULL);
- io_die(arg);
+ event_die(arg);
return (NULL);
}
diff --git a/philo_two/src/io.c b/philo_two/src/event.c
index 771d9ee..d86f6aa 100644
--- a/philo_two/src/io.c
+++ b/philo_two/src/event.c
@@ -6,73 +6,66 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:43:45 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:56:53 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-void io_take_fork(t_routine_arg *arg)
+void event_take_fork(t_philo *arg)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
- if (!arg->conf->all_alive)
- return ;
sem_wait(arg->forks);
pthread_mutex_lock(&arg->conf->mutex_stdout);
+ if (!arg->conf->all_alive)
+ return ;
philo_put(arg->id, EVENT_FORK);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
}
-void io_eat(t_routine_arg *arg)
+void event_eat(t_philo *arg)
{
int eat_counter;
eat_counter = 0;
while (eat_counter < arg->conf->meal_num)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
philo_put(arg->id, EVENT_EAT);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
usleep(arg->conf->timeout_eat * 1000);
eat_counter++;
}
}
-void io_think(t_routine_arg *arg)
+void event_think(t_philo *arg)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
philo_put(arg->id, EVENT_THINK);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
}
-void io_sleep(t_routine_arg *arg)
+void event_sleep(t_philo *arg)
{
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
philo_put(arg->id, EVENT_SLEEP);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
sem_post(arg->forks);
sem_post(arg->forks);
usleep(arg->conf->timeout_sleep * 1000);
}
-void io_die(t_routine_arg *arg)
+void event_die(t_philo *arg)
{
+ pthread_mutex_lock(&arg->conf->mutex_stdout);
if (!arg->conf->all_alive)
return ;
- pthread_mutex_lock(&arg->conf->mutex_stdout);
philo_put(arg->id, EVENT_DIE);
+ arg->conf->all_alive = false;
pthread_mutex_unlock(&arg->conf->mutex_stdout);
}
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 27dce34..c0c9fd6 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,39 +6,38 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:44:46 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:58:01 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-#define PHILO_SEM_NAME "philo_two"
+#define PHILO_SEM_NAME "semaphore_philo_two"
int main(int argc, char **argv)
{
int i;
t_philo_conf conf;
- t_routine_arg *routine_args;
+ t_philo *philos;
sem_t *forks;
pthread_t *threads;
if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
sem_unlink(PHILO_SEM_NAME);
- forks = sem_open(PHILO_SEM_NAME, O_CREAT, 0700, conf.philo_num);
+ forks = sem_open(PHILO_SEM_NAME, O_CREAT | O_EXCL, 0600, conf.philo_num);
if (forks == SEM_FAILED)
return (1);
- if ((routine_args = routine_args_create(&conf, forks)) == NULL)
+ if ((philos = routine_create_philos(&conf, forks)) == NULL)
return (1);
if ((threads = malloc(sizeof(pthread_t) * conf.philo_num)) == NULL)
return (1);
conf.all_alive = true;
- pthread_mutex_init(&conf.mutex_all_alive, NULL);
pthread_mutex_init(&conf.mutex_stdout, NULL);
i = -1;
while (++i < conf.philo_num)
- if (pthread_create(threads + i, NULL, (t_routine)routine_philo, routine_args + i) < 0)
+ if (pthread_create(threads + i, NULL, (t_routine)routine_philo, philos + i) < 0)
return (1);
while (conf.all_alive)
;
@@ -50,8 +49,7 @@ int main(int argc, char **argv)
sem_unlink(PHILO_SEM_NAME);
pthread_mutex_destroy(&conf.mutex_stdout);
- pthread_mutex_destroy(&conf.mutex_all_alive);
free(threads);
- free(routine_args);
+ free(philos);
return (0);
}
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index 5318510..a697382 100644
--- a/philo_two/src/philo_two.h
+++ b/philo_two/src/philo_two.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:41:15 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 10:02:50 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,34 +28,33 @@ typedef struct
t_time timeout_sleep;
long int meal_num;
bool all_alive;
- pthread_mutex_t mutex_all_alive;
pthread_mutex_t mutex_stdout;
} t_philo_conf;
typedef struct
{
- int id;
+ long int id;
t_philo_conf *conf;
t_time time_last_eat;
sem_t *forks;
-} t_routine_arg;
+} t_philo;
/*
** routine.c
*/
-void *routine_philo(t_routine_arg *arg);
-void *routine_death(t_routine_arg *arg);
-t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks);
+void *routine_philo(t_philo *arg);
+void *routine_death(t_philo *arg);
+t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks);
/*
** io.c
*/
-void io_take_fork(t_routine_arg *arg);
-void io_eat(t_routine_arg *arg);
-void io_think(t_routine_arg *arg);
-void io_sleep(t_routine_arg *arg);
-void io_die(t_routine_arg *arg);
+void event_take_fork(t_philo *arg);
+void event_eat(t_philo *arg);
+void event_think(t_philo *arg);
+void event_sleep(t_philo *arg);
+void event_die(t_philo *arg);
#endif
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index cfce645..6554c89 100644
--- a/philo_two/src/routine.c
+++ b/philo_two/src/routine.c
@@ -6,17 +6,17 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */
-/* Updated: 2020/09/30 08:41:23 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 09:57:41 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-void *routine_philo(t_routine_arg *arg)
+void *routine_philo(t_philo *arg)
{
pthread_t thread_death;
- io_think(arg);
+ event_think(arg);
if (!arg->conf->all_alive)
return (NULL);
arg->time_last_eat = h_time_now();
@@ -24,18 +24,18 @@ void *routine_philo(t_routine_arg *arg)
return (NULL);
while (arg->conf->all_alive)
{
- io_take_fork(arg);
- io_take_fork(arg);
- io_eat(arg);
+ event_take_fork(arg);
+ event_take_fork(arg);
+ event_eat(arg);
arg->time_last_eat = h_time_now();
- io_sleep(arg);
- io_think(arg);
+ event_sleep(arg);
+ event_think(arg);
}
pthread_join(thread_death, NULL);
return (NULL);
}
-void *routine_death(t_routine_arg *arg)
+void *routine_death(t_philo *arg)
{
t_time current;
@@ -43,19 +43,16 @@ void *routine_death(t_routine_arg *arg)
while (arg->conf->all_alive &&
current - arg->time_last_eat < arg->conf->timeout_death)
current = h_time_now();
- pthread_mutex_lock(&arg->conf->mutex_all_alive);
- io_die(arg);
- arg->conf->all_alive = false;
- pthread_mutex_unlock(&arg->conf->mutex_all_alive);
+ event_die(arg);
return (NULL);
}
-t_routine_arg *routine_args_create(t_philo_conf *conf, sem_t *forks)
+t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks)
{
int i;
- t_routine_arg *routine_conf;
+ t_philo *routine_conf;
- if ((routine_conf = malloc(sizeof(t_routine_arg) * conf->philo_num)) == NULL)
+ if ((routine_conf = malloc(sizeof(t_philo) * conf->philo_num)) == NULL)
return (NULL);
i = -1;
while (++i < conf->philo_num)