aboutsummaryrefslogtreecommitdiff
path: root/philo_two
diff options
context:
space:
mode:
Diffstat (limited to 'philo_two')
-rw-r--r--philo_two/src/main.c102
-rw-r--r--philo_two/src/philo_two.h12
-rw-r--r--philo_two/src/routine.c63
-rw-r--r--philo_two/src/routine_meta.c41
4 files changed, 121 insertions, 97 deletions
diff --git a/philo_two/src/main.c b/philo_two/src/main.c
index 197ee5c..2861536 100644
--- a/philo_two/src/main.c
+++ b/philo_two/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */
-/* Updated: 2021/01/10 10:18:12 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 13:39:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,23 +19,18 @@
#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,
- pthread_t *threads)
+static int st_destroy(t_philo *philos, long int philo_num)
{
- /* long int i; */
-
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);
- /* i = -1; */
- /* while (++i < philos[0].conf->philo_num) */
- /* sem_unlink(philo_sem_eat_name(philo[i].id)); */
+ while (philo_num-- > 0)
+ sem_unlink(philo_sem_eat_name(
+ PHILO_SEM_EAT_PREFIX, philos[philo_num].id));
free(philos);
- free(threads);
return (1);
}
@@ -46,48 +41,37 @@ static bool st_sem_create(const char *name, unsigned int value, sem_t **sem)
!= SEM_FAILED);
}
-void *routine_flush(t_philo_conf *conf)
+static int st_setup(t_philo_conf *conf, t_philo **philos)
{
- while (true)
- {
- sem_wait(conf->sem_stdout);
- philo_put_flush();
- sem_post(conf->sem_stdout);
- usleep(250000);
- }
+ 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, 1, &conf->sem_finish) ||
+ !st_sem_create(PHILO_SEM_START_NAME,
+ conf->philo_num, &conf->sem_start) ||
+ !st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab) ||
+ !st_sem_create(PHILO_SEM_MEAL_NUM_NAME,
+ conf->philo_num, &conf->sem_meal_num))
+ return (st_destroy(NULL, 0));
+ if ((*philos = routine_create_philos(conf)) == NULL)
+ return (st_destroy(NULL, 0));
+ return (0);
}
-static int st_setup(
- t_philo_conf *conf,
- t_philo **philos,
- pthread_t **threads)
+static int st_start(t_philo_conf *conf, t_philo *philos)
{
long int i;
+ pthread_t thread;
- 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, 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)) == NULL ||
- (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL)
- return (st_destroy(*philos, *threads));
i = -1;
while (++i < conf->philo_num)
sem_wait(conf->sem_start);
i = -1;
while (++i < conf->philo_num)
{
- if (pthread_create(*threads + i, NULL,
- (t_routine)routine_philo, *philos + i) != 0)
- {
- while (--i >= 0)
- pthread_detach((*threads)[i]);
- return (st_destroy(*philos, *threads));
- }
+ if (pthread_create(&thread, NULL,
+ (t_routine)routine_philo, philos + i) != 0)
+ return (st_destroy(philos, conf->philo_num));
+ pthread_detach(thread);
}
conf->initial_time = h_time_now();
i = -1;
@@ -96,48 +80,30 @@ static int st_setup(
return (0);
}
-static void *st_routine_meal_num(t_philo_conf *conf)
-{
- long int i;
-
- if (conf->meal_num == -1)
- 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)
{
- long int i;
t_philo_conf conf;
t_philo *philos;
- pthread_t *threads;
+ pthread_t thread_flush;
+ pthread_t thread_meal_num;
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, &threads) != 0)
+ if (st_setup(&conf, &philos) != 0 || st_start(&conf, philos) != 0)
return (1);
- pthread_t thread_flush;
- pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf);
+ if (pthread_create(&thread_flush, NULL,
+ (t_routine)routine_flush, &conf) != 0)
+ return (st_destroy(philos, conf.philo_num));
pthread_detach(thread_flush);
- pthread_t thread_meal_num;
- pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, (void*)&conf);
+ if (pthread_create(&thread_meal_num, NULL,
+ (t_routine)routine_meal_num, &conf) != 0)
+ return (st_destroy(philos, conf.philo_num));
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)
- pthread_detach(threads[i]);
- st_destroy(philos, threads);
+ st_destroy(philos, conf.philo_num);
return (0);
}
diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h
index 0126430..a186d6c 100644
--- a/philo_two/src/philo_two.h
+++ b/philo_two/src/philo_two.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */
-/* Updated: 2021/01/10 10:03:58 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 13:36:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,6 +22,8 @@
# include "common.h"
+# define PHILO_SEM_EAT_PREFIX "semaphore_philo_two_eat_"
+
typedef struct
{
long int philo_num;
@@ -51,10 +53,16 @@ typedef struct
*/
void *routine_philo(t_philo *arg);
-void *routine_death(t_philo *arg);
t_philo *routine_create_philos(t_philo_conf *conf);
/*
+** routine_meta.c
+*/
+
+void *routine_flush(t_philo_conf *conf);
+void *routine_meal_num(t_philo_conf *conf);
+
+/*
** io.c
*/
diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c
index ab01958..ec06b19 100644
--- a/philo_two/src/routine.c
+++ b/philo_two/src/routine.c
@@ -6,13 +6,37 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */
-/* Updated: 2021/01/10 10:30:04 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 13:39:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_two.h"
-void *routine_philo(t_philo *arg)
+static void *st_routine_death(t_philo *arg)
+{
+ while (true)
+ {
+ sem_wait(arg->sem_eat);
+ if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death)
+ break ;
+ sem_post(arg->sem_eat);
+ usleep(2000);
+ }
+ event_die(arg);
+ return (NULL);
+}
+
+static void st_check_meal_num_finished(t_philo *arg, long int eat_counter)
+{
+ if (arg->conf->meal_num != -1 && eat_counter == arg->conf->meal_num)
+ {
+ sem_wait(arg->conf->sem_stdout);
+ sem_post(arg->conf->sem_meal_num);
+ sem_post(arg->conf->sem_stdout);
+ }
+}
+
+void *routine_philo(t_philo *arg)
{
long int eat_counter;
pthread_t thread_death;
@@ -20,8 +44,11 @@ void *routine_philo(t_philo *arg)
eat_counter = 0;
sem_wait(arg->conf->sem_start);
arg->time_last_eat = h_time_now();
- if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0)
+ if (pthread_create(
+ &thread_death, NULL, (t_routine)st_routine_death, arg) != 0)
return (NULL);
+ pthread_detach(thread_death);
+ arg->time_last_eat = h_time_now();
while (true)
{
sem_wait(arg->conf->sem_grab);
@@ -29,36 +56,15 @@ void *routine_philo(t_philo *arg)
event_take_fork(arg);
sem_post(arg->conf->sem_grab);
event_eat(arg);
- if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num)
- {
- sem_wait(arg->conf->sem_stdout);
- sem_post(arg->conf->sem_meal_num);
- sem_post(arg->conf->sem_stdout);
- }
+ eat_counter++;
+ st_check_meal_num_finished(arg, eat_counter);
event_sleep(arg);
event_think(arg);
}
- pthread_join(thread_death, NULL);
return (NULL);
}
-void *routine_death(t_philo *arg)
-{
- while (true)
- {
- sem_wait(arg->sem_eat);
- if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death)
- break ;
- sem_post(arg->sem_eat);
- usleep(2000);
- }
- event_die(arg);
- return (NULL);
-}
-
-#define PHILO_SEM_EAT_PREFIX "semaphore_philo_two_eat_"
-
-t_philo *routine_create_philos(t_philo_conf *conf)
+t_philo *routine_create_philos(t_philo_conf *conf)
{
long int i;
t_philo *philos;
@@ -76,6 +82,9 @@ t_philo *routine_create_philos(t_philo_conf *conf)
philos[i].sem_eat = sem_open(name, O_CREAT | O_EXCL, 0700, 1);
if (philos[i].sem_eat == SEM_FAILED)
{
+ while (i-- > 0)
+ sem_unlink(philo_sem_eat_name(
+ PHILO_SEM_EAT_PREFIX, philos[i].id));
free(philos);
return (NULL);
}
diff --git a/philo_two/src/routine_meta.c b/philo_two/src/routine_meta.c
new file mode 100644
index 0000000..8bbc67a
--- /dev/null
+++ b/philo_two/src/routine_meta.c
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* routine_meta.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2021/01/10 13:31:54 by cacharle #+# #+# */
+/* Updated: 2021/01/10 13:32:38 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "philo_two.h"
+
+void *routine_flush(t_philo_conf *conf)
+{
+ while (true)
+ {
+ sem_wait(conf->sem_stdout);
+ philo_put_flush();
+ sem_post(conf->sem_stdout);
+ usleep(250000);
+ }
+}
+
+void *routine_meal_num(t_philo_conf *conf)
+{
+ long int i;
+
+ if (conf->meal_num == -1)
+ 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);
+}