Change semaphores array for a structure
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 24 May 2024 13:26:10 +0000 (15:26 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 24 May 2024 13:33:40 +0000 (15:33 +0200)
This change is mainly done because norminette sees the old declaration
of semaphores[sem_num] as VLA. Instead of changing the enum, it may be
better to reimplement semaphores as a structure.
This may make a little bit more sense syntactically also?
(compare semaphores[forks] with semaphores.forks)

philo_bonus/helpers.c
philo_bonus/hunger_watcher.c
philo_bonus/main.c
philo_bonus/mem_management.c
philo_bonus/philo.c
philo_bonus/philo.h

index 46bddffae3793cf21d391c76e6a43fd21166d133..f8caa764e640f1286462886ca4139cc61922a789 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 09:13:40 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 14:08:05 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:32:49 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -45,9 +45,9 @@ void  report(t_philo *philo, const char *str)
 {
        if (is_alive(philo))
        {
-               sem_wait(philo->semaphores[term]);
+               sem_wait(philo->semaphores.term);
                print_timestamp(philo, str);
-               sem_post(philo->semaphores[term]);
+               sem_post(philo->semaphores.term);
        }
        return ;
 }
index b22b59bfc6ad5d371b37c65c4531b2fe3757e97f..254dfd815b13a0a14e04c1ce32f3d613d935e337 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/24 08:49:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 10:33:27 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:33:10 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -44,7 +44,7 @@ static void   *watch_for_starvation(void *philo_void)
                time = time_till_death(philo);
        }
        philo->alive = 0;
-       sem_post(philo->semaphores[death]);
+       sem_post(philo->semaphores.death);
        sem_post(philo->philo_sem);
        return (NULL);
 }
index 48f6e4228c4fcfb4f3a5d33898ee3daca449b8ef..1605e32edbacaf6670ebbd11c80a5d8e5e953474 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 09:21:34 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:21:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -57,25 +57,25 @@ int main(int argc, char **argv)
 {
        t_settings      settings;
        pid_t           *philo_pids;
-       sem_t           *semaphores[sem_num];
+       t_sems          semaphores;
 
        if (parse_input(&settings, argc, argv))
        {
                return (1);
        }
-       if (init(settings.philo_count, &philo_pids, semaphores))
+       if (init(settings.philo_count, &philo_pids, &semaphores))
        {
-               cleanup(philo_pids, semaphores);
+               cleanup(philo_pids, &semaphores);
                return (2);
        }
        if (seat_philosophers(&settings, philo_pids))
        {
                kill_philosophers(settings.philo_count, philo_pids);
-               cleanup(philo_pids, semaphores);
+               cleanup(philo_pids, &semaphores);
                return (3);
        }
-       sem_post(semaphores[term]);
-       handle_philosophers(semaphores);
-       cleanup(philo_pids, semaphores);
+       sem_post(semaphores.term);
+       handle_philosophers(&semaphores);
+       cleanup(philo_pids, &semaphores);
        return (0);
 }
index a8a3cd8710b938511e0552b522a4087f8d317fe5..1fc290d9c07b3bae492d3c0a535e37abca2ef628 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 09:39:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/12 21:54:03 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:22:19 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <sys/stat.h>
 #include <semaphore.h>
 
-static int     open_semaphores(unsigned int count, sem_t *semaphores[sem_num])
+static int     open_semaphores(unsigned int count, t_sems *semaphores)
 {
-       size_t  i;
-
-       semaphores[forks] = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
+       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,
+       semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
                         S_IRUSR | S_IWUSR, count);
-       semaphores[death] = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
+       semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
                         S_IRUSR | S_IWUSR, count);
-       semaphores[term] = sem_open(TERM_SEM, O_CREAT | O_EXCL,
+       semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL,
                         S_IRUSR | S_IWUSR, 1);
-       i = 0;
-       while (i < sem_num)
-               if (semaphores[i++] == SEM_FAILED)
-                       return (1);
+       if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED
+                       || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED)
+               return (1);
        return (0);
 }
 
-int    init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num])
+int    init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
 {
        size_t  i;
 
@@ -49,21 +46,26 @@ int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num])
        while (i < count)
        {
                (*philo_pids)[i] = 0;
-               sem_wait(semaphores[death]);
-               sem_wait(semaphores[fed]);
+               sem_wait(semaphores->death);
+               sem_wait(semaphores->fed);
                ++i;
        }
-       sem_wait(semaphores[term]);
+       sem_wait(semaphores->term);
        return (0);
 }
 
-void   cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num])
+void   close_semaphores(t_sems *semaphores)
 {
-       size_t  i;
+       sem_close(semaphores->forks);
+       sem_close(semaphores->fed);
+       sem_close(semaphores->death);
+       sem_close(semaphores->term);
+       return ;
+}
 
