add logging

This commit is contained in:
Denis 2021-05-14 18:01:41 +03:00
parent 36c8c87c77
commit b1db9fe912
3 changed files with 44 additions and 15 deletions

View File

@ -34,8 +34,10 @@ typedef struct s_philo
time_t time_to_die; time_t time_to_die;
int died; int died;
int nbr_philo; int nbr_philo;
time_t last_eat;
time_t limit;
t_all *all_struct; t_all *all_struct;
pthread_mutex_t *eat_mutex; pthread_mutex_t eat_mutex;
} t_philo; } t_philo;
typedef struct s_philo_data typedef struct s_philo_data
@ -60,4 +62,5 @@ int check_all_run(t_all *all);
void run_simulation(t_all *all); void run_simulation(t_all *all);
void run_all_phils(t_all *all); void run_all_phils(t_all *all);
void messages(t_all *all, t_philo *philo, char *str); void messages(t_all *all, t_philo *philo, char *str);
void cycle(t_philo *philo);
#endif #endif

View File

@ -3,29 +3,57 @@
void *run_trhead(t_philo *philo) void *run_trhead(t_philo *philo)
{ {
philo->worked = 1; philo->worked = 1;
while(1) while(1)
{ {
if (philo->can_run_simulation) if (philo->can_run_simulation)
break; break;
} }
while(!philo->died) if (philo->can_run_simulation)
{ {
messages(philo->all_struct, philo, "я могу что то делать ура"); while(philo->can_run_simulation)
cycle(philo);
} }
return(NULL); return(NULL);
} }
void take_forks(t_philo *philo)
void philo_think(t_all *all, int i)
{ {
time_t time; t_all *all;
all = philo->all_struct;
pthread_mutex_lock(&all->data_philo->mutex[all->data_philo->philors[i].less_fork]); pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]);
pthread_mutex_lock(&all->data_philo->mutex[all->data_philo->philors[i].more_fork]); messages(philo->all_struct, philo, "has taken a fork]");
time = ft_get_time(); pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]);
time = ft_get_time(); messages(philo->all_struct, philo, "has taken a fork]");
} }
void eating(t_philo *philo)
{
pthread_mutex_lock(&philo->eat_mutex);
philo->last_eat = ft_get_time();
philo->limit = philo->last_eat + philo->all_struct->time_to_die;
messages(philo->all_struct, philo, "eating");
usleep(philo->all_struct->time_to_eat * 1000);
philo->count_eat++;
pthread_mutex_unlock(&philo->eat_mutex);
}
void sleeping(t_philo *philo)
{
t_all *all;
all = philo->all_struct;
messages(philo->all_struct, philo, "sleeping");
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
usleep(philo->all_struct->time_to_sleep * 1000);
}
void cycle(t_philo *philo)
{
take_forks(philo);
eating(philo);
sleeping(philo);
messages(philo->all_struct, philo, "thinking");
}
int run_observer(t_all *all) int run_observer(t_all *all)
{ {
@ -44,7 +72,6 @@ int run_observer(t_all *all)
if (all->data_philo->philors[i].died == 0) if (all->data_philo->philors[i].died == 0)
{ {
time = ft_get_time(); time = ft_get_time();
// printf("%ld\n", time - all->start_time);
i++; i++;
} }
else else
@ -68,10 +95,10 @@ int runner(t_all *all, int optional)
if (optional) if (optional)
{ {
prepare_resources(all); prepare_resources(all);
all->start_time = ft_get_time();
run_all_phils(all); run_all_phils(all);
if (check_all_run(all)) if (check_all_run(all))
{ {
all->start_time = ft_get_time();
run_simulation(all); run_simulation(all);
} }
if (!run_observer(all)) if (!run_observer(all))

View File

@ -66,7 +66,6 @@ t_all *structalloc_and_bzero(void)
all->time_to_eat = -1; all->time_to_eat = -1;
all->time_to_sleep = -1; all->time_to_sleep = -1;
all->number_of_times_each_philosopher_must_eat = -1; all->number_of_times_each_philosopher_must_eat = -1;
all->start_time = ft_get_time();
return(all); return(all);
} }