aboutsummaryrefslogtreecommitdiff
path: root/common/src/helper.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-10 10:54:04 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-10 10:54:04 +0100
commiteb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb (patch)
treeefff80bbbc2157979eaae006f6835d020062dba9 /common/src/helper.c
parent802ea8347d1ceb7a02cf279359b3b101106fab94 (diff)
downloadphilosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.tar.gz
philosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.tar.bz2
philosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.zip
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
Diffstat (limited to 'common/src/helper.c')
-rw-r--r--common/src/helper.c30
1 files changed, 29 insertions, 1 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
+}