доделать выход на вторых философах

This commit is contained in:
Matthos Denys 2021-05-20 14:14:31 +03:00
parent 62373dc04b
commit e27b4bcc36
6 changed files with 17 additions and 12 deletions

4
.vscode/launch.json vendored
View File

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

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */ /* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */ /* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:21:47 by mdenys ### ########.fr */ /* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,6 +38,7 @@ typedef struct s_all
sem_t *write; sem_t *write;
int smert; int smert;
int opt; int opt;
sem_t *bucket;
} t_all; } t_all;
typedef struct s_philo typedef struct s_philo

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */ /* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */ /* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
/* Updated: 2021/05/20 13:36:21 by mdenys ### ########.fr */ /* Updated: 2021/05/20 14:12:10 by mdenys ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,10 +21,11 @@ void *run_trhead(t_philo *philo)
if (philo->count_eat == philo->all_struct->etarate_meal) if (philo->count_eat == philo->all_struct->etarate_meal)
{ {
philo->all_struct->smert = 42; philo->all_struct->smert = 42;
break ; return (NULL);
} }
} }
cycle(philo); else
cycle(philo);
} }
return (NULL); return (NULL);
} }
@ -34,10 +35,12 @@ void take_forks(t_philo *philo)
t_all *all; t_all *all;
all = philo->all_struct; all = philo->all_struct;
sem_wait(all->bucket);
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);
messages(philo->all_struct, philo, "has taken a fork"); messages(philo->all_struct, philo, "has taken a fork");
sem_post(all->bucket);
} }
void eating(t_philo *philo) void eating(t_philo *philo)
@ -46,9 +49,9 @@ void eating(t_philo *philo)
all = philo->all_struct; all = philo->all_struct;
sem_wait(philo->eat_sem); sem_wait(philo->eat_sem);
philo->count_eat++;
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
messages(philo->all_struct, philo, "eating"); messages(philo->all_struct, philo, "eating");
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
philo->count_eat++;
ft_msleep(philo->all_struct->time_to_eat); ft_msleep(philo->all_struct->time_to_eat);
sem_post(philo->eat_sem); sem_post(philo->eat_sem);
} }

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */ /* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */ /* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
/* Updated: 2021/05/20 13:27:11 by mdenys ### ########.fr */ /* Updated: 2021/05/20 14:13:00 by mdenys ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */ /* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */ /* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:12:56 by mdenys ### ########.fr */ /* Updated: 2021/05/20 14:11:43 by mdenys ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -60,7 +60,7 @@ int parse_param(t_all *all, int optional, char **argv)
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]);
all->etarate_meal = 0; all->etarate_meal = -1;
all->opt = 0; all->opt = 0;
return (check_param(all, 0)); return (check_param(all, 0));
} }

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */ /* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */ /* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
/* Updated: 2021/05/20 13:21:04 by mdenys ### ########.fr */ /* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,6 +40,7 @@ int prepare_resources(t_all *all)
all->data_philo = ft_calloc(sizeof(t_philo_data), 1); all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils); all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils);
all->write = reopen_sem(_SEM_WRITE_LOCK, 1); all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
all->bucket = reopen_sem("bucket", 1);
all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \ all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \
+ 1, sizeof(t_philo)); + 1, sizeof(t_philo));
get_data_f_philo(all); get_data_f_philo(all);