aboutsummaryrefslogtreecommitdiff
path: root/philo_three/src/child.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-01 16:39:36 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-01 16:39:36 +0100
commita73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb (patch)
treeee14275a0f1ad9b8ddfa8feee1ee77ce0de5f285 /philo_three/src/child.c
parentb9d93edf40f228fcc6e18e9e6d0a1c5db498c004 (diff)
downloadphilosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.gz
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.tar.bz2
philosophers-a73b1e33ae3a4f55e3dd19defa1b853bc58f1eeb.zip
Fixing meal num for philo_three
Diffstat (limited to 'philo_three/src/child.c')
-rw-r--r--philo_three/src/child.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/philo_three/src/child.c b/philo_three/src/child.c
index 2b17960..e79f782 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: 2020/12/31 19:12:12 by charles ### ########.fr */
+/* Updated: 2021/01/01 16:31:24 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,14 +30,16 @@ 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_dead = sem_open(PHILO_SEM_DEAD_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);
@@ -45,8 +47,15 @@ pid_t child_start(t_philo *philo)
{
event_take_fork(philo);
event_take_fork(philo);
- philo->time_last_eat = h_time_now();
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);
}