почти готово кажется
This commit is contained in:
parent
f503236a75
commit
154f0da5d6
@ -1,6 +1,7 @@
|
|||||||
#ifndef PHILO_HEADER_H
|
#ifndef PHILO_HEADER_H
|
||||||
# define PHILO_HEADER_H
|
# define PHILO_HEADER_H
|
||||||
|
|
||||||
|
# define _SLEEP_STEP 50
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
@ -63,4 +64,6 @@ 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);
|
void cycle(t_philo *philo);
|
||||||
|
void ft_msleep(time_t interval_ms);
|
||||||
|
void my_usleep(long time);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -25,20 +25,18 @@ void take_forks(t_philo *philo)
|
|||||||
t_all *all;
|
t_all *all;
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]);
|
pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]);
|
||||||
messages(philo->all_struct, philo, "has taken a fork]");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]);
|
pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]);
|
||||||
messages(philo->all_struct, philo, "has taken a fork]");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
void eating(t_philo *philo)
|
void eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&philo->eat_mutex);
|
pthread_mutex_lock(&philo->eat_mutex);
|
||||||
philo->is_eat = 1;
|
|
||||||
philo->last_eat = ft_get_time();
|
philo->last_eat = ft_get_time();
|
||||||
philo->limit = philo->last_eat + philo->all_struct->time_to_die;
|
// philo->limit = philo->last_eat + philo->all_struct->time_to_die;
|
||||||
messages(philo->all_struct, philo, "eating");
|
messages(philo->all_struct, philo, "eating");
|
||||||
usleep(philo->all_struct->time_to_eat * 1000);
|
my_usleep(philo->all_struct->time_to_eat);
|
||||||
philo->count_eat++;
|
philo->count_eat++;
|
||||||
philo->is_eat = 0;
|
|
||||||
pthread_mutex_unlock(&philo->eat_mutex);
|
pthread_mutex_unlock(&philo->eat_mutex);
|
||||||
}
|
}
|
||||||
void sleeping(t_philo *philo)
|
void sleeping(t_philo *philo)
|
||||||
@ -48,16 +46,35 @@ void sleeping(t_philo *philo)
|
|||||||
messages(philo->all_struct, philo, "sleeping");
|
messages(philo->all_struct, philo, "sleeping");
|
||||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
|
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
|
||||||
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
|
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
|
||||||
usleep(philo->all_struct->time_to_sleep * 1000);
|
my_usleep(philo->all_struct->time_to_sleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ft_msleep(time_t interval_ms)
|
||||||
|
{
|
||||||
|
time_t end_ms = ft_get_time() + interval_ms;
|
||||||
|
time_t now_ms = ft_get_time();
|
||||||
|
|
||||||
|
while (now_ms < end_ms)
|
||||||
|
{
|
||||||
|
usleep(_SLEEP_STEP);
|
||||||
|
now_ms = ft_get_time();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_usleep(time_t time)
|
||||||
|
{
|
||||||
|
time_t t;
|
||||||
|
t = ft_get_time();
|
||||||
|
while (ft_get_time() - t < time)
|
||||||
|
usleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
void cycle(t_philo *philo)
|
void cycle(t_philo *philo)
|
||||||
{
|
{
|
||||||
take_forks(philo);
|
take_forks(philo);
|
||||||
eating(philo);
|
eating(philo);
|
||||||
sleeping(philo);
|
sleeping(philo);
|
||||||
messages(philo->all_struct, philo, "thinking");
|
// messages(philo->all_struct, philo, "thinking");19835 20240
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -65,6 +82,7 @@ int run_observer(t_all *all)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
time_t time;
|
time_t time;
|
||||||
|
time_t temp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
time = ft_get_time();
|
time = ft_get_time();
|
||||||
@ -73,13 +91,18 @@ int run_observer(t_all *all)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while(i <= all->number_of_philosophers)
|
while(i <= all->number_of_philosophers)
|
||||||
{
|
{
|
||||||
if (!all->data_philo->philors[i].is_eat && ft_get_time() > all->data_philo->philors[i].limit)
|
|
||||||
|
|
||||||
|
pthread_mutex_lock(&all->data_philo->philors[i].eat_mutex);
|
||||||
|
time = ft_get_time();
|
||||||
|
temp = time - all->start_time - all->data_philo->philors[i].last_eat;
|
||||||
|
if (temp > all->time_to_die)
|
||||||
{
|
{
|
||||||
// TODO пофиксить смерть
|
// TODO пофиксить смерть
|
||||||
|
|
||||||
printf("симуляция окончена фил %d умер\n", i + 1);
|
printf("симуляция окончена фил %d умер\n", i + 1);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&all->data_philo->philors[i].eat_mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
void messages(t_all *all, t_philo *philo, char *str)
|
void messages(t_all *all, t_philo *philo, char *str)
|
||||||
{
|
{
|
||||||
|
time_t timestemp = ft_get_time() - all->start_time;
|
||||||
|
|
||||||
pthread_mutex_lock(&all->write);
|
pthread_mutex_lock(&all->write);
|
||||||
printf("%d %s\n", philo->nbr_philo + 1, str);
|
printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str);
|
||||||
pthread_mutex_unlock(&all->write);
|
pthread_mutex_unlock(&all->write);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user