From ebbd81e4312d945b359d2761acfeb613da1f98c8 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 9 Jan 2021 14:40:23 +0100 Subject: Added antidead lock in philo_one --- philo_one/src/event.c | 11 ++++------- philo_one/src/main.c | 4 ++-- philo_one/src/philo.c | 22 +--------------------- philo_one/src/philo_one.h | 4 ++-- philo_one/src/routine.c | 22 ++++++++++++---------- 5 files changed, 21 insertions(+), 42 deletions(-) (limited to 'philo_one/src') diff --git a/philo_one/src/event.c b/philo_one/src/event.c index df621b4..92ca399 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,25 +6,21 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:27:33 by charles ### ########.fr */ +/* Updated: 2021/01/09 13:12:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void event_take_fork(t_philo *arg) +void event_take_fork(t_philo *arg, pthread_mutex_t *mutex_fork) { if (philo_finished(arg->conf)) return ; - pthread_mutex_lock(arg->fork_left); - pthread_mutex_lock(arg->fork_right); + pthread_mutex_lock(mutex_fork); pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); - if (philo_finished(arg->conf)) - return ; - philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -39,6 +35,7 @@ void event_eat(t_philo *arg) pthread_mutex_unlock(&arg->conf->mutex_stdout); arg->time_last_eat = h_time_now(); h_sleep(arg->conf->timeout_eat); + arg->time_last_eat = h_time_now(); } void event_think(t_philo *arg) diff --git a/philo_one/src/main.c b/philo_one/src/main.c index b2dc5ed..d02fb7a 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:20:18 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:26:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,7 +76,7 @@ int main(int argc, char **argv) return (1); } while (!philo_finished(&conf)) - ; + usleep(500); philo_put_flush(); philos_detach(philos, conf.philo_num); forks_destroy(forks, conf.philo_num); diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 1e98fa0..911f6b9 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:56:10 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:39:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,8 +41,6 @@ bool philos_start(t_philo *philos, long int num) i = -1; while (++i < num) { - if (i % 2 == 0) - continue; if (pthread_create( &philos[i].thread, NULL, @@ -60,23 +58,6 @@ bool philos_start(t_philo *philos, long int num) if (i % 2 == 0) continue; pthread_mutex_unlock(&philos[i].mutex_start); - /* usleep(200); */ - } - i = -1; - while (++i < num) - { - if (i % 2 == 1) - continue; - if (pthread_create( - &philos[i].thread, - NULL, - (t_routine)routine_philo, - (void*)(philos + i)) != 0) - { - while (--i >= 0) - pthread_detach(philos[i].thread); - return (false); - } } usleep(1000); i = -1; @@ -85,7 +66,6 @@ bool philos_start(t_philo *philos, long int num) if (i % 2 == 1) continue; pthread_mutex_unlock(&philos[i].mutex_start); - /* usleep(200); */ } return (true); diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index 2ebe708..910dfe8 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/04 10:38:44 by cacharle ### ########.fr */ +/* Updated: 2021/01/09 14:26:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,7 +82,7 @@ void *routine_death(t_philo *arg); ** io.c */ -void event_take_fork(t_philo *arg); +void event_take_fork(t_philo *arg, pthread_mutex_t *mutex_fork); void event_eat(t_philo *arg); void event_think(t_philo *arg); void event_sleep( diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 05304fe..340f3b7 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/08 18:52:48 by charles ### ########.fr */ +/* Updated: 2021/01/09 14:32:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,20 +39,22 @@ void *routine_philo(t_philo *arg) pthread_mutex_lock(&arg->mutex_start); if (philo_finished(arg->conf)) return (NULL); - /* if (arg->conf->philo_num % 2 == 0 && arg->id % 2 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 0) */ - /* usleep(1000); */ - /* if (arg->conf->philo_num % 2 == 1 && arg->id % 3 == 1) */ - /* usleep(2000); */ arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { - event_take_fork(arg); - /* arg->time_last_eat = h_time_now(); */ + if (arg->id % 2 == 0) + { + event_take_fork(arg, arg->fork_left); + event_take_fork(arg, arg->fork_right); + } + else + { + event_take_fork(arg, arg->fork_right); + event_take_fork(arg, arg->fork_left); + } event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); @@ -67,7 +69,7 @@ void *routine_death(t_philo *arg) { while (!philo_finished(arg->conf) && h_time_now() - arg->time_last_eat < arg->conf->timeout_death) - usleep(500); + usleep(1000); event_die(arg); return (NULL); } -- cgit