add all link in philo

This commit is contained in:
Denis 2021-05-14 13:46:13 +03:00
parent bbecbe37b8
commit 36c8c87c77
5 changed files with 24 additions and 19 deletions

View File

@ -8,6 +8,20 @@
# include <stdio.h> # include <stdio.h>
# include <string.h> # include <string.h>
typedef struct s_philo_data t_philo_data;
typedef struct s_all
{
int number_of_philosophers;
time_t time_to_die;
time_t time_to_eat;
time_t time_to_sleep;
int number_of_times_each_philosopher_must_eat;
time_t start_time;
t_philo_data *data_philo;
pthread_mutex_t write;
} t_all;
typedef struct s_philo typedef struct s_philo
{ {
@ -20,7 +34,7 @@ typedef struct s_philo
time_t time_to_die; time_t time_to_die;
int died; int died;
int nbr_philo; int nbr_philo;
t_all *all_struct;
pthread_mutex_t *eat_mutex; pthread_mutex_t *eat_mutex;
} t_philo; } t_philo;
@ -30,18 +44,6 @@ typedef struct s_philo_data
t_philo *philors; t_philo *philors;
} t_philo_data; } t_philo_data;
typedef struct s_all
{
int number_of_philosophers;
time_t time_to_die;
time_t time_to_eat;
time_t time_to_sleep;
int number_of_times_each_philosopher_must_eat;
time_t start_time;
t_philo_data *data_philo;
pthread_mutex_t write;
} t_all;
void *ft_calloc(size_t num, size_t size); void *ft_calloc(size_t num, size_t size);
t_all *structalloc_and_bzero(void); t_all *structalloc_and_bzero(void);
int parse_and_check_values(char **argv, int quantity); int parse_and_check_values(char **argv, int quantity);
@ -57,4 +59,5 @@ int prepare_resources(t_all *all);
int check_all_run(t_all *all); 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);
#endif #endif

Binary file not shown.

View File

@ -7,9 +7,11 @@ void *run_trhead(t_philo *philo)
while(1) while(1)
{ {
if (philo->can_run_simulation) if (philo->can_run_simulation)
{ break;
printf("я могу что то делать ура я %d\n", philo->nbr_philo);
} }
while(!philo->died)
{
messages(philo->all_struct, philo, "я могу что то делать ура");
} }
return(NULL); return(NULL);
} }

View File

@ -1,9 +1,8 @@
#include "../includes/philo_header.h" #include "../includes/philo_header.h"
void messages(t_all *all, time_t *time, int phil, char *str) void messages(t_all *all, t_philo *philo, char *str)
{ {
pthread_mutex_lock(&all->write); pthread_mutex_lock(&all->write);
phil++; printf("%d %s\n", philo->nbr_philo + 1, str);
printf("%lu %d %s\n", (unsigned long)time, phil, str);
pthread_mutex_unlock(&all->write); pthread_mutex_unlock(&all->write);
} }

View File

@ -54,6 +54,7 @@ void get_fork_for_philo(t_all *all)
all->data_philo->philors[i].nbr_philo = i; all->data_philo->philors[i].nbr_philo = i;
all->data_philo->philors[i].worked = 0; all->data_philo->philors[i].worked = 0;
all->data_philo->philors[i].can_run_simulation = 0; all->data_philo->philors[i].can_run_simulation = 0;
all->data_philo->philors[i].all_struct = all;
i++; i++;
} }
} }