transition from printf to write
This commit is contained in:
parent
531d3146e4
commit
6759520714
38
print.c
38
print.c
@ -1,24 +1,30 @@
|
||||
#include "philo.h"
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#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);
|
||||
}
|
||||
|
6
utils/putchar.c
Normal file
6
utils/putchar.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
47
utils/putnum.c
Normal file
47
utils/putnum.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
14
utils/putstr.c
Normal file
14
utils/putstr.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include "utils.h"
|
||||
#include <stddef.h>
|
||||
|
||||
void ft_putstr(char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
ft_putchar(str[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/11 14:38:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/11 14:46:12 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/25 14:09:53 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,5 +17,8 @@
|
||||
|
||||
bool ft_isnum(char str[]);
|
||||
int ft_atoi(const char str[]);
|
||||
void ft_putchar(char c);
|
||||
void ft_putstr(char str[]);
|
||||
void ft_putnum(size_t num, size_t padding);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user