Rewrite semaphore initialization, add end semaphore
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 28 May 2024 12:48:39 +0000 (14:48 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 28 May 2024 14:07:18 +0000 (16:07 +0200)
It is better to create semaphores at 0 than at some value and then
subtracting them to 0.

The end semaphore will be used to signal to philosophers they should
end. This mechanism was chosen instead of kill function, because the
philosophers have to clean after themselves and signal
handlers cannot be used.

philo_bonus/mem_management.c
philo_bonus/philo.h

index 1fc290d9c07b3bae492d3c0a535e37abca2ef628..b79c3d55c10047224ef369e055310067fe7a4f03 100644 (file)
@@ -22,35 +22,27 @@ static int  open_semaphores(unsigned int count, t_sems *semaphores)
        semaphores->forks = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
                        S_IRUSR | S_IWUSR, count);
        semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
-                        S_IRUSR | S_IWUSR, count);
+                       S_IRUSR | S_IWUSR, 0);
        semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
-                        S_IRUSR | S_IWUSR, count);
+                       S_IRUSR | S_IWUSR, 0);
        semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL,
-                        S_IRUSR | S_IWUSR, 1);
+                       S_IRUSR | S_IWUSR, 0);
+       semaphores->end = sem_open(END_SEM, O_CREAT | O_EXCL,
+                       S_IRUSR | S_IWUSR, 0);
        if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED
-                       || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED)
+                       || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED
+                       || semaphores->end == SEM_FAILED)
                return (1);
        return (0);
 }
 
 int    init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
 {
-       size_t  i;
-
        *philo_pids = malloc(count * sizeof(**philo_pids));
        if (!*philo_pids)
                return (1);
        if (open_semaphores(count, semaphores))
                return (1);
-       i = 0;
-       while (i < count)
-       {
-               (*philo_pids)[i] = 0;
-               sem_wait(semaphores->death);
-               sem_wait(semaphores->fed);
-               ++i;
-       }
-       sem_wait(semaphores->term);
        return (0);
 }
 
@@ -60,6 +52,7 @@ void  close_semaphores(t_sems *semaphores)
        sem_close(semaphores->fed);
        sem_close(semaphores->death);
        sem_close(semaphores->term);
+       sem_close(semaphores->end);
        return ;
 }
 
@@ -70,6 +63,7 @@ void  cleanup(pid_t *philo_pids, t_sems *semaphores)
        sem_unlink(WELL_FED_SEM);
        sem_unlink(DEATH_SEM);
        sem_unlink(TERM_SEM);
+       sem_unlink(END_SEM);
        free(philo_pids);
        return ;
 }
index b7d461c19f86f2240f94cd4b94b33d1726f1b590..b31e6591b14c7afc6de0cb7663af4b814cc51d82 100644 (file)
@@ -23,6 +23,7 @@
 # define WELL_FED_SEM "philosophers_fed"
 # define DEATH_SEM "philosophers_death"
 # define TERM_SEM "philosophers_terminal"
+# define END_SEM "philosophers_end"
 
 //     The .start member holds the result of gettimeofday
 //     Every other member that holds time info counts
@@ -44,6 +45,7 @@ typedef struct s_important_semaphores
        sem_t   *fed;
        sem_t   *death;
        sem_t   *term;
+       sem_t   *end;
 }                      t_sems;
 
 //     As mutex is not available in bonus, the idea is to have