Compare commits

..

No commits in common. "caa4491fbbb3e38fdbbf7b525e64708953352e42" and "cb2e29d18b22ef284fb9f2a6e49691cc83355c5d" have entirely different histories.

2 changed files with 2 additions and 39 deletions

12
main.c
View File

@ -3,7 +3,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "./philos.h" #include "./philos.h"
#include "./data.h" #include "./data.h"
#include "./parsing.h" #include "./parsing.h"
@ -71,19 +70,8 @@ void *check_routine(t_data *data)
stop(data); stop(data);
return (NULL); 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++; i++;
} }
usleep(100);
} }
} }

29
philo.c
View File

@ -38,17 +38,7 @@ void eat(t_philo *philo, t_data *data)
print_take_a_fork(philo); print_take_a_fork(philo);
print_take_a_fork(philo); print_take_a_fork(philo);
print_eating(philo); print_eating(philo);
pthread_mutex_lock(&philo->last_eat_mutex); usleep(data->eat_time * 1000);
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); pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++; philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex); pthread_mutex_unlock(&philo->nb_meal_mutex);
@ -91,22 +81,7 @@ bool philo_eat(t_philo *philo, t_data *data)
void philo_sleep(t_data *data, t_philo *philo) void philo_sleep(t_data *data, t_philo *philo)
{ {
print_sleeping(philo); print_sleeping(philo);
pthread_mutex_lock(&philo->last_sleep_mutex); usleep(data->sleep_time * 1000);
// 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);
} }