Make semaphores close properly, fix array access
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 23 May 2024 07:32:26 +0000 (09:32 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 23 May 2024 07:32:26 +0000 (09:32 +0200)
philo_bonus/mem_management.c

index 25d5e28b0b5fd74a5c2b4df138656948f5e38bb7..a8a3cd8710b938511e0552b522a4087f8d317fe5 100644 (file)
@@ -48,7 +48,7 @@ int   init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num])
        i = 0;
        while (i < count)
        {
-               *philo_pids[i] = 0;
+               (*philo_pids)[i] = 0;
                sem_wait(semaphores[death]);
                sem_wait(semaphores[fed]);
                ++i;
@@ -63,11 +63,11 @@ void        cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num])
 
        i = 0;
        while (i < sem_num)
-       {
-               if (semaphores[i] != SEM_FAILED)
-                       sem_close(semaphores[i]);
-               ++i;
-       }
+               sem_close(semaphores[i++]);
+       sem_unlink(FORKS_SEM);
+       sem_unlink(WELL_FED_SEM);
+       sem_unlink(DEATH_SEM);
+       sem_unlink(TERM_SEM);
        free(philo_pids);
        return ;
 }