From eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 10 Jan 2021 10:54:04 +0100 Subject: Fixing bad performance at school by adding more delay to the death checking loop, Added mutex/semaphore to protect against eating and dying at the same time --- common/src/helper.c | 30 +++++++++++++++++++++++++++++- common/src/io.c | 33 +++++++++------------------------ 2 files changed, 38 insertions(+), 25 deletions(-) (limited to 'common/src') diff --git a/common/src/helper.c b/common/src/helper.c index a9d0652..53c24c4 100644 --- a/common/src/helper.c +++ b/common/src/helper.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:55:42 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 10:32:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,3 +57,31 @@ void h_sleep(t_time sleep_time) while (h_time_now() - start < sleep_time) usleep(500); } + +char *h_nbrcpy(char *dst, long long int num) +{ + if (num > 9) + dst = h_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + return (dst + 1); +} + +char *h_strcpy_end(char *dst, const char *str) +{ + while (*str != '\0') + *dst++ = *str++; + return (dst); +} + +#define PHILO_SEM_EAT_BUF_SIZE 2048 + +const char *philo_sem_eat_name(const char *prefix, long int id) +{ + static char buf[PHILO_SEM_EAT_BUF_SIZE]; + char *end; + + buf[0] = '\0'; + end = h_strcpy_end(buf, prefix); + h_nbrcpy(end, id); + return (buf); +} diff --git a/common/src/io.c b/common/src/io.c index 6fc202b..abb26b7 100644 --- a/common/src/io.c +++ b/common/src/io.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/08 21:25:43 by charles ### ########.fr */ +/* Updated: 2021/01/10 10:12:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,21 +22,6 @@ static size_t st_strlen(char *s) return (counter); } -static char *st_nbrcpy(char *dst, long long int num) -{ - if (num > 9) - dst = st_nbrcpy(dst, num / 10); - dst[0] = num % 10 + '0'; - return (dst + 1); -} - -static char *st_strcpy_end(char *dst, char *str) -{ - while (*str != '\0') - *dst++ = *str++; - return (dst); -} - #define PHILO_PUT_BUF_SIZE 20000 static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'}; @@ -44,19 +29,19 @@ static char *g_curr = g_buf; void philo_put(size_t id, t_philo_event event, t_time initial_time) { - g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time); - g_curr = st_strcpy_end(g_curr, " "); - g_curr = st_nbrcpy(g_curr, id); + g_curr = h_nbrcpy(g_curr, h_time_now() - initial_time); + g_curr = h_strcpy_end(g_curr, " "); + g_curr = h_nbrcpy(g_curr, id); if (event == EVENT_FORK) - g_curr = st_strcpy_end(g_curr, " has taken fork\n"); + g_curr = h_strcpy_end(g_curr, " has taken fork\n"); else if (event == EVENT_EAT) - g_curr = st_strcpy_end(g_curr, " is eating\n"); + g_curr = h_strcpy_end(g_curr, " is eating\n"); else if (event == EVENT_SLEEP) - g_curr = st_strcpy_end(g_curr, " is sleeping\n"); + g_curr = h_strcpy_end(g_curr, " is sleeping\n"); else if (event == EVENT_THINK) - g_curr = st_strcpy_end(g_curr, " is thinking\n"); + g_curr = h_strcpy_end(g_curr, " is thinking\n"); else if (event == EVENT_DIE) - g_curr = st_strcpy_end(g_curr, " died\n"); + g_curr = h_strcpy_end(g_curr, " died\n"); if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) philo_put_flush(); } -- cgit