aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-04 12:20:25 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-04 12:20:25 +0100
commit907debbb7d1e7ccc4914805cfe4acbed92b82bcc (patch)
tree199d60e60d9d9b689062c5f08d6381c090127619 /common
parent7e971b3fa805c40d02ac3e996ab1d181f9584866 (diff)
downloadphilosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.tar.gz
philosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.tar.bz2
philosophers-907debbb7d1e7ccc4914805cfe4acbed92b82bcc.zip
Added buffered output, Added waiting for all to be started
Diffstat (limited to 'common')
-rw-r--r--common/common.h3
-rw-r--r--common/io.c44
2 files changed, 28 insertions, 19 deletions
diff --git a/common/common.h b/common/common.h
index d224264..09fdf79 100644
--- a/common/common.h
+++ b/common/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
-/* Updated: 2021/01/03 16:34:25 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 11:52:30 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -62,6 +62,7 @@ void h_sleep(t_time sleep_time);
void philo_put(
size_t id, t_philo_event event, t_time initial_time);
+void philo_put_flush(void);
int h_err(int ret, const char *format, char *str);
#endif
diff --git a/common/io.c b/common/io.c
index 5d50ccc..c01accd 100644
--- a/common/io.c
+++ b/common/io.c
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
-/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */
+/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
static size_t st_strlen(char *s)
{
- int counter;
+ size_t counter;
counter = 0;
while (s[counter])
@@ -27,38 +27,46 @@ static char *st_nbrcpy(char *dst, long long int num)
if (num > 9)
dst = st_nbrcpy(dst, num / 10);
dst[0] = num % 10 + '0';
- dst[1] = '\0';
return (dst + 1);
}
-static void st_strcat(char *dst, char *str)
+static char *st_strcpy_end(char *dst, char *str)
{
- while (*dst != '\0')
- dst++;
while (*str != '\0')
*dst++ = *str++;
- *dst = '\0';
+ return (dst);
}
+#define PHILO_PUT_BUF_SIZE 4048
+
+static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'};
+static char *g_curr = g_buf;
+
void philo_put(size_t id, t_philo_event event, t_time initial_time)
{
- static char buf[2048];
- buf[0] = '\0';
- st_nbrcpy(buf, h_time_now() - initial_time);
- st_strcat(buf, " ");
- st_nbrcpy(buf + st_strlen(buf), id);
+ 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);
if (event == EVENT_FORK)
- st_strcat(buf, " has taken fork\n");
+ g_curr = st_strcpy_end(g_curr, " has taken fork\n");
else if (event == EVENT_EAT)
- st_strcat(buf, " is eating\n");
+ g_curr = st_strcpy_end(g_curr, " is eating\n");
else if (event == EVENT_SLEEP)
- st_strcat(buf, " is sleeping\n");
+ g_curr = st_strcpy_end(g_curr, " is sleeping\n");
else if (event == EVENT_THINK)
- st_strcat(buf, " is thinking\n");
+ g_curr = st_strcpy_end(g_curr, " is thinking\n");
else if (event == EVENT_DIE)
- st_strcat(buf, " died\n");
- write(STDOUT_FILENO, buf, st_strlen(buf));
+ g_curr = st_strcpy_end(g_curr, " died\n");
+ if (g_curr - g_buf > PHILO_PUT_BUF_SIZE)
+ philo_put_flush();
+}
+
+void philo_put_flush(void)
+{
+ write(STDOUT_FILENO, g_buf, g_curr - g_buf);
+ g_curr = g_buf;
+ g_buf[0] = '\0';
}
int h_err(int ret, const char *format, char *str)