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 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'common/src/helper.c') 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); +} -- cgit