Implement be_a_philosopher and its helpers
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 10 May 2024 10:13:10 +0000 (12:13 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 10 May 2024 10:13:10 +0000 (12:13 +0200)
Also implement kill_philosophers and other minor changes.

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

index 35f974a05f09d1c8b1b62470a97055aae3e5796c..1c250bdd57ae344c3e50b303054524ac6ed26a55 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 09:13:40 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 11:59:36 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/10 12:11:52 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <sys/time.h>
 #include <stdio.h>
 
-int    end(t_settings *set)
-{
-       int     res;
-
-       mutex_lock(&set->end_lock);
-       res = set->end;
-       mutex_unlock(&set->end_lock);
-       return (res);
-}
-
 useconds_t     usecs_since_start(struct timeval start)
 {
        struct timeval  time;
@@ -35,19 +25,10 @@ useconds_t  usecs_since_start(struct timeval start)
 
 void   print_timestamp(t_philo *philo, const char *str)
 {
+       sem_wait(philo->semaphores[term]);
        mutex_lock(&philo->settings->terminal_lock);
-       printf("%u %lu %s\n", usecs_since_start(philo->settings->start)
+       printf("%u %lu %s\n", usecs_since_start(philo->settings.start)
                / 1000, philo->id, str);
-       mutex_unlock(&philo->settings->terminal_lock);
+       sem_post(philo->semaphores[term]);
        return ;
 }
-
-int    report(t_philo *philo, const char *str)
-{
-       if (!end(philo->settings))
-       {
-               print_timestamp(philo, str);
-               return (0);
-       }
-       return (1);
-}
index e2667f340cc7d5fac922087a499dc70f40965f60..1b72eaaaec0c51d8a54de0e2c2e10e672503ae83 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/10 11:42:24 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/10 11:48:05 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -39,32 +39,13 @@ static int  seat_philosophers(t_settings *settings, pid_t *philo_pids)
        return (0);
 }
 
-static enum e_end      watch_philosophers(t_diner *diner)
+void   kill_philosophers(unsigned int count, pid_t *philo_pids)
 {
-       size_t  i;
-
-       while (1)
+       while (count > 0)
        {
-               i = 0;
-               while (i < diner->setting.philo_count)
-               {
-                       if (is_dead(diner->philos + i))
-                       {
-                               mutex_lock(&diner->setting.end_lock);
-                               diner->setting.end = 1;
-                               mutex_unlock(&diner->setting.end_lock);
-                               print_timestamp(diner->philos + i, "died");
-                               return (death);
-                       }
-                       ++i;
-               }
-               if (all_full(diner))
-               {
-                       mutex_lock(&diner->setting.end_lock);
-                       diner->setting.end = 1;
-                       mutex_unlock(&diner->setting.end_lock);
-                       return (well_fed);
-               }
+               --count;
+               if (philo_pids[count] != 0)
+                       kill(philo_pids[count], SIGTERM);
        }
 }
 
index 6dfa0fb03458a8f0399e72ca77553e82c6d9fce6..7a7877eb8920639536cb3be8f2fd8675a6b5fa24 100644 (file)
@@ -6,56 +6,64 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 10:25:46 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/10 12:12:26 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "philo.h"
 #include <unistd.h>
 
+static void    open_semaphores(sem_t *semaphores[sem_num])
+{
+       semaphores[forks] = sem_open(FORKS_SEM, 0);
+       semaphores[fed] = sem_open(WELL_FED_SEM, 0);
+       semaphores[death] = sem_open(DEATH_SEM, 0);
+       semaphores[term] = sem_open(TERM_SEM, 0);
+}
+
 static void    philo_think(t_philo *philo)
 {
-       report(philo, "is thinking");
+       print_timestamp(philo, "is thinking");
        return ;
 }
 
 static void    philo_eat(t_philo *philo)
 {
-       mutex_lock(philo->forks[0]);
-       if (report(philo, "has taken a fork"))
-       {
-               mutex_unlock(philo->forks[0]);
-               return ;
-       }
-       mutex_lock(philo->forks[1]);
-       report(philo, "has taken a fork");
-       mutex_lock(&philo->philo_lock);
-       philo->last_eaten = usecs_since_start(philo->settings->start);
+       useconds_t      time_now;
+
+       sem_wait(philo->semaphores[forks]);
+       print_timestamp(philo, "has taken a fork");
+       sem_wait(philo->semaphores[forks]);
+       print_timestamp(philo, "has taken a fork");
+       time_now = usecs_since_start(philo->settings.start);
+       if (time_now - philo->last_eaten > philo->settings.time_to_die)
+               die(philo);
+       philo->last_eaten = time_now;
        ++philo->num_eaten;
-       mutex_unlock(&philo->philo_lock);
-       if (!report(philo, "is eating"))
-               usleep(philo->settings->time_to_eat);
-       mutex_unlock(philo->forks[0]);
-       mutex_unlock(philo->forks[1]);
+       print_timestamp(philo, "is eating");
+       if (philo->settings.should_check_hunger &&
+               philo->num_eaten == philo->settings.min_eat_num)
+               sem_post(philo->semaphores[fed]);
+       usleep(philo->settings->time_to_eat);
+       sem_post(philo->semaphores[forks]);
+       sem_post(philo->semaphores[forks]);
        return ;
 }
 
 static void    philo_sleep(t_philo *philo)
 {
-       if (!report(philo, "is sleeping"))
-               usleep(philo->settings->time_to_sleep);
+       print_timestamp(philo, "is sleeping");
+       usleep(philo->settings->time_to_sleep);
        return ;
 }
 
-void   *be_a_philosopher(void *void_philo)
+void   be_a_philosopher(t_philo *philo)
 {
-       t_philo *const  philo = void_philo;
-
-       while (!end(philo->settings))
+       open_semaphores(philo->semaphores);
+       while (1)
        {
                philo_think(philo);
                philo_eat(philo);
                philo_sleep(philo);
        }
-       return (NULL);
 }
index 548d65e6f5266b054d3c59d07b06fe5e1fcbabcf..c1df3c5ffcedd655cc58acfd0fcfb097a767ffeb 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/10 10:59:31 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/10 12:12:26 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -59,10 +59,7 @@ typedef struct s_philosopher
        unsigned int    num_eaten;
        unsigned int    id;
        useconds_t              last_eaten;
-       sem_t                   *forks;
-       sem_t                   *well_fed_sem;
-       sem_t                   *death_sem;
-       sem_t                   *terminal_sem;
+       sem_t                   *semaphores[sem_num];
        t_settings              settings;
 }                                      t_philo;