diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-12-30 14:06:17 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-12-30 14:06:17 +0100 |
| commit | cf2ef2c3901f3da524079a9ad912dfb99e115a7e (patch) | |
| tree | 7868f1f02a3fab1aa74606c1f9a3a00c0287976f /philo_one/src | |
| parent | 2baa67fd7ad31fe53ab21d257a104478e787fb62 (diff) | |
| download | philosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.tar.gz philosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.tar.bz2 philosophers-cf2ef2c3901f3da524079a9ad912dfb99e115a7e.zip | |
Fixed meal_num args for philo_one
Diffstat (limited to 'philo_one/src')
| -rw-r--r-- | philo_one/src/event.c | 21 | ||||
| -rw-r--r-- | philo_one/src/main.c | 17 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 3 | ||||
| -rw-r--r-- | philo_one/src/philo_one.h | 4 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 14 |
5 files changed, 39 insertions, 20 deletions
diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 5261f4c..ef3d7a5 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/10/05 14:27:39 by cacharle ### ########.fr */ +/* Updated: 2020/12/30 13:39:56 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,19 +24,12 @@ void event_take_fork(t_philo *arg, pthread_mutex_t *fork) void event_eat(t_philo *arg) { - int eat_counter; - - eat_counter = 0; - while (eat_counter < arg->conf->meal_num) - { - pthread_mutex_lock(&arg->conf->mutex_stdout); - if (!arg->conf->all_alive) - return ; - philo_put(arg->id, EVENT_EAT); - pthread_mutex_unlock(&arg->conf->mutex_stdout); - usleep(arg->conf->timeout_eat * 1000); - eat_counter++; - } + pthread_mutex_lock(&arg->conf->mutex_stdout); + if (!arg->conf->all_alive) + return ; + philo_put(arg->id, EVENT_EAT); + pthread_mutex_unlock(&arg->conf->mutex_stdout); + usleep(arg->conf->timeout_eat * 1000); } void event_think(t_philo *arg) diff --git a/philo_one/src/main.c b/philo_one/src/main.c index eec5d4e..a9a6a63 100644 --- a/philo_one/src/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/10/05 15:42:09 by cacharle ### ########.fr */ +/* Updated: 2020/12/30 14:04:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,15 @@ static int st_setup( free(*philos); return (1); } + if (pthread_mutex_init(&conf->mutex_meal_num_finished_counter, NULL) != 0) + { + forks_destroy(*forks, conf->philo_num); + free(*philos); + pthread_mutex_destroy(&conf->mutex_stdout); + return (1); + } conf->all_alive = true; + conf->meal_num_finished_counter = 0; return (0); } @@ -42,7 +50,7 @@ int main(int argc, char **argv) if (!parse_args((t_philo_args*)&conf, argc, argv)) return (1); - if (conf.philo_num == 0) + if (conf.philo_num == 0 || conf.meal_num == 0) return (0); if (st_setup(&conf, &philos, &forks) != 0) return (1); @@ -52,11 +60,14 @@ int main(int argc, char **argv) free(philos); return (1); } - while (conf.all_alive) + while (conf.all_alive && (conf.meal_num != -1 && conf.meal_num_finished_counter != conf.philo_num)) + /* printf("%ld\n", conf.meal_num_finished_counter); */ ; + conf.all_alive = false; philos_detach(philos, conf.philo_num); forks_destroy(forks, conf.philo_num); pthread_mutex_destroy(&conf.mutex_stdout); + pthread_mutex_destroy(&conf.mutex_meal_num_finished_counter); free(philos); return (0); } diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 30c0a7c..3e3d90f 100644 --- a/philo_one/src/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/10/05 16:13:10 by cacharle ### ########.fr */ +/* Updated: 2020/12/30 11:58:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ bool philos_start(t_philo *philos, int num) pthread_detach(philos[i].thread); return (false); } + usleep(200); } return (true); } diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index ae7d040..341c1a8 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/10/05 14:28:24 by cacharle ### ########.fr */ +/* Updated: 2020/12/30 13:44:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,9 @@ typedef struct t_time timeout_sleep; long int meal_num; bool all_alive; + long int meal_num_finished_counter; pthread_mutex_t mutex_stdout; + pthread_mutex_t mutex_meal_num_finished_counter; } t_philo_conf; typedef struct s_philo diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index c1a7a4f..280f9d6 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/09/30 09:43:16 by cacharle ### ########.fr */ +/* Updated: 2020/12/30 13:44:25 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ void *routine_philo(t_philo *arg) { pthread_t thread_death; + long int eat_counter; if (!arg->conf->all_alive) return (NULL); @@ -22,12 +23,20 @@ void *routine_philo(t_philo *arg) if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); event_think(arg); + eat_counter = 0; while (arg->conf->all_alive) { event_take_fork(arg, arg->fork_left); event_take_fork(arg, arg->fork_right); arg->time_last_eat = h_time_now(); event_eat(arg); + eat_counter++; + if (arg->conf->meal_num != -1 && eat_counter == arg->conf->meal_num) + { + pthread_mutex_lock(&arg->conf->mutex_meal_num_finished_counter); + arg->conf->meal_num_finished_counter++; + pthread_mutex_unlock(&arg->conf->mutex_meal_num_finished_counter); + } event_sleep(arg, arg->fork_right, arg->fork_left); event_think(arg); } @@ -42,7 +51,10 @@ void *routine_death(t_philo *arg) current = h_time_now(); while (arg->conf->all_alive && current - arg->time_last_eat < arg->conf->timeout_death) + { current = h_time_now(); + usleep(200); + } if (!arg->conf->all_alive) return (NULL); event_die(arg); |
