Get rid of philo_pids array as it is not needed
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 11:19:35 +0000 (13:19 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 11:19:35 +0000 (13:19 +0200)
philo_bonus/main.c
philo_bonus/mem_management.c
philo_bonus/philo.h

index 2bf9e6527e3d4d34010a117ab5f4eeeb295c8eea..f9976ab917ae25c903e5b28293ac7dc932f44a9a 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:54:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:16:05 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <sys/types.h>
 #include <signal.h>
 
-static int     seat_philosophers(t_settings *settings, pid_t *philo_pids, t_sems *semaphores)
+static int     seat_philosophers(t_settings *settings, t_sems *semaphores)
 {
        t_philo philo;
+       pid_t   pid;
 
        philo.num_eaten = 0;
        gettimeofday(&settings->start, NULL);
@@ -31,28 +32,23 @@ static int  seat_philosophers(t_settings *settings, pid_t *philo_pids, t_sems *se
        philo.last_eaten = 0;
        while (philo.id <= settings->philo_count)
        {
-               philo_pids[philo.id - 1] = fork();
-               if (philo_pids[philo.id - 1] == 0)
+               pid = fork();
+               if (pid == 0)
                {
                        close_semaphores(semaphores);
-                       free(philo_pids);
                        be_a_philosopher(&philo);
                }
-               else if (philo_pids[philo.id - 1] < 0)
+               else if (pid < 0)
                        return (1);
                ++philo.id;
        }
        return (0);
 }
 
-void   kill_philosophers(unsigned int count, pid_t *philo_pids)
+void   kill_philosophers(t_sems *semaphores)
 {
-       while (count > 0)
-       {
-               --count;
-               if (philo_pids[count] != 0)
-                       kill(philo_pids[count], SIGTERM);
-       }
+       sem_post(semaphores->death);
+       return ;
 }
 
 void   *check_fed(void *input)
@@ -112,26 +108,25 @@ void      wait_for_end(unsigned int philo_count, t_sems *semaphores)
 int    main(int argc, char **argv)
 {
        t_settings      settings;
-       pid_t           *philo_pids;
        t_sems          semaphores;
 
        if (parse_input(&settings, argc, argv))
        {
                return (1);
        }
-       if (init(settings.philo_count, &philo_pids, &semaphores))
+       if (init(settings.philo_count, &semaphores))
        {
-               cleanup(philo_pids, &semaphores);
+               cleanup(&semaphores);
                return (2);
        }
-       if (seat_philosophers(&settings, philo_pids, &semaphores))
+       if (seat_philosophers(&settings, &semaphores))
        {
-               kill_philosophers(settings.philo_count, philo_pids);
-               cleanup(philo_pids, &semaphores);
+               kill_philosophers(&semaphores);
+               cleanup(&semaphores);
                return (3);
        }
        sem_post(semaphores.term);
        wait_for_end(settings.philo_count, &semaphores);
-       cleanup(philo_pids, &semaphores);
+       cleanup(&semaphores);
        return (0);
 }
index 76fc5402aee35e4a2dfb6499160019f0b3fd0b6b..5f004b1747a25aaa67959ead6d016c47eaa56541 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 09:39:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 13:01:59 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:17:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -41,14 +41,9 @@ static int   open_semaphores(unsigned int count, t_sems *semaphores)
        return (0);
 }
 
-int    init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
+int    init(unsigned int count, t_sems *semaphores)
 {
-       *philo_pids = malloc(count * sizeof(**philo_pids));
-       if (!*philo_pids)
-               return (1);
-       if (open_semaphores(count, semaphores))
-               return (1);
-       return (0);
+       return (open_semaphores(count, semaphores));
 }
 
 void   close_semaphores(t_sems *semaphores)
@@ -68,7 +63,7 @@ void  close_semaphores(t_sems *semaphores)
        return ;
 }
 
-void   cleanup(pid_t *philo_pids, t_sems *semaphores)
+void   cleanup(t_sems *semaphores)
 {
        close_semaphores(semaphores);
        sem_unlink(FORKS_SEM);
@@ -77,6 +72,5 @@ void  cleanup(pid_t *philo_pids, t_sems *semaphores)
        sem_unlink(TERM_SEM);
        sem_unlink(END_SEM);
        sem_unlink(CHECK_END_SEM);
-       free(philo_pids);
        return ;
 }
index 46020191d410b8fd70a14dbb7ecc62c1821772ec..2f6cf26c05e86a766c195228e43547a8001426bf 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 12:18:20 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:15:08 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -79,9 +79,8 @@ typedef struct s_check
 
 int                    parse_input(t_settings *settings, int argc, char **argv);
 
-int                    init(unsigned int count, pid_t **philo_pids,
-                               t_sems *semaphores);
-void           cleanup(pid_t *philo_pids, t_sems *semaphores);
+int                    init(unsigned int count, t_sems *semaphores);
+void           cleanup(t_sems *semaphores);
 void           close_semaphores(t_sems *semaphores);
 
 useconds_t     usecs_since_start(struct timeval start);