Add missing index increment, change cleanup order
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 09:19:16 +0000 (11:19 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 09:19:16 +0000 (11:19 +0200)
The program only checked the forst philo for starvation.

The change in the cleanup order is needed because without the change
(forks) mutexes were deinitialized before joining the (philo) threads.
This could lead to a philo waiting for a fork, which has just been
cleared, leading to undefined behavior (endless wait). When the main
thread joins all the threads it gets gets stuck in the one waitin for
a fork.

philo/main.c
philo/mem_management.c

index 97ef11ba8cb728e33e25caa2ea6776819664b2ac..c19c92aa44286b9dc81029a42604189c13a43a69 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 10:24:12 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 11:15:59 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -67,6 +67,7 @@ void  watch_philosophers(t_diner *diner)
                                print_timestamp(diner->philos + i, "died");
                                return ;
                        }
+                       ++i;
                }
        }
 }
index aaa9464b5a31614dd1e7629c82839200becf7b80..0b245b484ae4b9879eb5e7aac5b2ae00bc2ed084 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 09:39:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 11:05:18 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 11:18:43 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -54,16 +54,16 @@ void        cleanup(t_diner *diner, pthread_t *threads)
        destroy_mutex(&diner->setting.terminal_lock);
        destroy_mutex(&diner->setting.end_lock);
        i = 0;
-       if (diner->forks)
-               while (i < count)
-                       destroy_mutex(diner->forks + (i++));
-       i = 0;
        if (diner->philos)
                while (i < count)
                {
                        pthread_join(threads[i], NULL);
                        destroy_mutex(&diner->philos[i++].philo_lock);
                }
+       i = 0;
+       if (diner->forks)
+               while (i < count)
+                       destroy_mutex(diner->forks + (i++));
        free(diner->philos);
        free(diner->forks);
        free(threads);