From 8285f3b0a1c8b8593da03f232620c307be437634 Mon Sep 17 00:00:00 2001 From: Matthos Denys Date: Sun, 16 May 2021 22:43:59 +0300 Subject: [PATCH] add new func --- philo_two/includes/philo_header.h | 1 + philo_two/srcs/logic.c | 6 +++--- philo_two/srcs/prepare_simulation.c | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/philo_two/includes/philo_header.h b/philo_two/includes/philo_header.h index 94f8991..3fdbb1e 100644 --- a/philo_two/includes/philo_header.h +++ b/philo_two/includes/philo_header.h @@ -52,6 +52,7 @@ typedef struct s_philo t_all *all_struct; int is_eat; sem_t *eat_sem; + char *name; } t_philo; typedef struct s_philo_data diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index aa62b1f..4057c47 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -27,7 +27,7 @@ void take_forks(t_philo *philo) { t_all *all; all = philo->all_struct; - pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]); + pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]); // TODO передать на на семафор messages(philo->all_struct, philo, "has taken a fork"); pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]); messages(philo->all_struct, philo, "has taken a fork"); @@ -44,8 +44,8 @@ void sleeping(t_philo *philo) t_all *all; all = philo->all_struct; messages(philo->all_struct, philo, "sleeping"); - pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]); - pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]); + // pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]); + // pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]); ft_msleep(philo->all_struct->time_to_sleep); } diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index 6bbcf1d..f54e994 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -40,6 +40,10 @@ void get_fork_for_philo(t_all *all) while(i <= all->number_of_philosophers) { + all->data_philo->philors[i].name = sem_set_name("philo", i); + all->data_philo->philors[i].eat_sem = reopen_sem(all->data_philo->philors[i].name, 1); + if (all->data_philo->philors[i].eat_sem == SEM_FAILED) + exit(0); // TODO fix this all->data_philo->philors[i].died = 0; all->data_philo->philors[i].nbr_philo = i; all->data_philo->philors[i].worked = 0;