From aafec429a489d5d8d9c1c745d3ced88a8bede2ff Mon Sep 17 00:00:00 2001 From: narnaud Date: Mon, 16 May 2022 18:10:36 +0200 Subject: [PATCH] gettime fct --- philo/philo_init.c | 8 ++------ philo/philo_launcher.c | 7 ++----- philo/philo_threads.c | 46 ++++++++---------------------------------- philo/philo_utils.c | 35 ++++++++++++++++++++++++-------- philo/philosophers.h | 11 ++++------ 5 files changed, 43 insertions(+), 64 deletions(-) diff --git a/philo/philo_init.c b/philo/philo_init.c index 4dda75e..c8c7744 100644 --- a/philo/philo_init.c +++ b/philo/philo_init.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/11 10:33:01 by narnaud #+# #+# */ -/* Updated: 2022/04/11 10:35:12 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 18:07:56 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,10 +31,8 @@ t_param *init_param(char **argv) t_room *init_room(char **argv) { t_room *room; - struct timeval time; static int i = 0; - gettimeofday(&time, NULL); room = malloc(sizeof(t_room)); room->param = init_param(argv); room->safe = malloc(sizeof(pthread_mutex_t)); @@ -42,8 +40,6 @@ t_room *init_room(char **argv) room->philos = \ (t_philo **)malloc(room->param->philo_amount * sizeof(t_philo *)); room->running = 1; - room->start_time = time.tv_sec * 1000 + time.tv_usec / 1000; - room->time = 0; room->i = 0; while (i < room->param->philo_amount) { @@ -51,7 +47,7 @@ t_room *init_room(char **argv) room->philos[i]->fork = malloc(sizeof(pthread_mutex_t)); room->philos[i]->safe = malloc(sizeof(pthread_mutex_t)); room->philos[i]->thd = malloc(sizeof(pthread_t)); - room->philos[i]->eat_time = room->time; + room->philos[i]->eat_time = gettime(); room->philos[i]->eat_amount = 0; room->philos[i]->i = i; pthread_mutex_init(room->philos[i]->fork, NULL); diff --git a/philo/philo_launcher.c b/philo/philo_launcher.c index 7c9ea4a..00a8f35 100644 --- a/philo/philo_launcher.c +++ b/philo/philo_launcher.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ -/* Updated: 2022/04/11 14:55:43 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 18:04:42 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,6 @@ int main(int argc, char **argv) if (argc < 5 || argc > 6) return (1); room = init_room(argv + 1); - pthread_create(&room->clock_thd, NULL, room_clock, (void *)room); while (room->i >= 0) { pthread_mutex_lock(room->safe); @@ -57,12 +56,10 @@ int waiter(t_room *room) while (i < param->philo_amount) { pthread_mutex_lock(room->philos[i]->safe); - if (room->philos[i]->eat_time + param->die_time < room->time) + if (room->philos[i]->eat_time + param->die_time < gettime()) { safe_print("%ld %d died\n", room, i); room->running = 0; - pthread_mutex_unlock(room->philos[i]->safe); - pthread_mutex_unlock(room->safe); return (1); } if (room->philos[i]->eat_amount < starvest) diff --git a/philo/philo_threads.c b/philo/philo_threads.c index bbed929..e4267b2 100644 --- a/philo/philo_threads.c +++ b/philo/philo_threads.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ -/* Updated: 2022/04/11 14:52:22 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 17:51:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,60 +25,30 @@ void *philos_life(void *r) pthread_mutex_lock(room->safe); id = room->i; philo = room->philos[id]; - philo->eat_time = room->time; + philo->eat_time = gettime(); id_p = (id + 1) % param->philo_amount; - pthread_mutex_unlock(room->safe); while (room->running) { + pthread_mutex_unlock(room->safe); pthread_mutex_lock(philo->fork); safe_print("%ld %d has taken a fork\n", room, id); pthread_mutex_lock(room->philos[id_p]->fork); safe_print("%ld %d has taken a fork\n", room, id); safe_print("%ld %d is eating\n", room, id); - pthread_mutex_lock(philo->safe); - philo->eat_time = room->time; - pthread_mutex_unlock(philo->safe); - if (check_end(room)) - { - pthread_mutex_unlock(philo->fork); - pthread_mutex_unlock(room->philos[id_p]->fork); + if (!safe_wait(room, gettime(), param->eat_duration * 1000)) return (NULL); - } - usleep(param->eat_duration * 1000); pthread_mutex_unlock(philo->fork); pthread_mutex_unlock(room->philos[id_p]->fork); - if (check_end(room)) - return (NULL); - safe_print("%ld %d is sleeping\n", room, id); pthread_mutex_lock(philo->safe); + philo->eat_time = gettime(); philo->eat_amount++; pthread_mutex_unlock(philo->safe); - usleep(param->sleep_duration * 1000); - if (check_end(room)) + safe_print("%ld %d is sleeping\n", room, id); + if (!safe_wait(room, gettime(), param->sleep_duration * 1000)) return (NULL); safe_print("%ld %d is thinking\n", room, id); - } - return (NULL); -} - -void *room_clock(void *r) -{ - t_room *room; - struct timeval time; - - room = (t_room *)r; - while (1) - { - gettimeofday(&time, NULL); pthread_mutex_lock(room->safe); - room->time = time.tv_sec * 1000 + time.tv_usec / 1000 - room->start_time; - if (!room->running) - { - pthread_mutex_unlock(room->safe); - return(NULL); - } - pthread_mutex_unlock(room->safe); - usleep(CLOCK_TWO); } + pthread_mutex_unlock(room->safe); return (NULL); } diff --git a/philo/philo_utils.c b/philo/philo_utils.c index 549780d..3d1a83d 100644 --- a/philo/philo_utils.c +++ b/philo/philo_utils.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/11 10:29:50 by narnaud #+# #+# */ -/* Updated: 2022/04/11 14:50:59 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 18:03:12 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,24 +26,43 @@ int mini_atoi(char *nbr) return (ret); } +size_t gettime(void) +{ + static struct timeval start = {0,0}; + struct timeval actualtime; + size_t ret; + + if(start.tv_sec == 0 && start.tv_usec == 0) + gettimeofday(&start, NULL); + gettimeofday(&actualtime, NULL); + ret = (size_t)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\ + - (start.tv_sec * 1000 + start.tv_usec / 1000)); + return (ret); +} + void safe_print(char *str, t_room *room, int id) { pthread_mutex_lock(room->safe); - printf(str, room->time, id); + printf(str, gettime(), id); pthread_mutex_unlock(room->safe); } -int check_end(t_room *room) +int safe_wait(t_room *room, size_t start, size_t duration) { int ret; - ret = 0; - pthread_mutex_lock(room->safe); - if (!room->running) - ret = 1; - pthread_mutex_unlock(room->safe); + ret = 1; + while (ret && (gettime() < start + duration)) + { + pthread_mutex_lock(room->safe); + if (!room->running) + ret = 0; + pthread_mutex_unlock(room->safe); + usleep(5000); + } return (ret); } + int clean_memory(t_room *room) { t_param *param; diff --git a/philo/philosophers.h b/philo/philosophers.h index a014c4c..127e4d1 100644 --- a/philo/philosophers.h +++ b/philo/philosophers.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/24 15:44:06 by narnaud #+# #+# */ -/* Updated: 2022/05/16 14:09:56 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 18:06:07 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ typedef struct s_philo pthread_mutex_t *fork; pthread_mutex_t *safe; pthread_t *thd; - unsigned long eat_time; + size_t eat_time; int eat_amount; int i; } t_philo; @@ -46,16 +46,12 @@ typedef struct s_room t_param *param; t_philo **philos; pthread_mutex_t *safe; - pthread_t clock_thd; int running; - unsigned long start_time; - unsigned long time; int i; } t_room; //philo_threads.c void *philos_life(void *r); -void *room_clock(void *r); //philo_init.c t_param *init_param(char **argv); @@ -65,6 +61,7 @@ t_room *init_room(char **argv); int mini_atoi(char *nbr); void safe_print(char *str, t_room *room, int id); int clean_memory(t_room *room); -int check_end(t_room *room); +int safe_wait(t_room *room, size_t start, size_t duration); +size_t gettime(void); #endif