/* 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;
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);
-}
/* 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 */
/* */
/* ************************************************************************** */
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);
}
}
/* 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);
}