diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 22:11:19 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 22:11:19 +0100 |
| commit | a69a877b3a2758aafe3de0db87a56063b28ed00f (patch) | |
| tree | 2f0e61aa28cea420fd808fb51eee46ba004d9698 | |
| parent | 36c5e3a81de8b910bc04a37994b53296c1540353 (diff) | |
| download | philosophers-a69a877b3a2758aafe3de0db87a56063b28ed00f.tar.gz philosophers-a69a877b3a2758aafe3de0db87a56063b28ed00f.tar.bz2 philosophers-a69a877b3a2758aafe3de0db87a56063b28ed00f.zip | |
Add sem_grab in philo_two and philo_three to fix philosophers each taking 1 fork
| -rw-r--r-- | common/src/io.c | 2 | ||||
| -rw-r--r-- | philo_one/src/event.c | 3 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 33 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 19 | ||||
| -rw-r--r-- | philo_three/src/child.c | 9 | ||||
| -rw-r--r-- | philo_three/src/event.c | 4 | ||||
| -rw-r--r-- | philo_three/src/main.c | 5 | ||||
| -rw-r--r-- | philo_three/src/philo_three.h | 5 | ||||
| -rw-r--r-- | philo_two/src/event.c | 4 | ||||
| -rw-r--r-- | philo_two/src/main.c | 17 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 3 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 11 |
12 files changed, 77 insertions, 38 deletions
diff --git a/common/src/io.c b/common/src/io.c index ae624fb..6fc202b 100644 --- a/common/src/io.c +++ b/common/src/io.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:19:47 by charles ### ########.fr */ +/* Updated: 2021/01/08 21:25:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 025435f..df621b4 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: 2021/01/08 16:02:43 by charles ### ########.fr */ +/* Updated: 2021/01/08 18:27:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ void event_eat(t_philo *arg) return ; philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); + arg->time_last_eat = h_time_now(); h_sleep(arg->conf->timeout_eat); } diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 589e1c7..1e98fa0 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: 2021/01/08 16:15:10 by charles ### ########.fr */ +/* Updated: 2021/01/08 18:56:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,8 @@ 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, @@ -55,8 +57,35 @@ bool philos_start(t_philo *philos, long int num) i = -1; while (++i < num) { + if (i % 2 == 0) + continue; pthread_mutex_unlock(&philos[i].mutex_start); - /* usleep(1000); */ + /* 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; + while (++i < num) + { + if (i % 2 == 1) + continue; + pthread_mutex_unlock(&philos[i].mutex_start); + /* usleep(200); */ } return (true); diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index b3fc71a..05304fe 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: 2021/01/08 16:14:57 by charles ### ########.fr */ +/* Updated: 2021/01/08 18:52:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,23 +35,24 @@ void *routine_philo(t_philo *arg) pthread_t thread_death; long int eat_counter; + eat_counter = 0; 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); + /* 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); - eat_counter = 0; + arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { event_take_fork(arg); - arg->time_last_eat = h_time_now(); + /* arg->time_last_eat = h_time_now(); */ event_eat(arg); eat_counter++; st_check_meal_num_finished(arg, eat_counter); diff --git a/philo_three/src/child.c b/philo_three/src/child.c index 88a4b7c..144f7ba 100644 --- a/philo_three/src/child.c +++ b/philo_three/src/child.c @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 14:36:16 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:42:02 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:52:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void *routine_death(t_philo *philo) while (current - philo->time_last_eat < philo->conf->timeout_death) { current = h_time_now(); - usleep(500); + usleep(1000); } event_die(philo); return (NULL); @@ -33,7 +33,10 @@ void st_child_loop(t_philo *philo) eat_counter = 0; while (true) { + sem_wait(philo->sem_grab); event_take_fork(philo); + event_take_fork(philo); + sem_post(philo->sem_grab); philo->time_last_eat = h_time_now(); event_eat(philo); eat_counter++; @@ -61,10 +64,10 @@ pid_t child_start(t_philo *philo) philo->sem_stdout = sem_open(PHILO_SEM_STDOUT_NAME, 0); philo->sem_finish = sem_open(PHILO_SEM_FINISH_NAME, 0); philo->sem_start = sem_open(PHILO_SEM_START_NAME, 0); + philo->sem_grab = sem_open(PHILO_SEM_GRAB_NAME, 0); philo->time_last_eat = h_time_now(); pthread_create(&thread_death, NULL, (t_routine)routine_death, philo); pthread_detach(thread_death); - event_think(philo); sem_wait(philo->sem_start); st_child_loop(philo); exit(0); diff --git a/philo_three/src/event.c b/philo_three/src/event.c index 2a4ccf7..1c7fa14 100644 --- a/philo_three/src/event.c +++ b/philo_three/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:13:02 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:25:44 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,8 @@ void event_take_fork(t_philo *philo) { sem_wait(philo->forks); - sem_wait(philo->forks); sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_FORK, philo->initial_time); - philo_put(philo->id, EVENT_FORK, philo->initial_time); philo_put_flush(); sem_post(philo->sem_stdout); } diff --git a/philo_three/src/main.c b/philo_three/src/main.c index 0b3397c..8790140 100644 --- a/philo_three/src/main.c +++ b/philo_three/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:45:24 by cacharle #+# #+# */ -/* Updated: 2021/01/08 15:11:05 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:23:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,8 @@ static int st_destroy(t_sems *sems, pid_t *pids, int philo_num) sem_unlink(PHILO_SEM_FINISH_NAME); sem_close(sems->sem_start); sem_unlink(PHILO_SEM_START_NAME); + sem_close(sems->sem_grab); + sem_unlink(PHILO_SEM_GRAB_NAME); return (1); } @@ -58,6 +60,7 @@ static int st_setup( args->meal_num == -1 ? 1 : args->philo_num)) == SEM_FAILED || (sems->sem_start = st_sem_create(PHILO_SEM_START_NAME, args->philo_num)) == SEM_FAILED + || (sems->sem_grab = st_sem_create(PHILO_SEM_GRAB_NAME, 1)) == SEM_FAILED || (*pids = malloc(sizeof(pid_t) * args->philo_num)) == NULL) return (st_destroy(sems, *pids, 0)); i = -1; diff --git a/philo_three/src/philo_three.h b/philo_three/src/philo_three.h index fe3441b..f2b841d 100644 --- a/philo_three/src/philo_three.h +++ b/philo_three/src/philo_three.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/15 00:46:26 by cacharle #+# #+# */ -/* Updated: 2021/01/04 11:01:18 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 20:25:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ # define PHILO_SEM_STDOUT_NAME "semaphore_philo_three_stdout" # define PHILO_SEM_FINISH_NAME "semaphore_philo_three_finish" # define PHILO_SEM_START_NAME "semaphore_philo_three_start" +# define PHILO_SEM_GRAB_NAME "semaphore_philo_three_grab" typedef struct s_philo { @@ -38,6 +39,7 @@ typedef struct s_philo sem_t *sem_stdout; sem_t *sem_finish; sem_t *sem_start; + sem_t *sem_grab; } t_philo; typedef struct s_sems @@ -46,6 +48,7 @@ typedef struct s_sems sem_t *sem_stdout; sem_t *sem_finish; sem_t *sem_start; + sem_t *sem_grab; } t_sems; pid_t child_start(t_philo *arg); diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 4eafd90..3ce471e 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/04 12:15:05 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 19:54:40 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,8 @@ void event_take_fork(t_philo *arg) { sem_wait(arg->forks); - sem_wait(arg->forks); sem_wait(arg->conf->sem_stdout); philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); - philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } diff --git a/philo_two/src/main.c b/philo_two/src/main.c index 2d5c003..034518c 100644 --- a/philo_two/src/main.c +++ b/philo_two/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 22:45:23 by cacharle #+# #+# */ -/* Updated: 2021/01/08 16:32:15 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:16:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #define PHILO_SEM_STDOUT_NAME "semaphore_philo_two_stdout" #define PHILO_SEM_FINISH_NAME "semaphore_philo_two_finish" #define PHILO_SEM_START_NAME "semaphore_philo_two_start" +#define PHILO_SEM_GRAB_NAME "semaphore_philo_two_grab" static int st_destroy( t_philo *philos, @@ -25,6 +26,7 @@ static int st_destroy( sem_unlink(PHILO_SEM_STDOUT_NAME); sem_unlink(PHILO_SEM_FINISH_NAME); sem_unlink(PHILO_SEM_START_NAME); + sem_unlink(PHILO_SEM_GRAB_NAME); free(philos); free(threads); return (1); @@ -60,7 +62,8 @@ static int st_setup( !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) || - !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start)) + !st_sem_create(PHILO_SEM_START_NAME, conf->philo_num, &conf->sem_start) || + !st_sem_create(PHILO_SEM_GRAB_NAME, 1, &conf->sem_grab)) return (1); *threads = NULL; if ((*philos = routine_create_philos(conf, *forks)) == NULL || @@ -69,9 +72,9 @@ static int st_setup( i = -1; while (++i < conf->philo_num) sem_wait(conf->sem_start); - conf->initial_time = h_time_now(); i = -1; while (++i < conf->philo_num) + { if (pthread_create(*threads + i, NULL, (t_routine)routine_philo, *philos + i) != 0) { @@ -79,6 +82,11 @@ static int st_setup( pthread_detach((*threads)[i]); return (st_destroy(*philos, *threads)); } + } + conf->initial_time = h_time_now(); + i = -1; + while (++i < conf->philo_num) + sem_post(conf->sem_start); return (0); } @@ -119,9 +127,6 @@ int main(int argc, char **argv) pthread_t thread_flush; pthread_create(&thread_flush, NULL, (t_routine)routine_flush, (void*)&conf); pthread_detach(thread_flush); - i = -1; - while (++i < conf.philo_num) - sem_post(conf.sem_start); st_wait(&conf); philo_put_flush(); i = -1; diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index bde41c1..91cdbef 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/04 09:35:42 by cacharle ### ########.fr */ +/* Updated: 2021/01/08 20:10:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ typedef struct sem_t *sem_stdout; sem_t *sem_finish; sem_t *sem_start; + sem_t *sem_grab; } t_philo_conf; typedef struct diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index d69488d..52bf53d 100644 --- a/philo_two/src/routine.c +++ b/philo_two/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 23:00:07 by cacharle #+# #+# */ -/* Updated: 2021/01/08 16:31:27 by charles ### ########.fr */ +/* Updated: 2021/01/08 20:18:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,18 +19,15 @@ void *routine_philo(t_philo *arg) eat_counter = 0; sem_wait(arg->conf->sem_start); - /* 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(500); */ arg->time_last_eat = h_time_now(); if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) return (NULL); while (true) { + sem_wait(arg->conf->sem_grab); event_take_fork(arg); + event_take_fork(arg); + sem_post(arg->conf->sem_grab); arg->time_last_eat = h_time_now(); event_eat(arg); if (arg->conf->meal_num != -1 && ++eat_counter == arg->conf->meal_num) |
