diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-02 12:08:59 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-02 12:08:59 +0100 |
| commit | 7aab8f34e8a238ce0688bf9f7e7f0b731902580f (patch) | |
| tree | 6ab0541168d07c2755c51d9ef6875012105816e8 /philo_two/src | |
| parent | 280dace8bf97242151faf8297af31de67f77085c (diff) | |
| download | philosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.tar.gz philosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.tar.bz2 philosophers-7aab8f34e8a238ce0688bf9f7e7f0b731902580f.zip | |
Updated philo_two with philo_three logic, Norming philo_two and philo_three
Diffstat (limited to 'philo_two/src')
| -rw-r--r-- | philo_two/src/event.c | 52 | ||||
| -rw-r--r-- | philo_two/src/main.c | 72 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 7 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 26 |
4 files changed, 58 insertions, 99 deletions
diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 0eaad11..0544042 100644 --- a/philo_two/src/event.c +++ b/philo_two/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/01 15:57:43 by charles ### ########.fr */ +/* Updated: 2021/01/02 11:25:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,79 +14,49 @@ void event_take_fork(t_philo *arg) { - if (philo_finished(arg->conf)) - return ; sem_wait(arg->forks); - if (philo_finished(arg->conf)) - return ; sem_wait(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; philo_put(arg->id, EVENT_FORK); - if (philo_finished(arg->conf)) - return ; sem_post(arg->conf->sem_stdout); } void event_eat(t_philo *arg) { - if (philo_finished(arg->conf)) - return ; sem_wait(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; philo_put(arg->id, EVENT_EAT); - if (philo_finished(arg->conf)) - return ; sem_post(arg->conf->sem_stdout); usleep(arg->conf->timeout_eat * 1000); } void event_think(t_philo *arg) { - if (philo_finished(arg->conf)) - return ; sem_wait(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; philo_put(arg->id, EVENT_THINK); - if (philo_finished(arg->conf)) - return ; sem_post(arg->conf->sem_stdout); } void event_sleep(t_philo *arg) { - if (philo_finished(arg->conf)) - return ; sem_wait(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; philo_put(arg->id, EVENT_SLEEP); - if (philo_finished(arg->conf)) - return ; sem_post(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; sem_post(arg->forks); - if (philo_finished(arg->conf)) - return ; sem_post(arg->forks); - if (philo_finished(arg->conf)) - return ; usleep(arg->conf->timeout_sleep * 1000); } void event_die(t_philo *arg) { - if (philo_finished(arg->conf)) - return ; + long int i; + sem_wait(arg->conf->sem_stdout); - if (philo_finished(arg->conf)) - return ; philo_put(arg->id, EVENT_DIE); - arg->conf->all_alive = false; - if (philo_finished(arg->conf)) - return ; - sem_post(arg->conf->sem_stdout); + if (arg->conf->meal_num == -1) + sem_post(arg->conf->sem_finish); + else + { + i = -1; + while (++i < arg->conf->philo_num) + sem_post(arg->conf->sem_finish); + } } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 85994da..e88ccac 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,42 +6,33 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/01 15:57:40 by charles ### ########.fr */ +/* Updated: 2021/01/02 12:05:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -#define PHILO_SEM_NAME "semaphore_philo_two" -#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" -#define PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME "semaphore_philo_two_meal_num" +#define PHILO_SEM_NAME "semaphore_philo_two" +#define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" +#define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish" static int st_destroy( - sem_t *forks, t_philo *philos, - pthread_t *threads, - t_philo_conf *conf) + pthread_t *threads) { - /* int i; */ - /* */ - /* i = -1; */ - /* while (++i < conf->philo_num) */ - /* sem_post(forks); */ - sem_close(forks); sem_unlink(PHILO_SEM_NAME); - sem_close(conf->sem_stdout); sem_unlink(PHILO_SEM_STDOUT_NAME); - sem_close(conf->sem_meal_num_finished_counter); - sem_unlink(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME); + sem_unlink(PHILO_SEM_FINISH_NAME); free(philos); free(threads); return (1); } -static sem_t *st_sem_create(const char *name, unsigned int value) +static bool st_sem_create(const char *name, unsigned int value, sem_t **sem) { sem_unlink(name); - return (sem_open(name, O_CREAT | O_EXCL, 0700, value)); + return ((*sem = sem_open(name, O_CREAT | O_EXCL, 0700, value)) + != SEM_FAILED); } static int st_setup( @@ -52,21 +43,15 @@ static int st_setup( { long int i; - *forks = st_sem_create(PHILO_SEM_NAME, conf->philo_num); - if (*forks == SEM_FAILED) - return (1); - conf->sem_stdout = st_sem_create(PHILO_SEM_STDOUT_NAME, 1); - if (conf->sem_stdout == SEM_FAILED) - return (1); - conf->sem_meal_num_finished_counter = st_sem_create(PHILO_SEM_MEAL_NUM_FINISHED_COUNTER_NAME, 1); - if (conf->sem_meal_num_finished_counter == SEM_FAILED) + if (!st_sem_create(PHILO_SEM_NAME, conf->philo_num, forks) || + !st_sem_create(PHILO_SEM_STDOUT_NAME, 1, &conf->sem_stdout) || + !st_sem_create(PHILO_SEM_FINISH_NAME, + conf->meal_num == -1 ? 1 : conf->philo_num, &conf->sem_finish)) return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) - return (st_destroy(*forks, *philos, *threads, conf)); - conf->all_alive = true; - conf->meal_num_finished_counter = 0; + return (st_destroy(*philos, *threads)); i = -1; while (++i < conf->philo_num) { @@ -75,13 +60,34 @@ static int st_setup( { while (--i >= 0) pthread_detach((*threads)[i]); - return (st_destroy(*forks, *philos, *threads, conf)); + return (st_destroy(*philos, *threads)); } usleep(200); } return (0); } +static void st_wait(t_philo_conf *conf) +{ + long int i; + + if (conf->meal_num == -1) + { + sem_wait(conf->sem_finish); + sem_wait(conf->sem_finish); + } + else + { + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_finish); + i = -1; + while (++i < conf->philo_num) + sem_wait(conf->sem_finish); + sem_wait(conf->sem_stdout); + } +} + int main(int argc, char **argv) { long int i; @@ -96,12 +102,10 @@ int main(int argc, char **argv) return (0); if (st_setup(&conf, &philos, &forks, &threads) != 0) return (1); - while (!philo_finished(&conf)) - ; - conf.all_alive = false; + st_wait(&conf); i = -1; while (++i < conf.philo_num) pthread_detach(threads[i]); - st_destroy(forks, philos, threads, &conf); + st_destroy(philos, threads); return (0); } diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index ec5783b..f0c55ba 100644 --- a/philo_two/src/philo_two.h +++ b/philo_two/src/philo_two.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:47:23 by cacharle #+# #+# */ -/* Updated: 2021/01/01 14:18:57 by charles ### ########.fr */ +/* Updated: 2021/01/02 11:20:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,10 +29,8 @@ typedef struct t_time timeout_eat; t_time timeout_sleep; long int meal_num; - bool all_alive; - long int meal_num_finished_counter; sem_t *sem_stdout; - sem_t *sem_meal_num_finished_counter; + sem_t *sem_finish; } t_philo_conf; typedef struct @@ -47,7 +45,6 @@ typedef struct ** routine.c */ -bool philo_finished(t_philo_conf *conf); void *routine_philo(t_philo *arg); void *routine_death(t_philo *arg); t_philo *routine_create_philos(t_philo_conf *conf, sem_t *forks); diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 783011c..1066e97 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,44 +6,33 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/01 14:23:59 by charles ### ########.fr */ +/* Updated: 2021/01/02 11:59:27 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_two.h" -inline bool philo_finished(t_philo_conf *conf) -{ - return (!conf->all_alive || - (conf->meal_num != -1 && - conf->meal_num_finished_counter == conf->philo_num)); -} - void *routine_philo(t_philo *arg) { long int eat_counter; pthread_t thread_death; - if (philo_finished(arg->conf)) - return (NULL); arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); eat_counter = 0; event_think(arg); - while (!philo_finished(arg->conf)) + while (true) { event_take_fork(arg); event_take_fork(arg); event_eat(arg); arg->time_last_eat = h_time_now(); - eat_counter++; - if (!philo_finished(arg->conf) && arg->conf->meal_num != -1 && - eat_counter == arg->conf->meal_num) + if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) { - sem_wait(arg->conf->sem_meal_num_finished_counter); - arg->conf->meal_num_finished_counter++; - sem_post(arg->conf->sem_meal_num_finished_counter); + sem_wait(arg->conf->sem_stdout); + sem_post(arg->conf->sem_finish); + sem_post(arg->conf->sem_stdout); } event_sleep(arg); event_think(arg); @@ -57,8 +46,7 @@ void *routine_death(t_philo *arg) t_time current; current = h_time_now(); - while (!philo_finished(arg->conf) - && current - arg->time_last_eat < arg->conf->timeout_death) + while (current - arg->time_last_eat < arg->conf->timeout_death) { current = h_time_now(); usleep(200); |
