From 67595207143896c1db3b36f734b18a8b16a34fe9 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 25 Apr 2023 14:30:21 +0000 Subject: [PATCH] transition from printf to write --- print.c | 38 ++++++++++++++++++++++++++++---------- utils/putchar.c | 6 ++++++ utils/putnum.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ utils/putstr.c | 14 ++++++++++++++ utils/utils.h | 5 ++++- 5 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 utils/putchar.c create mode 100644 utils/putnum.c create mode 100644 utils/putstr.c diff --git a/print.c b/print.c index f93f491..f151e00 100644 --- a/print.c +++ b/print.c @@ -1,24 +1,30 @@ #include "philo.h" #include #include +#include #include #include "./time.h" -#include "struct.h" +#include "./utils/utils.h" static void print(t_data *data, size_t id, char *str) { size_t time; + bool stop; - pthread_mutex_lock(&data->stop_mutex); - if (data->stop) - { - pthread_mutex_unlock(&data->stop_mutex); - return ; - } - pthread_mutex_unlock(&data->stop_mutex); time = get_time(); pthread_mutex_lock(&data->print_mutex); - printf("%07zu %03zu %s\n", time, id + 1, str); + pthread_mutex_lock(&data->stop_mutex); + stop = data->stop; + pthread_mutex_unlock(&data->stop_mutex); + if (stop == false) + { + ft_putnum(time, 7); + ft_putchar(' '); + ft_putnum(id + 1, 3); + ft_putchar(' '); + ft_putstr(str); + ft_putchar('\n'); + } pthread_mutex_unlock(&data->print_mutex); } @@ -44,5 +50,17 @@ void print_thinking(t_philo *philo) void print_died(t_philo *philo) { - print(philo->data, philo->id, "died"); + t_data *data; + size_t time; + + data = philo->data; + time = get_time(); + pthread_mutex_lock(&data->print_mutex); + ft_putnum(time, 7); + ft_putchar(' '); + ft_putnum(philo->id + 1, 3); + ft_putchar(' '); + ft_putstr("died"); + ft_putchar('\n'); + pthread_mutex_unlock(&data->print_mutex); } diff --git a/utils/putchar.c b/utils/putchar.c new file mode 100644 index 0000000..31d19b5 --- /dev/null +++ b/utils/putchar.c @@ -0,0 +1,6 @@ +#include + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/utils/putnum.c b/utils/putnum.c new file mode 100644 index 0000000..3eed39f --- /dev/null +++ b/utils/putnum.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "utils.h" + +static size_t get_size(size_t num) +{ + size_t nb_digit; + size_t rest; + + nb_digit = 0; + if (num == 0) + nb_digit++; + rest = num; + while (rest != 0) + { + nb_digit++; + rest = rest / 10; + } + return (nb_digit); +} + +static void ft_putdigit(size_t num) +{ + if (num > 9) + { + ft_putdigit(num / 10); + ft_putchar(num % 10 + 48); + } + else + ft_putchar(num + 48); +} + +void ft_putnum(size_t num, size_t padding) +{ + size_t size; + size_t i; + + i = 0; + size = get_size(num); + while (i < padding - size) + { + ft_putchar('0'); + i++; + } + ft_putdigit(num); +} diff --git a/utils/putstr.c b/utils/putstr.c new file mode 100644 index 0000000..83b579d --- /dev/null +++ b/utils/putstr.c @@ -0,0 +1,14 @@ +#include "utils.h" +#include + +void ft_putstr(char *str) +{ + size_t i; + + i = 0; + while (str[i] != 0) + { + ft_putchar(str[i]); + i++; + } +} diff --git a/utils/utils.h b/utils/utils.h index 28d5e05..8cb3cbc 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -6,7 +6,7 @@ /* By: cchauvet