Further improve philos memory, make 1 philo work
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 10:50:12 +0000 (12:50 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 10:50:12 +0000 (12:50 +0200)
1 philo did not ever exit because he did not ever get out of waiting
for a fork. This is rectified by main posting new forks at the end.

Philo cleanup has been refactored to ease with the error printing.

philo_bonus/main.c
philo_bonus/philo.c
philo_bonus/philo.h

index 477aba35aab8a8f51451ff10e0b1cd1f28200bf8..ef7cae7189fa6bb6a440055fd61830bb569f6f99 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 12:05:00 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 12:46:31 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -100,6 +100,7 @@ void        wait_for_end(unsigned int philo_count, t_sems *semaphores)
        {
                sem_wait(semaphores->end);
                sem_post(semaphores->fed);
+               sem_post(semaphores->forks);
                --philo_count;
        }
        sem_post(semaphores->term);
index 30f92290b2bfaa92599ad78ef5a8a709c1f02e5e..bb44b9c032b272ad4fe65dc33dd0b0bf97feb615 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 12:08:48 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 12:49:14 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stdio.h>
 #include <stdlib.h>
 
-static char    *open_semaphores(t_philo *philo)
+static int     open_semaphores(t_philo *philo)
 {
-       char    *sem_name;
-
-       sem_name = create_philo_sem_name(philo->id);
-       if (!sem_name)
-               return (NULL);
-       philo->philo_sem = sem_open(sem_name, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1);
+       philo->philo_sem_name = create_philo_sem_name(philo->id);
+       if (!philo->philo_sem_name)
+               return (1);
+       philo->philo_sem = sem_open(philo->philo_sem_name, O_CREAT | O_EXCL,
+                       S_IRUSR | S_IWUSR, 1);
        philo->semaphores.forks = sem_open(FORKS_SEM, 0);
        philo->semaphores.fed = sem_open(WELL_FED_SEM, 0);
        philo->semaphores.death = sem_open(DEATH_SEM, 0);
@@ -35,11 +34,9 @@ static char  *open_semaphores(t_philo *philo)
                        || philo->semaphores.death == SEM_FAILED
                        || philo->semaphores.term == SEM_FAILED
                        || philo->semaphores.end == SEM_FAILED)
-       {
-               free(sem_name);
-               return (NULL);
-       }
-       return (sem_name);
+               return (2);
+       philo->semaphores.check_end = NULL;
+       return (0);
 }
 
 static void    philo_think(t_philo *philo)
@@ -75,30 +72,36 @@ static void philo_sleep(t_philo *philo)
        return ;
 }
 
+static void    clear_philo(t_philo *philo)
+{
+       close_semaphores(&philo->semaphores);
+       if (philo->philo_sem_name && philo->philo_sem != SEM_FAILED)
+       {
+               sem_close(philo->philo_sem);
+               sem_unlink(philo->philo_sem_name);
+       }
+       free(philo->philo_sem_name);
+       return ;
+}
+
 static void    error_exit(t_philo *philo, int exit_code)
 {
        sem_wait(philo->semaphores.term);
        printf("An error as occured.\n");
        sem_post(philo->semaphores.death);
        sem_post(philo->semaphores.end);
-       close_semaphores(&philo->semaphores);
+       clear_philo(philo);
        exit(exit_code);
 }
 
 void   be_a_philosopher(t_philo *philo)
 {
        pthread_t       watchers[2];
-       char            *philo_sem_name;
 
-       philo_sem_name = open_semaphores(philo);
-       if (!philo_sem_name)
+       if (open_semaphores(philo))
                error_exit(philo, 2);
        if (create_watchers(philo, watchers))
-       {
-               sem_unlink(philo_sem_name);
-               free(philo_sem_name);
                error_exit(philo, 3);
-       }
        while (is_alive(philo))
        {
                philo_think(philo);
@@ -106,8 +109,6 @@ void        be_a_philosopher(t_philo *philo)
                philo_sleep(philo);
        }
        destroy_watchers(watchers);
-       close_semaphores(&philo->semaphores);
-       sem_unlink(philo_sem_name);
-       free(philo_sem_name);
+       clear_philo(philo);
        exit(0);
 }
index 8e311ae101cd23f8821d718e6bda5cffc86fab54..46020191d410b8fd70a14dbb7ecc62c1821772ec 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 10:40:40 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 12:18:20 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -65,6 +65,7 @@ typedef struct s_philosopher
        int                             alive;
        useconds_t              last_eaten;
        t_sems                  semaphores;
+       char                    *philo_sem_name;
        sem_t                   *philo_sem;
        t_settings              settings;
 }                                      t_philo;