aboutsummaryrefslogtreecommitdiff
path: root/philo_three
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-15 01:47:26 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-15 01:47:26 +0100
commit2c5abe421b7a1b92081e38f6b1f04d407fcba834 (patch)
treec7e7aa9db3a3b84bd80bc8a5a713d5bfeb6a66f7 /philo_three
parent6a1e91750ee43fccb6160af0f44139698c8dfdc3 (diff)
downloadphilosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.tar.gz
philosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.tar.bz2
philosophers-2c5abe421b7a1b92081e38f6b1f04d407fcba834.zip
philo_one small refactoring, philo_three draft
Diffstat (limited to 'philo_three')
-rw-r--r--philo_three/Makefile21
-rw-r--r--philo_three/child.c39
-rw-r--r--philo_three/main.c42
-rw-r--r--philo_three/philo_three.h19
4 files changed, 114 insertions, 7 deletions
diff --git a/philo_three/Makefile b/philo_three/Makefile
index 8fe7c4d..431bf62 100644
--- a/philo_three/Makefile
+++ b/philo_three/Makefile
@@ -6,33 +6,40 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2019/11/24 07:07:21 by cacharle ### ########.fr #
+# Updated: 2020/02/15 00:46:58 by cacharle ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
+COMMON_DIR = ../common
+
CC = gcc
-CCFLAGS = -Wall -Wextra #-Werror
-LDFLAGS = -lpthread
+CCFLAGS = -I$(COMMON_DIR) -Wall -Wextra #-Werror
+LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon
NAME = philo_three
SRC = main.c
OBJ = $(SRC:.c=.o)
-
all: $(NAME)
-$(NAME): $(OBJ)
+$(NAME): common_all $(OBJ)
$(CC) $(LDFLAGS) -o $@ $(OBJ)
%.o: %.c
- $(CC) $(CCFLAGS) -c -o $@ $^
+ $(CC) $(CCFLAGS) -c -o $@ $<
-clean:
+clean: common_fclean
$(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_three/child.c b/philo_three/child.c
new file mode 100644
index 0000000..b2c570b
--- /dev/null
+++ b/philo_three/child.c
@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* child.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/15 01:40:05 by cacharle #+# #+# */
+/* Updated: 2020/02/15 01:46:20 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_three.h"
+
+void child_start(t_philo_args *philo_args)
+{
+ pthread_t thread_death;
+
+ io_think(arg);
+ if (!arg->args->all_alive)
+ return (NULL);
+ arg->time_last_eat = h_time_now();
+ if (pthread_create(&thread_death, NULL, routine_death, arg) != 0)
+ return (NULL);
+
+ while (arg->running)
+ {
+ sem_wait(arg->forks);
+ sem_wait(arg->forks);
+ io_eat(arg);
+ sem_post(arg->forks);
+ sem_post(arg->forks);
+ arg->time_last_eat = h_time_now();
+ io_sleep(arg);
+ io_think(arg);
+ }
+ pthread_join(thread_death, NULL);
+ return (NULL);
+}
diff --git a/philo_three/main.c b/philo_three/main.c
new file mode 100644
index 0000000..5c9b869
--- /dev/null
+++ b/philo_three/main.c
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */
+/* Updated: 2020/02/15 01:40:26 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_three.h"
+
+int main(int argc, char **argv)
+{
+ t_philo_args philo_args;
+ sem_t *forks;
+
+ if (!parse_args(&philo_args, argc, argv))
+ return (1);
+ forks = sem_open("philo_two", O_CREAT, 0700, philo_args.philo_num);
+ if (forks == SEM_FAILED)
+ return (1);
+
+ i = -1;
+ while (++i < philo_args.philo_num)
+ {
+ pid = fork();
+ if (pid == -1)
+ return (1);
+ if (pid == 0)
+ {
+ child_start(&philo_args);
+ return (0);
+ }
+ }
+
+ sem_close(forks);
+ sem_unlink("philo_two");
+ return (0);
+}
diff --git a/philo_three/philo_three.h b/philo_three/philo_three.h
new file mode 100644
index 0000000..35e0cd3
--- /dev/null
+++ b/philo_three/philo_three.h
@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* philo_three.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */
+/* Updated: 2020/02/15 00:47:25 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef PHILO_THREE_H
+# define PHILO_THREE_H
+
+# include <semaphore.h>
+# include "common.h"
+
+#endif