From e88ec90f41beeb6d415f5bd588a6cbef1a5bc296 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Mon, 13 Mar 2023 18:34:33 +0100 Subject: [PATCH] Add --- philo/philo.c | 9 +-------- philo/philo.h | 35 ++++++++++++++++++----------------- philo/threads.c | 35 ++++++++++++++++++++++++++--------- utils/Makefile | 4 ++-- 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/philo/philo.c b/philo/philo.c index 333224e..fed3370 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,16 +6,9 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:43:51 by erey-bet #+# #+# */ -/* Updated: 2023/03/12 16:54:49 by erey-bet ### ########.fr */ +/* Updated: 2023/03/13 13:43:32 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void *philosopher(t_philo *philo) -{ - write(1, "Hey i'm ", 8); - ft_putnbr_fd(philo->id, 1); - write(1, "\n", 1); - return (NULL); -} diff --git a/philo/philo.h b/philo/philo.h index e4d89d7..99815f2 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,33 +6,34 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */ -/* Updated: 2023/03/12 16:56:46 by erey-bet ### ########.fr */ +/* Updated: 2023/03/13 14:01:59 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PHILO_H # define PHILO_H -typedef struct s_config -{ - int nbr_philo; - int time_die; - int time_eat; - int time_sleep; - int must_eat; -} t_config; - -typedef struct s_philo -{ - t_config conf; - int id; -} t_philo; - # include "../utils/utils.h" # include # include # include -# include +# include + +typedef struct s_config +{ + int nbr_philo; + int time_die; + int time_eat; + int time_sleep; + int must_eat; +} t_config; + +typedef struct s_philo +{ + t_config conf; + pthread_mutex_t mutex; + int *id; +} t_philo; int parsing(char *argv[], t_config *conf); int manage_threads(t_config *conf); diff --git a/philo/threads.c b/philo/threads.c index 78a76e2..44573e9 100644 --- a/philo/threads.c +++ b/philo/threads.c @@ -6,33 +6,50 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/03/12 16:57:52 by erey-bet ### ########.fr */ +/* Updated: 2023/03/13 18:33:50 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include +void *philosopher(t_philo *philo) +{ + pthread_mutex_lock(&philo->mutex); + write(1, "Hey i'm ", 8); + ft_putnbr_fd(*philo->id, 1); + write(1, "\n", 1); + pthread_mutex_unlock(&philo->mutex); + return (NULL); +} + int manage_threads(t_config *config) { - pthread_t *threads; - t_philo philo; - int i; + pthread_t *threads; + pthread_mutex_t mutex_first_fork; + pthread_mutex_t mutex_second_fork; + pthread_mutex_t mutex_dead; + t_philo *philo; + int i; threads = malloc(config->nbr_philo * sizeof(pthread_t)); if (threads == NULL) return (1); + pthread_mutex_init(&mutex, NULL); + philo = ft_calloc(sizeof(t_philo), 1); + if (!philo) + return (1); + philo->conf = *config; + philo->mutex = mutex; i = -1; - philo.conf = *config; while (++i < config->nbr_philo) { - philo.id = i; - if (pthread_create(&threads[i], NULL, (void *)&philosopher, &philo) != 0) + philo->id = &i; + if (pthread_create(&threads[i], NULL, (void *)&philosopher, philo) != 0) { write(2, "Error thread creation\n", 21); return (1); } - write(1, "Thread created\n", 15); } while (--i > -1) { @@ -41,7 +58,7 @@ int manage_threads(t_config *config) write(2, "Error thread finish\n", 20); return (1); } - write(1, "Thread finish execution\n", 24); } + pthread_mutex_destroy(&mutex); return (0); } diff --git a/utils/Makefile b/utils/Makefile index a4d935f..76e2e06 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -6,12 +6,12 @@ # By: erey-bet +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/09/27 04:19:30 by erey-bet #+# #+# # -# Updated: 2023/03/08 21:29:13 by erey-bet ### ########.fr # +# Updated: 2023/03/13 14:03:30 by erey-bet ### ########.fr # # # # **************************************************************************** # SRCS = ft_atoi.c ft_calloc.c ft_putstr_fd.c ft_putnbr_fd.c ft_atoi_check.c \ -ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c +ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c OBJS = ${SRCS:.c=.o} CC = clang CFLAGS = -Wall -Wextra -Werror