add new func

This commit is contained in:
Matthos Denys 2021-05-16 22:43:59 +03:00
parent 255f85c9ea
commit 8285f3b0a1
3 changed files with 8 additions and 3 deletions

View File

@ -52,6 +52,7 @@ typedef struct s_philo
t_all *all_struct; t_all *all_struct;
int is_eat; int is_eat;
sem_t *eat_sem; sem_t *eat_sem;
char *name;
} t_philo; } t_philo;
typedef struct s_philo_data typedef struct s_philo_data

View File

@ -27,7 +27,7 @@ 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]); // TODO передать на на семафор
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");
@ -44,8 +44,8 @@ void sleeping(t_philo *philo)
t_all *all; t_all *all;
all = philo->all_struct; all = philo->all_struct;
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]);
ft_msleep(philo->all_struct->time_to_sleep); ft_msleep(philo->all_struct->time_to_sleep);
} }

View File

@ -40,6 +40,10 @@ void get_fork_for_philo(t_all *all)
while(i <= all->number_of_philosophers) while(i <= all->number_of_philosophers)
{ {
all->data_philo->philors[i].name = sem_set_name("philo", i);
all->data_philo->philors[i].eat_sem = reopen_sem(all->data_philo->philors[i].name, 1);
if (all->data_philo->philors[i].eat_sem == SEM_FAILED)
exit(0); // TODO fix this
all->data_philo->philors[i].died = 0; all->data_philo->philors[i].died = 0;
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;