Change watch_for_starvation to not print after end
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 12:29:08 +0000 (14:29 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 12:29:08 +0000 (14:29 +0200)
This change may prevent some printing but I am not sure whther it stops
it all. Further testing (or thinking) needed.

philo_bonus/watchers.c

index 817649929b1ceaaf0fd83ea794993f5ffd7325db..0ae0db2d129f399e0554cf4f9f4b3c940e4a4c62 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/24 08:49:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 11:39:51 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 14:25:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -36,22 +36,23 @@ static void *watch_for_starvation(void *philo_void)
        philo = philo_void;
        sem_wait(philo->philo_sem);
        time = time_till_death(philo);
-       while (time > 0)
+       while (time > 0 && philo->alive)
        {
-               if (!philo->alive)
-               {
-                       sem_post(philo->philo_sem);
-                       return (NULL);
-               }
                sem_post(philo->philo_sem);
                usleep(time / 2);
                sem_wait(philo->philo_sem);
                time = time_till_death(philo);
        }
        sem_wait(philo->semaphores.term);
-       print_timestamp(philo, "died");
-       philo->alive = 0;
-       sem_post(philo->semaphores.death);
+       if (philo->alive)
+       {
+               print_timestamp(philo, "died");
+               philo->alive = 0;
+               sem_post(philo->semaphores.death);
+               sem_post(philo->semaphores.end);
+       }
+       else
+               sem_post(philo->semaphores.term);
        sem_post(philo->philo_sem);
        return (NULL);
 }