philo_one
This commit is contained in:
parent
154f0da5d6
commit
797bb0d3bc
@ -9,6 +9,16 @@
|
|||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
# include <string.h>
|
# include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define ANSI_COLOR_RED "\x1b[31m"
|
||||||
|
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||||
|
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||||
|
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||||
|
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||||
|
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||||
|
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||||
|
|
||||||
typedef struct s_philo_data t_philo_data;
|
typedef struct s_philo_data t_philo_data;
|
||||||
|
|
||||||
typedef struct s_all
|
typedef struct s_all
|
||||||
|
|||||||
@ -10,8 +10,8 @@ void *run_trhead(t_philo *philo)
|
|||||||
}
|
}
|
||||||
if (philo->can_run_simulation)
|
if (philo->can_run_simulation)
|
||||||
{
|
{
|
||||||
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;
|
||||||
while(philo->can_run_simulation)
|
while(philo->can_run_simulation)
|
||||||
{
|
{
|
||||||
cycle(philo);
|
cycle(philo);
|
||||||
@ -31,13 +31,12 @@ void take_forks(t_philo *philo)
|
|||||||
}
|
}
|
||||||
void eating(t_philo *philo)
|
void eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&philo->eat_mutex);
|
// 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");
|
messages(philo->all_struct, philo, "eating");
|
||||||
my_usleep(philo->all_struct->time_to_eat);
|
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
||||||
philo->count_eat++;
|
philo->count_eat++;
|
||||||
pthread_mutex_unlock(&philo->eat_mutex);
|
my_usleep(philo->all_struct->time_to_eat);
|
||||||
|
// pthread_mutex_unlock(&philo->eat_mutex);
|
||||||
}
|
}
|
||||||
void sleeping(t_philo *philo)
|
void sleeping(t_philo *philo)
|
||||||
{
|
{
|
||||||
@ -49,60 +48,40 @@ void sleeping(t_philo *philo)
|
|||||||
my_usleep(philo->all_struct->time_to_sleep);
|
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");19835 20240
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int run_observer(t_all *all)
|
int run_observer(t_all *all)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
time_t time;
|
|
||||||
time_t temp;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
time = ft_get_time();
|
time_t time;
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
|
|
||||||
while(i <= all->number_of_philosophers)
|
while(i <= all->number_of_philosophers)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// pthread_mutex_lock(&all->data_philo->philors[i].eat_mutex);
|
||||||
pthread_mutex_lock(&all->data_philo->philors[i].eat_mutex);
|
time = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat;
|
||||||
time = ft_get_time();
|
// printf("%ld test\n", time);
|
||||||
temp = time - all->start_time - all->data_philo->philors[i].last_eat;
|
if (time > all->time_to_die) // TODO пофиксить смерть
|
||||||
if (temp > all->time_to_die)
|
|
||||||
{
|
{
|
||||||
// TODO пофиксить смерть
|
printf("%ld---------------симуляция окончена фил %d умер ---------------\n", ft_get_time()- all->start_time, i + 1);
|
||||||
printf("симуляция окончена фил %d умер\n", i + 1);
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&all->data_philo->philors[i].eat_mutex);
|
i++;
|
||||||
|
// pthread_mutex_unlock(&all->data_philo->philors[i].eat_mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@ -96,3 +96,32 @@ time_t ft_get_time(void)
|
|||||||
return (return_value);
|
return (return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
long get_time(void)
|
||||||
|
{
|
||||||
|
struct timeval time;
|
||||||
|
gettimeofday(&time, NULL);
|
||||||
|
return ((long)(time.tv_sec * 1000 + time.tv_usec / 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
void my_usleep(long time)
|
||||||
|
{
|
||||||
|
long t;
|
||||||
|
t = get_time();
|
||||||
|
while (get_time() - t < time)
|
||||||
|
usleep(1);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user