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 /philo_three/src | |
| 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
Diffstat (limited to 'philo_three/src')
| -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 |
4 files changed, 15 insertions, 8 deletions
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); |
