aboutsummaryrefslogtreecommitdiff
path: root/philo_two/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'philo_two/src/main.c')
-rw-r--r--philo_two/src/main.c72
1 files changed, 38 insertions, 34 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 85994da..e88ccac 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,42 +6,33 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2021/01/01 15:57:40 by charles ### ########.fr */
+/* Updated: 2021/01/02 12:05:01 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-#define PHILO_SEM_NAME "semaphore_philo_two"
-#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout"
-#define PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME "semaphore_philo_two_meal_num"
+#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"
static int st_destroy(
- sem_t *forks,
t_philo *philos,
- pthread_t *threads,
- t_philo_conf *conf)
+ pthread_t *threads)
{
- /* int i; */
- /* */
- /* i = -1; */
- /* while (++i < conf->philo_num) */
- /* sem_post(forks); */
- sem_close(forks);
sem_unlink(PHILO_SEM_NAME);
- sem_close(conf->sem_stdout);
sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_close(conf->sem_meal_num_finished_counter);
- sem_unlink(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME);
+ sem_unlink(PHILO_SEM_FINISH_NAME);
free(philos);
free(threads);
return (1);
}
-static sem_t *st_sem_create(const char *name, unsigned int value)
+static bool st_sem_create(const char *name, unsigned int value, sem_t **sem)
{
sem_unlink(name);
- return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
+ return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value))
+ != SEM_FAILED);
}
static int st_setup(
@@ -52,21 +43,15 @@ static int st_setup(
{
long int i;
- *forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num);
- if (*forks == SEM_FAILED)
- return (1);
- conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1);
- if (conf->sem_stdout == SEM_FAILED)
- return (1);
- conf->sem_meal_num_finished_counter = st_sem_create(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, 1);
- if (conf->sem_meal_num_finished_counter == SEM_FAILED)
+ if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, 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))
return (1);
*threads = NULL;
if ((*philos = routine_create_philos(conf, *forks)) == NULL ||
(*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
- return (st_destroy(*forks, *philos, *threads, conf));
- conf->all_alive = true;
- conf->meal_num_finished_counter = 0;
+ return (st_destroy(*philos, *threads));
i = -1;
while (++i < conf->philo_num)
{
@@ -75,13 +60,34 @@ static int st_setup(
{
while (--i >= 0)
pthread_detach((*threads)[i]);
- return (st_destroy(*forks, *philos, *threads, conf));
+ return (st_destroy(*philos, *threads));
}
usleep(200);
}
return (0);
}
+static void st_wait(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);
+ sem_wait(conf->sem_stdout);
+ }
+}
+
int main(int argc, char **argv)
{
long int i;
@@ -96,12 +102,10 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&conf, &philos, &forks, &threads) != 0)
return (1);
- while (!philo_finished(&conf))
- ;
- conf.all_alive = false;
+ st_wait(&conf);
i = -1;
while (++i < conf.philo_num)
pthread_detach(threads[i]);
- st_destroy(forks, philos, threads, &conf);
+ st_destroy(philos, threads);
return (0);
}