aboutsummaryrefslogtreecommitdiff
path: root/philo/src/simulation.c
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-26 12:44:13 +0100
committerDominik Kaiser2025-01-26 12:44:13 +0100
commit0dad3463b22e541153dcfded178eb0f3fe428cbd (patch)
tree88e39f083a355727fc8cf5772e7948fb7990bd47 /philo/src/simulation.c
parent0813f4153af4723d3fddffea6926243f3a94fa57 (diff)
downloadPhilosophers-0dad3463b22e541153dcfded178eb0f3fe428cbd.tar.gz
Philosophers-0dad3463b22e541153dcfded178eb0f3fe428cbd.zip
Update simulation and fix norme errors
Diffstat (limited to 'philo/src/simulation.c')
-rw-r--r--philo/src/simulation.c196
1 files changed, 70 insertions, 126 deletions
diff --git a/philo/src/simulation.c b/philo/src/simulation.c
index fa8e886..fd9f40b 100644
--- a/philo/src/simulation.c
+++ b/philo/src/simulation.c
@@ -6,148 +6,92 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/17 14:38:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/18 17:09:34 by dkaiser ### ########.fr */
+/* Updated: 2025/01/26 12:43:53 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
-int philo_die(t_philo *philo)
+int philo_die(t_philo *philo)
{
- if (ft_cur_time_in_ms() > philo->last_time_eaten + philo->data->time_to_die)
- {
- philo->is_alive = 0;
- ft_log(philo->id, "has died");
- philo->data->simulation_running = 0;
- return (1);
- }
- return (0);
+ if (ft_cur_time_in_ms() > philo->last_time_eaten + philo->data->time_to_die)
+ {
+ philo->is_alive = 0;
+ ft_log(philo->id, "has died");
+ philo->data->simulation_running = 0;
+ return (1);
+ }
+ return (0);
}
-void philo_eat(t_philo *philo)
+void philo_sleep(t_philo *philo)
{
- t_fork *left_fork;
- t_fork *right_fork;
- int started_eating;
- int tte;
+ int started_sleeping;
+ int tts;
- left_fork = &philo->data->forks[philo->id - 1];
- right_fork = &philo->data->forks[philo->id % philo->data->nbr_of_philos];
- while (!left_fork->available || !right_fork->available)
- {
- if (!philo->data->simulation_running)
- return;
- if (philo_die(philo))
- return ;
- usleep(1000);
- }
- pthread_mutex_lock(&left_fork->mutex);
- pthread_mutex_lock(&right_fork->mutex);
- if (philo->data->simulation_running)
- {
-
- left_fork->available = 0;
- ft_log(philo->id, "has taken a fork");
- right_fork->available = 0;
- ft_log(philo->id, "has taken a fork");
- started_eating = ft_cur_time_in_ms();
- tte = philo->data->time_to_eat;
- philo->last_time_eaten = started_eating;
- philo->times_must_eat -= 1;
- ft_log(philo->id, "is eating");
- while (ft_cur_time_in_ms() < started_eating + tte)
- {
- if (philo_die(philo))
- return;
- usleep(1000);
- }
- left_fork->available = 1;
- right_fork->available = 1;
- }
- pthread_mutex_unlock(&left_fork->mutex);
- pthread_mutex_unlock(&right_fork->mutex);
-}
-
-void philo_sleep(t_philo *philo)
-{
- int started_sleeping;
- int tts;
-
- started_sleeping = ft_cur_time_in_ms();
- tts = philo->data->time_to_sleep;
- ft_log(philo->id, "is sleeping");
- while (ft_cur_time_in_ms() < started_sleeping + tts)
- {
- if (philo_die(philo))
- return;
- usleep(1000);
- }
-}
-
-void philo_think(t_philo *philo)
-{
- ft_log(philo->id, "is thinking");
+ started_sleeping = ft_cur_time_in_ms();
+ tts = philo->data->time_to_sleep;
+ ft_log(philo->id, "is sleeping");
+ while (ft_cur_time_in_ms() < started_sleeping + tts)
+ {
+ if (philo_die(philo))
+ return ;
+ usleep(1000);
+ }
}
-int *process_philo(void *arg)
+void process_philo(void *arg)
{
- t_philo *philo;
- int *result;
+ t_philo *philo;
- result = malloc(sizeof(int));
- if (result == NULL)
- return (NULL);
- philo = (t_philo *)arg;
- *result = EXIT_SUCCESS;
- while (philo->data->simulation_running)
- {
- philo_eat(philo);
- if (philo->times_must_eat == 0)
- {
- pthread_mutex_lock(&philo->data->pme_mutex);
- philo->data->philos_must_eat -= 1;
- pthread_mutex_unlock(&philo->data->pme_mutex);
- }
- if (!philo->data->simulation_running)
- break;
- philo_sleep(philo);
- if (!philo->data->simulation_running)
- break;
- philo_think(philo);
- }
- return (result);
+ philo = (t_philo *)arg;
+ while (philo->data->simulation_running)
+ {
+ philo_eat(philo);
+ if (philo->times_must_eat == 0)
+ {
+ pthread_mutex_lock(&philo->data->pme_mutex);
+ philo->data->philos_must_eat -= 1;
+ pthread_mutex_unlock(&philo->data->pme_mutex);
+ }
+ if (!philo->data->simulation_running)
+ break ;
+ if (philo->data->nbr_of_philos < 2)
+ {
+ if (philo_die(philo))
+ break ;
+ continue ;
+ }
+ philo_sleep(philo);
+ if (!philo->data->simulation_running)
+ break ;
+ ft_log(philo->id, "is thinking");
+ }
}
-int run_simulation(int nbr_of_philos, t_philo *philos, t_phdata *data)
+int run_simulation(int nbr_of_philos, t_philo *philos, t_phdata *data)
{
- int i;
- int result;
- void *retval;
+ int i;
+ int result;
- i = 0;
- result = EXIT_SUCCESS;
- while (i < nbr_of_philos)
- {
- result = pthread_create(&(philos[i].thread), NULL, (void*) process_philo, &philos[i]);
- if (result != 0)
- break;
- i++;
- }
- while (data->simulation_running)
- {
- if (data->philos_must_eat <= 0)
- data->simulation_running = 0;
- }
- while (i--)
- {
- pthread_join(philos[i].thread, &retval);
- if (retval == NULL)
- result = ft_err(ERR_MALLOC);
- else if (*(int *)retval != EXIT_SUCCESS)
- {
- result = *(int *)retval;
- free(retval);
- }
- }
- return (result);
+ i = 0;
+ result = EXIT_SUCCESS;
+ while (i < nbr_of_philos)
+ {
+ result = pthread_create(&(philos[i].thread), NULL,
+ (void *)process_philo, &philos[i]);
+ if (result != 0)
+ break ;
+ i++;
+ }
+ while (data->simulation_running)
+ {
+ if (data->philos_must_eat <= 0)
+ data->simulation_running = 0;
+ }
+ while (i--)
+ {
+ pthread_join(philos[i].thread, NULL);
+ }
+ return (result);
}