aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-09 16:07:01 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-09 16:07:01 +0100
commit802ea8347d1ceb7a02cf279359b3b101106fab94 (patch)
tree3f8b38056e29d94a813351fb99cc911d89f8df3d /philo_two/src/main.c
parentebbd81e4312d945b359d2761acfeb613da1f98c8 (diff)
downloadphilosophers-802ea8347d1ceb7a02cf279359b3b101106fab94.tar.gz
philosophers-802ea8347d1ceb7a02cf279359b3b101106fab94.tar.bz2
philosophers-802ea8347d1ceb7a02cf279359b3b101106fab94.zip
Added separate thread to check if max meal num is reached independent from philosopher death
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c57
1 files changed, 29 insertions, 28 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 034518c..fb441dc 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,17 +6,18 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2021/01/08 20:16:20 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:35:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-#define PHILO_SEM_NAME "semaphore_philo_two"
-#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout"
-#define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish"
-#define PHILO_SEM_START_NAME "semaphore_philo_two_start"
-#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab"
+#define PHILO_SEM_NAME "semaphore_philo_two"
+#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout"
+#define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish"
+#define PHILO_SEM_START_NAME "semaphore_philo_two_start"
+#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab"
+#define PHILO_SEM_MEAL_NUM_NAME "semaphore_philo_two_meal_num"
static int st_destroy(
t_philo *philos,
@@ -25,6 +26,7 @@ static int st_destroy(
sem_unlink(PHILO_SEM_NAME);
sem_unlink(PHILO_SEM_STDOUT_NAME);
sem_unlink(PHILO_SEM_FINISH_NAME);
+ sem_unlink(PHILO_SEM_MEAL_NUM_NAME);
sem_unlink(PHILO_SEM_START_NAME);
sem_unlink(PHILO_SEM_GRAB_NAME);
free(philos);
@@ -53,20 +55,19 @@ void *routine_flush(t_philo_conf *conf)
static int st_setup(
t_philo_conf *conf,
t_philo **philos,
- sem_t **forks,
pthread_t **threads)
{
long int i;
- if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) ||
+ if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->sem_forks) ||
!st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) ||
- !st_sem_create(PHILO_SEM_FINISH_NAME,
- conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish) ||
+ !st_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) ||
+ !st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num, &conf->sem_meal_num) ||
!st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start) ||
!st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab))
return (1);
*threads = NULL;
- if ((*philos = routine_create_philos(conf, *forks)) == NULL ||
+ if ((*philos = routine_create_philos(conf)) == NULL ||
(*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
return (st_destroy(*philos, *threads));
i = -1;
@@ -90,24 +91,21 @@ static int st_setup(
return (0);
}
-static void st_wait(t_philo_conf *conf)
+static void *st_routine_meal_num(t_philo_conf *conf)
{
long int i;
if (conf->meal_num == -1)
- {
- sem_wait(conf->sem_finish);
- sem_wait(conf->sem_finish);
- }
- else
- {
- i = -1;
- while (++i < conf->philo_num)
- sem_wait(conf->sem_finish);
- i = -1;
- while (++i < conf->philo_num)
- sem_wait(conf->sem_finish);
- }
+ return (NULL);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ i = -1;
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_meal_num);
+ sem_wait(conf->sem_stdout);
+ sem_post(conf->sem_finish);
+ return (NULL);
}
int main(int argc, char **argv)
@@ -115,19 +113,22 @@ int main(int argc, char **argv)
long int i;
t_philo_conf conf;
t_philo *philos;
- sem_t *forks;
pthread_t *threads;
if (!parse_args((t_philo_args *)&conf, argc, argv))
return (1);
if (conf.philo_num == 0 || conf.meal_num == 0)
return (0);
- if (st_setup(&conf, &philos, &forks, &threads) != 0)
+ if (st_setup(&conf, &philos, &threads) != 0)
return (1);
pthread_t thread_flush;
pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf);
pthread_detach(thread_flush);
- st_wait(&conf);
+ pthread_t thread_meal_num;
+ pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf);
+ pthread_detach(thread_meal_num);
+ sem_wait(conf.sem_finish);
+ sem_wait(conf.sem_finish);
philo_put_flush();
i = -1;
while (++i < conf.philo_num)