diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-03 16:58:00 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-03 16:58:00 +0100 |
| commit | 7e971b3fa805c40d02ac3e996ab1d181f9584866 (patch) | |
| tree | f904b6f96e0080b86aadd4d9047f7dfb855c3a3d | |
| parent | e874ad94a31a8259b8eb7ad2865767c081bcd279 (diff) | |
| download | philosophers-7e971b3fa805c40d02ac3e996ab1d181f9584866.tar.gz philosophers-7e971b3fa805c40d02ac3e996ab1d181f9584866.tar.bz2 philosophers-7e971b3fa805c40d02ac3e996ab1d181f9584866.zip | |
Added h_sleep instead of usleep for better precision
| -rw-r--r-- | common/common.h | 6 | ||||
| -rw-r--r-- | common/helper.c | 11 | ||||
| -rw-r--r-- | philo_one/src/event.c | 6 | ||||
| -rw-r--r-- | philo_one/src/philo.c | 2 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 12 | ||||
| -rw-r--r-- | philo_three/src/event.c | 6 | ||||
| -rw-r--r-- | philo_two/src/event.c | 6 |
7 files changed, 26 insertions, 23 deletions
diff --git a/common/common.h b/common/common.h index 617db1a..d224264 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:34:25 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,14 +54,14 @@ bool parse_args(t_philo_args *args, int argc, char **argv); long int h_atou_strict(char *s); t_time h_time_now(void); -int h_err(int ret, const char *format, char *str); +void h_sleep(t_time sleep_time); /* ** io.c */ -void philo_put_set_initial_time(void); void philo_put( size_t id, t_philo_event event, t_time initial_time); +int h_err(int ret, const char *format, char *str); #endif diff --git a/common/helper.c b/common/helper.c index e1cc1e2..a9d0652 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:55:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,3 +48,12 @@ t_time h_time_now(void) gettimeofday(&tv, NULL); return (tv.tv_sec * 1000 + tv.tv_usec / 1000); } + +void h_sleep(t_time sleep_time) +{ + t_time start; + + start = h_time_now(); + while (h_time_now() - start < sleep_time) + usleep(500); +} diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 4abfa67..8ceab65 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/03 13:56:42 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:48:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,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); - usleep(arg->conf->timeout_eat * 1000); + h_sleep(arg->conf->timeout_eat); } void event_think(t_philo *arg) @@ -61,7 +61,7 @@ void event_sleep( pthread_mutex_unlock(&arg->conf->mutex_stdout); pthread_mutex_unlock(fork_right); pthread_mutex_unlock(fork_left); - usleep(arg->conf->timeout_sleep * 1000); + h_sleep(arg->conf->timeout_sleep); } void event_die(t_philo *arg) diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c index 1ffb15b..870857b 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/01 13:33:47 by charles ### ########.fr */ +/* Updated: 2021/01/03 16:49:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 70e6694..d8291ef 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/03 12:23:35 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:42:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,15 +59,9 @@ void *routine_philo(t_philo *arg) 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) - { - current = h_time_now(); - usleep(1000); - } + h_time_now() - arg->time_last_eat < arg->conf->timeout_death) + usleep(500); event_die(arg); return (NULL); } diff --git a/philo_three/src/event.c b/philo_three/src/event.c index 10f82c4..185147f 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/03 13:51:20 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:56:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void event_eat(t_philo *philo) sem_wait(philo->sem_stdout); philo_put(philo->id, EVENT_EAT, philo->initial_time); sem_post(philo->sem_stdout); - usleep(philo->conf->timeout_eat * 1000); + h_sleep(philo->conf->timeout_eat); } void event_think(t_philo *philo) @@ -42,7 +42,7 @@ void event_sleep(t_philo *philo) sem_post(philo->sem_stdout); sem_post(philo->forks); sem_post(philo->forks); - usleep(philo->conf->timeout_sleep * 1000); + h_sleep(philo->conf->timeout_sleep); } void event_die(t_philo *philo) diff --git a/philo_two/src/event.c b/philo_two/src/event.c index eead2f5..25fcb0e 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/03 13:55:28 by cacharle ### ########.fr */ +/* Updated: 2021/01/03 16:53:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void event_eat(t_philo *arg) sem_wait(arg->conf->sem_stdout); philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); sem_post(arg->conf->sem_stdout); - usleep(arg->conf->timeout_eat * 1000); + h_sleep(arg->conf->timeout_eat); } void event_think(t_philo *arg) @@ -42,7 +42,7 @@ void event_sleep(t_philo *arg) sem_post(arg->conf->sem_stdout); sem_post(arg->forks); sem_post(arg->forks); - usleep(arg->conf->timeout_sleep * 1000); + h_sleep(arg->conf->timeout_sleep); } void event_die(t_philo *arg) |
