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.c110
1 files changed, 58 insertions, 52 deletions
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index 8790140..64b1004 100644
--- a/philo_three/src/main.c
+++ b/philo_three/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */
-/* Updated: 2021/01/08 20:23:28 by charles ### ########.fr */
+/* Updated: 2021/01/09 15:57:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ static sem_t *st_sem_create(char *name, unsigned int value)
return (sem_open(name, O_CREAT | O_EXCL, 0700, value));
}
-static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
+static int st_destroy(t_philo_conf *conf, pid_t *pids, int philo_num)
{
int i;
@@ -29,92 +29,98 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num)
kill(pids[i], SIGKILL);
free(pids);
}
- sem_close(sems->forks);
+ sem_close(conf->forks);
sem_unlink(PHILO_SEM_NAME);
- sem_close(sems->sem_stdout);
+ sem_close(conf->sem_stdout);
sem_unlink(PHILO_SEM_STDOUT_NAME);
- sem_close(sems->sem_finish);
+ sem_close(conf->sem_finish);
sem_unlink(PHILO_SEM_FINISH_NAME);
- sem_close(sems->sem_start);
+ 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(sems->sem_grab);
+ sem_close(conf->sem_grab);
sem_unlink(PHILO_SEM_GRAB_NAME);
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_args *args, t_sems *sems, pid_t **pids, t_time initial_time)
+ t_philo_conf *conf, pid_t **pids, t_time initial_time)
{
t_philo philo;
int i;
- sems->sem_stdout = SEM_FAILED;
- sems->sem_finish = SEM_FAILED;
- sems->sem_start = SEM_FAILED;
- if ((sems->forks =
- st_sem_create(PHILO_SEM_NAME, args->philo_num)) == SEM_FAILED
- || (sems->sem_stdout =
- st_sem_create(PHILO_SEM_STDOUT_NAME, 1)) == SEM_FAILED
- || (sems->sem_finish =
- st_sem_create(PHILO_SEM_FINISH_NAME,
- args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
- || (sems->sem_start =
- st_sem_create(PHILO_SEM_START_NAME, args->philo_num)) == SEM_FAILED
- || (sems->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED
- || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
- return (st_destroy(sems, *pids, 0));
+ conf->sem_stdout = SEM_FAILED;
+ conf->sem_finish = 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));
i = -1;
- while (++i < args->philo_num)
- sem_wait(sems->sem_start);
+ while (++i < conf->philo_num)
+ sem_wait(conf->sem_start);
i = -1;
- while (++i < args->philo_num)
+ while (++i < conf->philo_num)
{
- philo.conf = args;
+ philo.conf = conf;
philo.id = i + 1;
philo.initial_time = initial_time;
if (((*pids)[i] = child_start(&philo)) == -1)
- return (st_destroy(sems, *pids, i));
+ return (st_destroy(conf, *pids, i));
}
i = -1;
- while (++i < args->philo_num)
- sem_post(sems->sem_start);
+ while (++i < conf->philo_num)
+ sem_post(conf->sem_start);
return (0);
}
-static void st_wait(t_philo_args *args, t_sems *sems)
+static void *st_routine_meal_num(t_philo_conf *conf)
{
long int i;
- if (args->meal_num == -1)
- {
- sem_wait(sems->sem_finish);
- sem_wait(sems->sem_finish);
- }
- else
- {
- i = -1;
- while (++i < args->philo_num)
- sem_wait(sems->sem_finish);
- i = -1;
- while (++i < args->philo_num)
- sem_wait(sems->sem_finish);
- }
+ 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)
{
- t_philo_args args;
- t_sems sems;
+ t_philo_conf conf;
pid_t *pids;
- if (!parse_args(&args, argc, argv))
+ if (!parse_args((t_philo_args*)&conf, argc, argv))
return (1);
- if (args.philo_num == 0 || args.meal_num == 0)
+ if (conf.philo_num == 0 || conf.meal_num == 0)
return (0);
pids = NULL;
- if (st_setup(&args, &sems, &pids, h_time_now()) != 0)
+ if (st_setup(&conf, &pids, h_time_now()) != 0)
return (1);
- st_wait(&args, &sems);
- st_destroy(&sems, pids, args.philo_num);
+ if (conf.meal_num != -1)
+ {
+ 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);
+ st_destroy(&conf, pids, conf.philo_num);
return (0);
}