aboutsummaryrefslogtreecommitdiff
path: root/common
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
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')
-rw-r--r--common/inc/common.h5
-rw-r--r--common/src/helper.c30
-rw-r--r--common/src/io.c33
3 files changed, 42 insertions, 26 deletions
diff --git a/common/inc/common.h b/common/inc/common.h
index 6d36e35..56fb284 100644
--- a/common/inc/common.h
+++ b/common/inc/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
-/* Updated: 2021/01/04 13:06:52 by charles ### ########.fr */
+/* Updated: 2021/01/10 10:32:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -56,6 +56,9 @@ bool parse_args(t_philo_args *args, int argc, char **argv);
long int h_atou_strict(char *s);
t_time h_time_now(void);
void h_sleep(t_time sleep_time);
+char *h_nbrcpy(char *dst, long long int num);
+char *h_strcpy_end(char *dst, const char *str);
+const char *philo_sem_eat_name(const char *prefix, long int id);
/*
** io.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 <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);
+}
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 <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}