aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src
diff options
context:
space:
mode:
Diffstat (limited to 'philo_three/src')
-rw-r--r--philo_three/src/child.c44
-rw-r--r--philo_three/src/main.c44
2 files changed, 50 insertions, 38 deletions
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index e79f782..bce5d21 100644
--- a/philo_three/src/child.c
+++ b/philo_three/src/child.c
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */
-/* Updated: 2021/01/01 16:31:24 by charles ### ########.fr */
+/* Updated: 2021/01/02 11:58:03 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,39 +26,45 @@ void *routine_death(t_philo *philo)
return (NULL);
}
+void st_child_loop(t_philo *philo)
+{
+ long int eat_counter;
+
+ eat_counter = 0;
+ while (true)
+ {
+ event_take_fork(philo);
+ event_take_fork(philo);
+ event_eat(philo);
+ philo->time_last_eat = h_time_now();
+ eat_counter++;
+ if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
+ {
+ sem_wait(philo->sem_stdout);
+ sem_post(philo->sem_finish);
+ sem_post(philo->sem_stdout);
+ }
+ event_sleep(philo);
+ event_think(philo);
+ }
+}
+
pid_t child_start(t_philo *philo)
{
pthread_t thread_death;
pid_t pid;
- long int eat_counter;
if ((pid = fork()) == -1)
return (-1);
if (pid == 0)
{
- eat_counter = 0;
philo->forks = sem_open(PHILO_SEM_NAME, 0);
philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0);
philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0);
philo->time_last_eat = h_time_now();
pthread_create(&thread_death, NULL, (t_routine)routine_death, philo);
event_think(philo);
- while (true)
- {
- event_take_fork(philo);
- event_take_fork(philo);
- event_eat(philo);
- philo->time_last_eat = h_time_now();
- eat_counter++;
- if (philo->conf->meal_num != -1 && eat_counter == philo->conf->meal_num)
- {
- sem_wait(philo->sem_stdout);
- sem_post(philo->sem_finish);
- sem_post(philo->sem_stdout);
- }
- event_sleep(philo);
- event_think(philo);
- }
+ st_child_loop(philo);
exit(0);
}
return (pid);
diff --git a/philo_three/src/main.c b/philo_three/src/main.c
index e5e8dd2..eb69ae8 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/01 16:30:33 by charles ### ########.fr */
+/* Updated: 2021/01/02 12:07:14 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,7 +51,8 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
|| (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
+ st_sem_create(PHILO_SEM_FINISH_NAME,
+ args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED
|| (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL)
return (st_destroy(sems, *pids, 0));
i = -1;
@@ -66,12 +67,32 @@ static int st_setup(t_philo_args *args, t_sems *sems, pid_t **pids)
return (0);
}
+static void st_wait(t_philo_args *args, t_sems *sems)
+{
+ 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);
+ sem_wait(sems->sem_stdout);
+ }
+}
+
int main(int argc, char **argv)
{
t_philo_args args;
t_sems sems;
pid_t *pids;
- long int i;
if (!parse_args(&args, argc, argv))
return (1);
@@ -79,22 +100,7 @@ int main(int argc, char **argv)
return (0);
if (st_setup(&args, &sems, &pids) != 0)
return (1);
- if (args.meal_num == -1)
- {
- sem_wait(sems.sem_finish);
- sem_wait(sems.sem_finish);
- }
- else
- {
- /* printf("eat %d\n", args.meal_num); */
- i = -1;
- while (++i < args.philo_num)
- sem_wait(sems.sem_finish);
- i = -1;
- while (++i < args.philo_num)
- sem_wait(sems.sem_finish);
- sem_wait(sems.sem_stdout);
- }
+ st_wait(&args, &sems);
st_destroy(&sems, pids, args.philo_num);
return (0);
}