Compare commits

...

2 Commits

2 changed files with 39 additions and 2 deletions

12
main.c
View File

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "./philos.h"
#include "./data.h"
#include "./parsing.h"
@ -70,8 +71,19 @@ void *check_routine(t_data *data)
stop(data);
return (NULL);
}
pthread_mutex_lock(&philo->last_sleep_mutex);
// printf("time=%zu, last_sleep=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->life_expectency);
ok = !(philo->last_sleep + data->life_expectency > get_time());
pthread_mutex_unlock(&philo->last_sleep_mutex);
if (ok == 1)
{
print_died(philo);
stop(data);
return (NULL);
}
i++;
}
usleep(100);
}
}

29
philo.c
View File

@ -38,7 +38,17 @@ void eat(t_philo *philo, t_data *data)
print_take_a_fork(philo);
print_take_a_fork(philo);
print_eating(philo);
usleep(data->eat_time * 1000);
pthread_mutex_lock(&philo->last_eat_mutex);
if (get_time() - philo->last_eat + data->eat_time > data->life_expectency)
{
pthread_mutex_unlock(&philo->last_eat_mutex);
usleep((data->life_expectency -
(get_time() - philo->last_eat)) * 1000);
return ;
}
else
usleep(data->eat_time * 1000);
pthread_mutex_unlock(&philo->last_eat_mutex);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
@ -81,7 +91,22 @@ bool philo_eat(t_philo *philo, t_data *data)
void philo_sleep(t_data *data, t_philo *philo)
{
print_sleeping(philo);
usleep(data->sleep_time * 1000);
pthread_mutex_lock(&philo->last_sleep_mutex);
// printf("time=%zu, last_sleep=%zu, sleep_time=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->sleep_time, data->life_expectency);
if (get_time() - philo->last_sleep + data->sleep_time > data->life_expectency)
{
pthread_mutex_unlock(&philo->last_sleep_mutex);
// printf("rompiche=%zu\n", (data->life_expectency -
// (get_time() - philo->last_sleep)));
usleep((data->life_expectency -
(get_time() - philo->last_sleep)) * 1000);
return ;
}
else
usleep(data->sleep_time * 1000);
philo->last_sleep = get_time();
pthread_mutex_unlock(&philo->last_sleep_mutex);
}