-       i = 0;
-       while (i < sem_num)
-               sem_close(semaphores[i++]);
+void   cleanup(pid_t *philo_pids, t_sems *semaphores)
+{
+       close_semaphores(semaphores);
        sem_unlink(FORKS_SEM);
        sem_unlink(WELL_FED_SEM);
        sem_unlink(DEATH_SEM);
index 92211f9d26bf4844e18075ebdd983d318c9b927b..c56ea195ef7c72c84fa02a0eea60ebd9e459a7f5 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 14:08:23 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:22:54 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -20,19 +20,19 @@ static char *open_semaphores(t_philo *philo)
 {
        char    *sem_name;
 
-       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);
-       philo->semaphores[term] = sem_open(TERM_SEM, 0);
+       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);
+       philo->semaphores.term = sem_open(TERM_SEM, 0);
        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);
        if (philo->philo_sem == SEM_FAILED)
        {
-               sem_wait(philo->semaphores[term]);
+               sem_wait(philo->semaphores.term);
                printf("An error has occured.\n");
-               sem_wait(philo->semaphores[death]);
+               sem_wait(philo->semaphores.death);
                free(sem_name);
                return (NULL);
        }
@@ -47,9 +47,9 @@ static void   philo_think(t_philo *philo)
 
 static void    philo_eat(t_philo *philo)
 {
-       sem_wait(philo->semaphores[forks]);
+       sem_wait(philo->semaphores.forks);
        report(philo, "has taken a fork");
-       sem_wait(philo->semaphores[forks]);
+       sem_wait(philo->semaphores.forks);
        report(philo, "has taken a fork");
        sem_wait(philo->philo_sem);
        philo->last_eaten = usecs_since_start(philo->settings.start);
@@ -58,10 +58,10 @@ static void philo_eat(t_philo *philo)
        report(philo, "is eating");
        if (philo->settings.should_check_hunger &&
                philo->num_eaten == philo->settings.min_eat_num)
-               sem_post(philo->semaphores[fed]);
+               sem_post(philo->semaphores.fed);
        usleep(philo->settings.time_to_eat);
-       sem_post(philo->semaphores[forks]);
-       sem_post(philo->semaphores[forks]);
+       sem_post(philo->semaphores.forks);
+       sem_post(philo->semaphores.forks);
        return ;
 }
 
@@ -89,7 +89,7 @@ void  be_a_philosopher(t_philo *philo)
                philo_sleep(philo);
        }
        destroy_hunger_watcher(hunger_watcher);
-       close_semaphores(philo);
+       close_semaphores(&philo->semaphores);
        sem_unlink(philo_sem_name);
        free(philo_sem_name);
        exit(0);
index 1916549c7c81f53953c3e1f8d3da1578c99ab497..b7d461c19f86f2240f94cd4b94b33d1726f1b590 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 14:08:05 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/24 15:23:21 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 # define DEATH_SEM "philosophers_death"
 # define TERM_SEM "philosophers_terminal"
 
-typedef enum e_semaphores
-{
-       forks,
-       fed,
-       death,
-       term,
-       sem_num,
-}      t_my_sems;
-
 //     The .start member holds the result of gettimeofday
 //     Every other member that holds time info counts
 //     microseconds from that point (in other structures also)
@@ -47,6 +38,14 @@ typedef struct s_settings
        struct timeval  start;
 }                                      t_settings;
 
+typedef struct s_important_semaphores
+{
+       sem_t   *forks;
+       sem_t   *fed;
+       sem_t   *death;
+       sem_t   *term;
+}                      t_sems;
+
 //     As mutex is not available in bonus, the idea is to have
 //     a semaphore with value 1 to for the access to terminal.
 //     The well_fed_sem(aphore) and death_sem(aphore) will be taken
@@ -61,7 +60,7 @@ typedef struct s_philosopher
        unsigned int    id;
        int                             alive;
        useconds_t              last_eaten;
-       sem_t                   *semaphores[sem_num];
+       t_sems                  semaphores;
        sem_t                   *philo_sem;
        t_settings              settings;
 }                                      t_philo;
@@ -69,8 +68,9 @@ typedef struct s_philosopher
 int                    parse_input(t_settings *settings, int argc, char **argv);
 
 int                    init(unsigned int count, pid_t **philo_pids,
-                               sem_t *semaphores[sem_num]);
-void           cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num]);
+                               t_sems *semaphores);
+void           cleanup(pid_t *philo_pids, t_sems *semaphores);
+void           close_semaphores(t_sems *semaphores);
 
 useconds_t     usecs_since_start(struct timeval start);
 int                    is_alive(t_philo *philo);