Compare commits
2 Commits
cb2e29d18b
...
caa4491fbb
Author | SHA1 | Date | |
---|---|---|---|
|
caa4491fbb | ||
|
19c5a53ba9 |
12
main.c
12
main.c
@ -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
29
philo.c
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user