diff --git a/.vscode/launch.json b/.vscode/launch.json index 855d609..af52e7f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,8 +8,8 @@ "type": "lldb", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/philo_one/philo_one", - "args": ["5", "800", "200", "200"], + "program": "${workspaceFolder}/philo_two/philo_two", + "args": ["5", "800", "200", "200", "7"], "cwd": "${workspaceFolder}" } ] diff --git a/philo_two/includes/philo_header.h b/philo_two/includes/philo_header.h index de130e0..a1fad0c 100644 --- a/philo_two/includes/philo_header.h +++ b/philo_two/includes/philo_header.h @@ -6,7 +6,7 @@ /* 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; int smert; int opt; + sem_t *bucket; } t_all; typedef struct s_philo diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index bf34215..fb885eb 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -6,7 +6,7 @@ /* 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) { philo->all_struct->smert = 42; - break ; + return (NULL); } } - cycle(philo); + else + cycle(philo); } return (NULL); } @@ -34,10 +35,12 @@ void take_forks(t_philo *philo) t_all *all; all = philo->all_struct; + sem_wait(all->bucket); sem_wait(all->data_philo->sems); messages(philo->all_struct, philo, "has taken a fork"); sem_wait(all->data_philo->sems); messages(philo->all_struct, philo, "has taken a fork"); + sem_post(all->bucket); } void eating(t_philo *philo) @@ -46,9 +49,9 @@ void eating(t_philo *philo) all = philo->all_struct; 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"); + philo->last_eat = ft_get_time() - philo->all_struct->start_time; + philo->count_eat++; ft_msleep(philo->all_struct->time_to_eat); sem_post(philo->eat_sem); } diff --git a/philo_two/srcs/observer.c b/philo_two/srcs/observer.c index d271846..afea00a 100644 --- a/philo_two/srcs/observer.c +++ b/philo_two/srcs/observer.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/philo_two/srcs/parser.c b/philo_two/srcs/parser.c index 3369c66..c78b599 100644 --- a/philo_two/srcs/parser.c +++ b/philo_two/srcs/parser.c @@ -6,7 +6,7 @@ /* 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_eat = ft_atoi(argv[3]); all->time_to_sleep = ft_atoi(argv[4]); - all->etarate_meal = 0; + all->etarate_meal = -1; all->opt = 0; return (check_param(all, 0)); } diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index 66b94eb..894bc9b 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -6,7 +6,7 @@ /* 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->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils); 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 \ + 1, sizeof(t_philo)); get_data_f_philo(all);