aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'philo_three/src/main.c')
-rw-r--r--philo_three/src/main.c94
1 files changed, 43 insertions, 51 deletions
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index e3f3022..a3128a0 100644
--- a/philo_three/src/main.c
+++ b/philo_three/src/main.c
@@ -6,21 +6,15 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */
-/* Updated: 2021/01/10 13:23:54 by cacharle ### ########.fr */
+/* Updated: 2021/01/10 14:25:49 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo_three.h"
-static sem_t *st_sem_create(char *name, unsigned int value)
+static int st_destroy(t_philo_conf *conf, pid_t *pids, long int philo_num)
{
- sem_unlink(name);
- return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
-}
-
-static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num)
-{
- int i;
+ long int i;
if (pids != NULL)
{
@@ -29,45 +23,42 @@ static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num)
kill(pids[i], SIGKILL);
free(pids);
}
- sem_close(conf->forks);
- sem_unlink(PHILO_SEM_NAME);
- sem_close(conf->sem_stdout);
- sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_close(conf->sem_finish);
- sem_unlink(PHILO_SEM_FINISH_NAME);
- sem_close(conf->sem_meal_num);
- sem_unlink(PHILO_SEM_MEAL_NUM_NAME);
- sem_close(conf->sem_start);
- sem_unlink(PHILO_SEM_START_NAME);
- sem_close(conf->sem_grab);
- sem_unlink(PHILO_SEM_GRAB_NAME);
+ h_destroy_sem(PHILO_SEM_NAME, conf->forks);
+ h_destroy_sem(PHILO_SEM_STDOUT_NAME, conf->sem_stdout);
+ h_destroy_sem(PHILO_SEM_FINISH_NAME, conf->sem_finish);
+ h_destroy_sem(PHILO_SEM_MEAL_NUM_NAME, conf->sem_meal_num);
+ h_destroy_sem(PHILO_SEM_START_NAME, conf->sem_start);
+ h_destroy_sem(PHILO_SEM_GRAB_NAME, conf->sem_grab);
return (1);
}
-/* static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) */
-/* { */
-/* sem_unlink(name); */
-/* return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value)) */
-/* != SEM_FAILED); */
-/* } */
-
-static int st_setup(
- t_philo_conf *conf, pid_t **pids, t_time initial_time)
+static int st_setup(t_philo_conf *conf, pid_t **pids)
{
- t_philo philo;
- int i;
-
+ conf->forks = SEM_FAILED;
conf->sem_stdout = SEM_FAILED;
conf->sem_finish = SEM_FAILED;
+ conf->sem_meal_num = SEM_FAILED;
conf->sem_start = SEM_FAILED;
- if ((conf->forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num)) == SEM_FAILED
- || (conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
- || (conf->sem_finish = st_sem_create(PHILO_SEM_FINISH_NAME, 1)) == SEM_FAILED
- || (conf->sem_meal_num = st_sem_create(PHILO_SEM_MEAL_NUM_NAME, conf->philo_num)) == SEM_FAILED
- || (conf->sem_start = st_sem_create(PHILO_SEM_START_NAME, conf->philo_num)) == SEM_FAILED
- || (conf->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED
- || (*pids = malloc(sizeof(pid_t) * conf->philo_num)) == NULL)
- return (st_destroy(conf, *pids, 0));
+ conf->sem_grab = SEM_FAILED;
+ *pids = NULL;
+ if (!h_sem_create(PHILO_SEM_NAME, conf->philo_num, &conf->forks) ||
+ !h_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) ||
+ !h_sem_create(PHILO_SEM_FINISH_NAME, 1, &conf->sem_finish) ||
+ !h_sem_create(PHILO_SEM_MEAL_NUM_NAME,
+ conf->philo_num, &conf->sem_meal_num) ||
+ !h_sem_create(PHILO_SEM_START_NAME,
+ conf->philo_num, &conf->sem_start) ||
+ !h_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab) ||
+ (*pids = malloc(sizeof(pid_t) * conf->philo_num)) == NULL)
+ return (1);
+ return (0);
+}
+
+static int st_start(t_philo_conf *conf, pid_t *pids, t_time initial_time)
+{
+ t_philo philo;
+ long int i;
+
i = -1;
while (++i < conf->philo_num)
sem_wait(conf->sem_start);
@@ -77,8 +68,8 @@ static int st_setup(
philo.conf = conf;
philo.id = i + 1;
philo.initial_time = initial_time;
- if (((*pids)[i] = child_start(&philo)) == -1)
- return (st_destroy(conf, *pids, i));
+ if ((pids[i] = child_start(&philo)) == -1)
+ return (1);
}
i = -1;
while (++i < conf->philo_num)
@@ -86,9 +77,9 @@ static int st_setup(
return (0);
}
-static void *st_routine_meal_num(t_philo_conf *conf)
+static void *st_routine_meal_num(t_philo_conf *conf)
{
- long int i;
+ long int i;
i = -1;
while (++i < conf->philo_num)
@@ -101,22 +92,23 @@ static void *st_routine_meal_num(t_philo_conf *conf)
return (NULL);
}
-int main(int argc, char **argv)
+int main(int argc, char **argv)
{
t_philo_conf conf;
pid_t *pids;
+ 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);
- pids = NULL;
- if (st_setup(&conf, &pids, h_time_now()) != 0)
- return (1);
+ if (st_setup(&conf, &pids) != 0 || st_start(&conf, pids, h_time_now()) != 0)
+ return (st_destroy(&conf, pids, conf.philo_num));
if (conf.meal_num != -1)
{
- pthread_t thread_meal_num;
- pthread_create(&thread_meal_num, NULL, (t_routine)st_routine_meal_num, &conf);
+ if (pthread_create(&thread_meal_num,
+ NULL, (t_routine)st_routine_meal_num, &conf) != 0)
+ return (st_destroy(&conf, pids, conf.philo_num));
pthread_detach(thread_meal_num);
}
sem_wait(conf.sem_finish);