From 1a7c6c5a0ed9a5aae1fe45c3af335c120c2dc642 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 10 Feb 2020 00:34:47 +0100 Subject: WIP: philo one --- common/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ common/common.c | 6 +++--- common/common.h | 31 ++++++++----------------------- common/helper.c | 2 +- common/philo.c | 53 ++++++++++++----------------------------------------- 5 files changed, 62 insertions(+), 68 deletions(-) create mode 100644 common/Makefile (limited to 'common') diff --git a/common/Makefile b/common/Makefile new file mode 100644 index 0000000..cf3089c --- /dev/null +++ b/common/Makefile @@ -0,0 +1,38 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/02/09 22:39:08 by cacharle #+# #+# # +# Updated: 2020/02/09 23:57:34 by cacharle ### ########.fr # +# # +# **************************************************************************** # + +LIB = ar rcs +RM = rm -rf + +CC = gcc +CCFLAGS = -Wall -Wextra -Werror + +NAME = libphilocommon.a + +SRC = $(shell find . -type f -name "*.c") +OBJ = $(SRC:.c=.o) + +all: $(NAME) + +$(NAME): $(OBJ) + $(LIB) $(NAME) $(OBJ) + +%.o: %.c + $(CC) $(CCFLAGS) -c -o $@ $< + +clean: + $(RM) $(OBJ) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/common/common.c b/common/common.c index be35e69..03fc32f 100644 --- a/common/common.c +++ b/common/common.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/02/09 01:36:40 by cacharle ### ########.fr */ +/* Updated: 2020/02/09 23:57:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ t_bool parse_args(t_philo_args *philo_args, int argc, char **argv) return (TRUE); } -void philo_put_state_change(t_philo *philo, t_philo_event event) // not correct for philo3 +void philo_put_state_change(int id, t_philo_event event) // not correct for philo3 { struct timeval tv; @@ -42,7 +42,7 @@ void philo_put_state_change(t_philo *philo, t_philo_event event) // not corr h_putnbr(tv.tv_sec); h_putnbr(tv.tv_usec / 1000); h_putchar(' '); - h_putnbr(philo->id); + h_putnbr(id); if (event == EVENT_FORK) h_putstr(" has taken fork\n"); else if (event == EVENT_EATING) diff --git a/common/common.h b/common/common.h index ff371a9..def49cf 100644 --- a/common/common.h +++ b/common/common.h @@ -6,14 +6,16 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/02/09 03:22:56 by cacharle ### ########.fr */ +/* Updated: 2020/02/09 23:56:48 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef COMMON_H # define COMMON_H +# include # include +# include # include # define FALSE 0 @@ -38,13 +40,6 @@ typedef enum EVENT_DIED } t_philo_event; -typedef struct -{ - int id; - t_philo_state state; - pthread_t thread; -} t_philo; - typedef struct { int philo_num; @@ -56,30 +51,21 @@ typedef struct typedef void (*t_philo_routine)(void *arg); -typedef struct -{ - t_bool used; - t_philo *left; - t_philo *right; -} t_fork; - /* ** common.c */ t_bool parse_args(t_philo_args *philo_args, int argc, char **argv); -void philo_put_state_change(t_philo *philo, t_philo_event event); +void philo_put_state_change(int id, t_philo_event event); /* ** philo.c */ -t_philo *philos_new(int num); -void philos_destroy(t_philo *philo); - -void philo_eat(t_philo *philo); -void philo_sleep(t_philo *philo); -void philo_think(t_philo *philo); +void philo_eat(int id, int timeout); +void philo_sleep(int id, int timeout); +void philo_think(int id); +void philo_die(int id); /* ** helper.c @@ -92,5 +78,4 @@ void h_putchar(char c); void h_putstr(char *s); void *h_calloc(int count, int size); - #endif diff --git a/common/helper.c b/common/helper.c index 4c4b1f1..590c415 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/02/09 02:15:02 by cacharle ### ########.fr */ +/* Updated: 2020/02/10 01:15:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/common/philo.c b/common/philo.c index b8d7caf..484257e 100644 --- a/common/philo.c +++ b/common/philo.c @@ -6,61 +6,32 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */ -/* Updated: 2020/02/09 03:26:07 by cacharle ### ########.fr */ +/* Updated: 2020/02/09 23:56:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -t_philo *philos_new(int num, t_philo_routine routine) +void philo_eat(int id, int timeout) { - int i; - t_philo *philos; - - if (num < 0) - return (NULL); - if ((philos = (t_philo*)h_calloc(num + 1, sizeof(t_philo))) == NULL) - return (NULL); - i = -1; - while (++i < num) - { - philos[i].id = num + 1; - if (pthread_create(&philos[i].thread, NULL, routine, philos + num) != 0) - { - philos_destroy(philos, i); - return (NULL); - } - } - return (philos); + philo_put_state_change(id, EVENT_EATING); + usleep(timeout * 1000); } -void philos_destroy(t_philo *philo, int num) +void philo_sleep(int id, int timeout) { - if (philo == NULL) - return ; - while (num-- > 0) - pthread_join(philos[num].thread, NULL); - free(philos); + philo_put_state_change(id, EVENT_SLEEPING); + usleep(timeout * 1000); } -void philo_eat(t_philo *philo) +void philo_think(int id) { - // take forks - // lock mutex - philo_put_state_change(philo, EVENT_EATING); - usleep(philo->timeout_eat * 1000); - // drop forks - // unlock mutex + philo_put_state_change(id, EVENT_THINKING); } -void philo_sleep(t_philo *philo) +void philo_die(int id) { - philo_put_state_change(philo, EVENT_SLEEPING); - usleep(philo->timeout_sleep * 1000); + philo_put_state_change(id, EVENT_DIED); } -void philo_think(t_philo *philo) -{ - philo_put_state_change(philo, EVENT_THINKING); - // search a fork -} + -- cgit