philo_one fixed

This commit is contained in:
Matthos Denys 2021-05-17 16:53:23 +03:00
parent 74404afb18
commit e1583f9a17
18 changed files with 31 additions and 13 deletions

2
.vscode/launch.json vendored
View File

@ -8,7 +8,7 @@
"type": "lldb", "type": "lldb",
"request": "launch", "request": "launch",
"name": "Debug", "name": "Debug",
"program": "${workspaceFolder}/philo_two/philo_two", "program": "${workspaceFolder}/philo_one/philo_one",
"args": ["5", "800", "200", "200"], "args": ["5", "800", "200", "200"],
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}"
} }

View File

@ -27,6 +27,9 @@ void take_forks(t_philo *philo)
{ {
t_all *all; t_all *all;
all = philo->all_struct; all = philo->all_struct;
//printf("less %d %d\n", philo->less_fork, philo->nbr_philo);
//printf("big %d %d\n", philo->more_fork, philo->nbr_philo);
// printf("mutex: %p\n", all->data_philo->mutex);
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]);
@ -34,9 +37,13 @@ void take_forks(t_philo *philo)
} }
void eating(t_philo *philo) void eating(t_philo *philo)
{ {
t_all *all;
all = philo->all_struct;
messages(philo->all_struct, philo, "eating"); messages(philo->all_struct, philo, "eating");
philo->last_eat = ft_get_time() - philo->all_struct->start_time; philo->last_eat = ft_get_time() - philo->all_struct->start_time;
ft_msleep(philo->all_struct->time_to_eat); ft_msleep(philo->all_struct->time_to_eat);
pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
philo->count_eat++; philo->count_eat++;
} }
void sleeping(t_philo *philo) void sleeping(t_philo *philo)
@ -44,8 +51,6 @@ 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->less_fork]);
ft_msleep(philo->all_struct->time_to_sleep); ft_msleep(philo->all_struct->time_to_sleep);
} }

View File

@ -47,8 +47,8 @@ void get_fork_for_philo(t_all *all)
} }
else else
{ {
all->data_philo->philors[i].more_fork = return_big(i, i + 1); all->data_philo->philors[i].more_fork = i + 1;
all->data_philo->philors[i].less_fork = return_less(i, i + 1); all->data_philo->philors[i].less_fork = i;
} }
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;
@ -66,13 +66,13 @@ int prepare_resources(t_all *all)
i = 0; i = 0;
all->data_philo = ft_calloc(sizeof(t_philo_data), 1); all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(sizeof(pthread_mutex_t), all->number_of_philosophers); all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(all->number_of_philosophers + 1, sizeof(pthread_mutex_t));
while (i <= all->number_of_philosophers) while (i <= all->number_of_philosophers)
{ {
pthread_mutex_init(&all->data_philo->mutex[i], NULL); pthread_mutex_init(&all->data_philo->mutex[i], NULL);
i++; i++;
} }
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers); all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers + 1, sizeof(t_philo));
get_fork_for_philo(all); get_fork_for_philo(all);
return (0); return (0);
} }

BIN
philo_two/obj/ft_itoa.o Normal file

Binary file not shown.

BIN
philo_two/obj/ft_strjoin.o Normal file

Binary file not shown.

BIN
philo_two/obj/logic.o Normal file

Binary file not shown.

BIN
philo_two/obj/main.o Normal file

Binary file not shown.

BIN
philo_two/obj/messages.o Normal file

Binary file not shown.

BIN
philo_two/obj/old_utilc.o Normal file

Binary file not shown.

BIN
philo_two/obj/parser.o Normal file

Binary file not shown.

Binary file not shown.

BIN
philo_two/obj/time.o Normal file

Binary file not shown.

BIN
philo_two/obj/utilc.o Normal file

Binary file not shown.

BIN
philo_two/philo_two Executable file

Binary file not shown.

View File

@ -10,7 +10,7 @@ void *run_trhead(t_philo *philo)
} }
if (philo->can_run_simulation) if (philo->can_run_simulation)
{ {
while(philo->can_run_simulation && philo->all_struct->smert != 42) while( philo->all_struct->smert != 42)
{ {
if (philo->all_struct->opt) if (philo->all_struct->opt)
{ {
@ -27,6 +27,10 @@ void take_forks(t_philo *philo)
{ {
t_all *all; t_all *all;
all = philo->all_struct; all = philo->all_struct;
messages(philo->all_struct, philo, "thinking");
sem_wait(all->data_philo->sems); sem_wait(all->data_philo->sems);
messages(philo->all_struct, philo, "has taken a fork"); messages(philo->all_struct, philo, "has taken a fork");
sem_wait(all->data_philo->sems); sem_wait(all->data_philo->sems);
@ -34,6 +38,11 @@ void take_forks(t_philo *philo)
} }
void eating(t_philo *philo) void eating(t_philo *philo)
{ {
t_all *all;
all = philo->all_struct;
sem_wait(philo->eat_sem); sem_wait(philo->eat_sem);
messages(philo->all_struct, philo, "eating"); messages(philo->all_struct, philo, "eating");
philo->last_eat = ft_get_time() - philo->all_struct->start_time; philo->last_eat = ft_get_time() - philo->all_struct->start_time;
@ -45,6 +54,9 @@ 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");
sem_post(all->data_philo->sems); sem_post(all->data_philo->sems);
sem_post(all->data_philo->sems); sem_post(all->data_philo->sems);
@ -53,7 +65,6 @@ void sleeping(t_philo *philo)
void cycle(t_philo *philo) void cycle(t_philo *philo)
{ {
messages(philo->all_struct, philo, "thinking");
take_forks(philo); take_forks(philo);
eating(philo); eating(philo);
sleeping(philo); sleeping(philo);

View File

@ -6,7 +6,8 @@ void messages(t_all *all, t_philo *philo, char *str)
if (philo->all_struct->smert != 42) if (philo->all_struct->smert != 42)
{ {
sem_wait(all->write); sem_wait(all->write);
printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str); printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo, str);
sem_post(all->write); sem_post(all->write);
} }
} }

View File

@ -34,7 +34,7 @@ int parse_param(t_all *all, int optional, char **argv)
all->smert = 0; all->smert = 0;
if (optional) if (optional)
{ {
all->number_of_philosophers = ft_atoi(argv[1]) - 1; all->number_of_philosophers = ft_atoi(argv[1]);
all->time_to_die = ft_atoi(argv[2]); all->time_to_die = ft_atoi(argv[2]);
all->time_to_eat = ft_atoi(argv[3]); all->time_to_eat = ft_atoi(argv[3]);
all->time_to_sleep = ft_atoi(argv[4]); all->time_to_sleep = ft_atoi(argv[4]);
@ -44,7 +44,7 @@ int parse_param(t_all *all, int optional, char **argv)
} }
else else
{ {
all->number_of_philosophers = ft_atoi(argv[1]) - 1; all->number_of_philosophers = ft_atoi(argv[1]);
all->time_to_die = ft_atoi(argv[2]); all->time_to_die = ft_atoi(argv[2]);
all->time_to_eat = ft_atoi(argv[3]); all->time_to_eat = ft_atoi(argv[3]);
all->time_to_sleep = ft_atoi(argv[4]); all->time_to_sleep = ft_atoi(argv[4]);

View File

@ -50,6 +50,7 @@ void get_data_for_philo(t_all *all)
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; all->data_philo->philors[i].all_struct = all;
all->data_philo->philors[i].is_eat = 0; all->data_philo->philors[i].is_eat = 0;
all->data_philo->philors[i].last_eat = 0;
i++; i++;
} }
} }
@ -63,7 +64,7 @@ int prepare_resources(t_all *all)
return(1); // TODO выход из программы сделать return(1); // TODO выход из программы сделать
if (all->write == SEM_FAILED) if (all->write == SEM_FAILED)
return(1); return(1);
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers); all->data_philo->philors = (t_philo *)ft_calloc(all->number_of_philosophers, sizeof(t_philo));
get_data_for_philo(all); get_data_for_philo(all);
return (0); return (0);
} }