diff --git a/.vscode/launch.json b/.vscode/launch.json index 27c4e51..855d609 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "type": "lldb", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/philo_two/philo_two", + "program": "${workspaceFolder}/philo_one/philo_one", "args": ["5", "800", "200", "200"], "cwd": "${workspaceFolder}" } diff --git a/philo_one/srcs/logic.c b/philo_one/srcs/logic.c index aa62b1f..c170400 100644 --- a/philo_one/srcs/logic.c +++ b/philo_one/srcs/logic.c @@ -27,6 +27,9 @@ void take_forks(t_philo *philo) { t_all *all; 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]); messages(philo->all_struct, philo, "has taken a 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) { + t_all *all; + all = philo->all_struct; messages(philo->all_struct, philo, "eating"); philo->last_eat = ft_get_time() - philo->all_struct->start_time; 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++; } void sleeping(t_philo *philo) @@ -44,8 +51,6 @@ void sleeping(t_philo *philo) t_all *all; all = philo->all_struct; 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); } diff --git a/philo_one/srcs/prepare_simulation.c b/philo_one/srcs/prepare_simulation.c index 6fab9e1..9a9886b 100644 --- a/philo_one/srcs/prepare_simulation.c +++ b/philo_one/srcs/prepare_simulation.c @@ -47,8 +47,8 @@ void get_fork_for_philo(t_all *all) } else { - all->data_philo->philors[i].more_fork = return_big(i, i + 1); - all->data_philo->philors[i].less_fork = return_less(i, i + 1); + all->data_philo->philors[i].more_fork = i + 1; + all->data_philo->philors[i].less_fork = i; } all->data_philo->philors[i].died = 0; all->data_philo->philors[i].nbr_philo = i; @@ -66,13 +66,13 @@ int prepare_resources(t_all *all) i = 0; 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) { pthread_mutex_init(&all->data_philo->mutex[i], NULL); 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); return (0); } diff --git a/philo_two/obj/ft_itoa.o b/philo_two/obj/ft_itoa.o new file mode 100644 index 0000000..74b3560 Binary files /dev/null and b/philo_two/obj/ft_itoa.o differ diff --git a/philo_two/obj/ft_strjoin.o b/philo_two/obj/ft_strjoin.o new file mode 100644 index 0000000..66270ab Binary files /dev/null and b/philo_two/obj/ft_strjoin.o differ diff --git a/philo_two/obj/logic.o b/philo_two/obj/logic.o new file mode 100644 index 0000000..fb1dfa2 Binary files /dev/null and b/philo_two/obj/logic.o differ diff --git a/philo_two/obj/main.o b/philo_two/obj/main.o new file mode 100644 index 0000000..dfc0ba2 Binary files /dev/null and b/philo_two/obj/main.o differ diff --git a/philo_two/obj/messages.o b/philo_two/obj/messages.o new file mode 100644 index 0000000..d829941 Binary files /dev/null and b/philo_two/obj/messages.o differ diff --git a/philo_two/obj/old_utilc.o b/philo_two/obj/old_utilc.o new file mode 100644 index 0000000..dc3a617 Binary files /dev/null and b/philo_two/obj/old_utilc.o differ diff --git a/philo_two/obj/parser.o b/philo_two/obj/parser.o new file mode 100644 index 0000000..afafe8d Binary files /dev/null and b/philo_two/obj/parser.o differ diff --git a/philo_two/obj/prepare_simulation.o b/philo_two/obj/prepare_simulation.o new file mode 100644 index 0000000..7343fa7 Binary files /dev/null and b/philo_two/obj/prepare_simulation.o differ diff --git a/philo_two/obj/time.o b/philo_two/obj/time.o new file mode 100644 index 0000000..294aaf7 Binary files /dev/null and b/philo_two/obj/time.o differ diff --git a/philo_two/obj/utilc.o b/philo_two/obj/utilc.o new file mode 100644 index 0000000..b15b86f Binary files /dev/null and b/philo_two/obj/utilc.o differ diff --git a/philo_two/philo_two b/philo_two/philo_two new file mode 100755 index 0000000..97f5793 Binary files /dev/null and b/philo_two/philo_two differ diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index 413c1c7..2c060b5 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -10,7 +10,7 @@ void *run_trhead(t_philo *philo) } 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) { @@ -27,6 +27,10 @@ void take_forks(t_philo *philo) { t_all *all; all = philo->all_struct; + + + + messages(philo->all_struct, philo, "thinking"); sem_wait(all->data_philo->sems); messages(philo->all_struct, philo, "has taken a fork"); sem_wait(all->data_philo->sems); @@ -34,6 +38,11 @@ void take_forks(t_philo *philo) } void eating(t_philo *philo) { + t_all *all; + all = philo->all_struct; + + + sem_wait(philo->eat_sem); messages(philo->all_struct, philo, "eating"); philo->last_eat = ft_get_time() - philo->all_struct->start_time; @@ -45,6 +54,9 @@ void sleeping(t_philo *philo) { t_all *all; all = philo->all_struct; + + + messages(philo->all_struct, philo, "sleeping"); 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) { - messages(philo->all_struct, philo, "thinking"); take_forks(philo); eating(philo); sleeping(philo); diff --git a/philo_two/srcs/messages.c b/philo_two/srcs/messages.c index 214ff31..35dd993 100644 --- a/philo_two/srcs/messages.c +++ b/philo_two/srcs/messages.c @@ -6,7 +6,8 @@ void messages(t_all *all, t_philo *philo, char *str) if (philo->all_struct->smert != 42) { 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); } + } diff --git a/philo_two/srcs/parser.c b/philo_two/srcs/parser.c index fd9fa19..55f6723 100644 --- a/philo_two/srcs/parser.c +++ b/philo_two/srcs/parser.c @@ -34,7 +34,7 @@ int parse_param(t_all *all, int optional, char **argv) all->smert = 0; 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_eat = ft_atoi(argv[3]); all->time_to_sleep = ft_atoi(argv[4]); @@ -44,7 +44,7 @@ int parse_param(t_all *all, int optional, char **argv) } 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_eat = ft_atoi(argv[3]); all->time_to_sleep = ft_atoi(argv[4]); diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index 7e18904..43d0bf9 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -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].all_struct = all; all->data_philo->philors[i].is_eat = 0; + all->data_philo->philors[i].last_eat = 0; i++; } } @@ -63,7 +64,7 @@ int prepare_resources(t_all *all) return(1); // TODO выход из программы сделать if (all->write == SEM_FAILED) 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); return (0); }