aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/child.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-02 12:08:59 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-02 12:08:59 +0100
commit7aab8f34e8a238ce0688bf9f7e7f0b731902580f (patch)
tree6ab0541168d07c2755c51d9ef6875012105816e8 /philo_three/src/child.c
parent280dace8bf97242151faf8297af31de67f77085c (diff)
downloadphilosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.tar.gz
philosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.tar.bz2
philosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.zip
Updated philo_two with philo_three logic, Norming philo_two and philo_three
Diffstat (limited to 'philo_three/src/child.c')
-rw-r--r--philo_three/src/child.c44
1 files changed, 25 insertions, 19 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);