let them think

This commit is contained in:
whaffman 2025-01-27 14:14:43 +01:00
parent eb85c4c385
commit dd686a4f85
16 changed files with 33 additions and 132 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:34 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:50 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -81,10 +81,14 @@ int join_threads(t_rules *rules);
void print_rules(t_rules *rules);
int check_death(t_philo *philo);
void philo_die(t_philo *philo);
int philo_died_while_sleeping(t_philo *philo, int time);
int philo_eat(t_philo *philo);
void *monitor_routine(void *arg);
int check_philos(t_rules *rules);
int ft_min(int a, int b);
void set_finished(t_rules *rules);
int get_finished(t_rules *rules);
void synchronize_philos(t_philo *philo);
int philo_sleep(t_philo *philo, int time);
#endif

View File

@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* check_alive.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:43 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h"
int check_death(t_philo *philo)
{
return (get_time() - philo->last_meal >= philo->rules->time_to_die);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:44 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:51 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -21,9 +21,6 @@ int create_mutexes(t_rules *rules)
return (FAILURE);
if (pthread_mutex_init(rules->print_lock, NULL))
return (FAILURE);
rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t));
if (!rules->forks)
return (FAILURE);
while (i < rules->n_philos)
{
if (pthread_mutex_init(&rules->forks[i], NULL))

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:36 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:52 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:37 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:53 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:38 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:53 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:54 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -14,6 +14,14 @@
void free_philos(t_rules *rules)
{
int i;
i = 0;
while (i < rules->n_philos)
{
free(rules->philos[i].pid);
i++;
}
free(rules->monitor);
free(rules->philos);
free(rules->forks);
free(rules->print_lock);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:47 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:57 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:58 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:13:59 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,102 +6,12 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:26:49 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:41 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:14:02 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h"
void set_finished(t_rules *rules)
{
pthread_mutex_lock(rules->finished_lock);
rules->finished = 1;
pthread_mutex_unlock(rules->finished_lock);
}
int get_finished(t_rules *rules)
{
int finished;
pthread_mutex_lock(rules->finished_lock);
finished = rules->finished;
pthread_mutex_unlock(rules->finished_lock);
return (finished);
}
void set_death(t_philo *philo)
{
pthread_mutex_lock(philo->death_lock);
philo->death = 1;
pthread_mutex_unlock(philo->death_lock);
}
int get_death(t_philo *philo)
{
int death;
pthread_mutex_lock(philo->death_lock);
death = philo->death;
pthread_mutex_unlock(philo->death_lock);
return (death);
}
void philo_die(t_philo *philo)
{
print_status(philo, "died");
philo->death = 1;
set_finished(philo->rules);
pthread_mutex_unlock(philo->l_fork);
pthread_mutex_unlock(philo->r_fork);
}
int philo_sleep(t_philo *philo, int time)
{
const int start = get_time();
while (get_time() - start < time)
{
if (philo->death)
return (FAILURE);
usleep(50);
}
return (SUCCESS);
}
int philo_eat(t_philo *philo)
{
if (philo->id % 2 == 1)
{
pthread_mutex_lock(philo->r_fork);
print_status(philo, "has taken a fork");
}
pthread_mutex_lock(philo->l_fork);
print_status(philo, "has taken a fork");
if (philo->id % 2 != 1)
{
pthread_mutex_lock(philo->r_fork);
print_status(philo, "has taken a fork");
}
if (philo->death)
return (FAILURE);
print_status(philo, "is eating");
philo->last_meal = get_time();
philo->n_eat++;
return (SUCCESS);
}
void synchronize_philos(t_philo *philo)
{
pthread_mutex_lock(philo->rules->finished_lock);
pthread_mutex_unlock(philo->rules->finished_lock);
philo->last_meal = get_time();
if (philo->id % 2 == 1)
{
print_status(philo, "is thinking");
usleep(philo->rules->time_to_eat * 10);
}
}
void *philo_routine(void *arg)
{
t_philo *philo;
@ -110,20 +20,20 @@ void *philo_routine(void *arg)
synchronize_philos(philo);
while (!philo->death && !get_finished(philo->rules))
{
if (philo->death)
break ;
if (!philo_eat(philo))
break ;
if (!philo_sleep(philo, philo->rules->time_to_eat))
break ;
pthread_mutex_unlock(philo->l_fork);
pthread_mutex_unlock(philo->r_fork);
if (!philo->death)
if (!philo->death && !get_finished(philo->rules))
print_status(philo, "is sleeping");
if (!philo_sleep(philo, philo->rules->time_to_sleep))
break ;
if(!philo->death)
if(!philo->death && !get_finished(philo->rules))
print_status(philo, "is thinking");
if (!philo_sleep(philo, (philo->rules->time_to_die - philo->rules->time_to_eat - philo->rules->time_to_sleep) / 3))
break ;
}
return (NULL);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:08 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:14:05 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:09 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:14:05 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:43 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:14:06 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/20 14:27:44 by whaffman ### ### ### ### / \ */
/* Updated: 2025/01/27 14:14:07 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */