diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-03 14:04:22 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-03 14:04:22 +0100 |
| commit | e874ad94a31a8259b8eb7ad2865767c081bcd279 (patch) | |
| tree | e629a6cef53b2c6cafcde9f8bbf5d1f7678753d6 /philo_two | |
| parent | 592c515b742222c637478b3339b95cd2de8b4379 (diff) | |
| download | philosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.tar.gz philosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.tar.bz2 philosophers-e874ad94a31a8259b8eb7ad2865767c081bcd279.zip | |
Fixing philo_one/philo_two dying by sleeping more when checking the death, Fixing timestamp in the wrong order in philo_three
Diffstat (limited to 'philo_two')
| -rw-r--r-- | philo_two/src/event.c | 12 | ||||
| -rw-r--r-- | philo_two/src/main.c | 3 | ||||
| -rw-r--r-- | philo_two/src/philo_two.h | 3 | ||||
| -rw-r--r-- | philo_two/src/routine.c | 4 |
4 files changed, 12 insertions, 10 deletions
diff --git a/philo_two/src/event.c b/philo_two/src/event.c index 0544042..eead2f5 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/02 11:25:08 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:55:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,14 @@ void event_take_fork(t_philo *arg) { sem_wait(arg->forks); sem_wait(arg->conf->sem_stdout); - philo_put(arg->id, EVENT_FORK); + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } void event_eat(t_philo *arg) { sem_wait(arg->conf->sem_stdout); - philo_put(arg->id, EVENT_EAT); + philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); usleep(arg->conf->timeout_eat * 1000); } @@ -31,14 +31,14 @@ void event_eat(t_philo *arg) void event_think(t_philo *arg) { sem_wait(arg->conf->sem_stdout); - philo_put(arg->id, EVENT_THINK); + philo_put(arg->id, EVENT_THINK, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); } void event_sleep(t_philo *arg) { sem_wait(arg->conf->sem_stdout); - philo_put(arg->id, EVENT_SLEEP); + philo_put(arg->id, EVENT_SLEEP, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); sem_post(arg->forks); sem_post(arg->forks); @@ -50,7 +50,7 @@ void event_die(t_philo *arg) long int i; sem_wait(arg->conf->sem_stdout); - philo_put(arg->id, EVENT_DIE); + philo_put(arg->id, EVENT_DIE, arg->conf->initial_time); if (arg->conf->meal_num == -1) sem_post(arg->conf->sem_finish); else diff --git a/philo_two/src/main.c b/philo_two/src/main.c index e3994e2..0f823b5 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/02 12:49:06 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:54:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ static int st_setup( if ((*philos = routine_create_philos(conf, *forks)) == NULL || (*threads = malloc(sizeof(pthread_t) * conf->philo_num)) == NULL) return (st_destroy(*philos, *threads)); + conf->initial_time = h_time_now(); i = -1; while (++i < conf->philo_num) { diff --git a/philo_two/src/philo_two.h b/philo_two/src/philo_two.h index f0c55ba..8261b0c 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/02 11:20:45 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 13:55:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ typedef struct t_time timeout_eat; t_time timeout_sleep; long int meal_num; + t_time initial_time; sem_t *sem_stdout; sem_t *sem_finish; } t_philo_conf; diff --git a/philo_two/src/routine.c b/philo_two/src/routine.c index 1066e97..3c04817 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/02 11:59:27 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 12:57:27 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ void *routine_death(t_philo *arg) while (current - arg->time_last_eat < arg->conf->timeout_death) { current = h_time_now(); - usleep(200); + usleep(1000); } event_die(arg); return (NULL); |
