Avancement presque fini, norme + leak + must_eat
This commit is contained in:
parent
3a9d944f74
commit
5c36ef01cd
BIN
mandatory/.threads.c.swo
Normal file
BIN
mandatory/.threads.c.swo
Normal file
Binary file not shown.
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/12 16:53:19 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/27 16:59:25 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -19,13 +19,13 @@ int parsing(char *argv[], t_config *conf)
|
|||
conf->nbr_philo = ft_atoi(argv[1]);
|
||||
if (ft_atoi_check(argv[2]))
|
||||
{
|
||||
conf->time_die = ft_atoi(argv[2]);
|
||||
conf->time_die = ft_atoi(argv[2]) * 1000;
|
||||
if (ft_atoi_check(argv[3]))
|
||||
{
|
||||
conf->time_eat = ft_atoi(argv[3]);
|
||||
conf->time_eat = ft_atoi(argv[3]) * 1000;
|
||||
if (ft_atoi_check(argv[4]))
|
||||
{
|
||||
conf->time_sleep = ft_atoi(argv[4]);
|
||||
conf->time_sleep = ft_atoi(argv[4]) * 1000;
|
||||
if (ft_atoi_check(argv[5]))
|
||||
conf->must_eat = ft_atoi(argv[5]);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/13 14:01:59 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/27 16:55:06 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -25,10 +25,10 @@
|
|||
typedef struct s_config
|
||||
{
|
||||
int nbr_philo;
|
||||
int time_die;
|
||||
int time_eat;
|
||||
int time_sleep;
|
||||
int must_eat;
|
||||
long time_die;
|
||||
long time_eat;
|
||||
long time_sleep;
|
||||
long must_eat;
|
||||
} t_config;
|
||||
|
||||
typedef struct s_same
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/25 00:17:08 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/28 00:42:38 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,35 +17,42 @@ long get_usec(long long time)
|
|||
return (time % 1000000);
|
||||
}
|
||||
|
||||
long long get_utime()
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_sec * 1000000 + time.tv_usec);
|
||||
}
|
||||
|
||||
long long get_mstime()
|
||||
{
|
||||
struct timeval time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
return (time.tv_usec / 1000);
|
||||
}
|
||||
|
||||
long long get_time()
|
||||
{
|
||||
struct timeval time;
|
||||
long long new_time;
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
new_time = time.tv_sec * 1000000;
|
||||
new_time += time.tv_usec;
|
||||
return (new_time);
|
||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||
}
|
||||
|
||||
void message(int id, char *msg)
|
||||
void message(long long time, int id, char *msg)
|
||||
{
|
||||
char *all;
|
||||
char *id_str;
|
||||
char *time;
|
||||
char send[1024];
|
||||
int i;
|
||||
|
||||
id_str = ft_itoa(id);
|
||||
time = ft_itoa(get_time());
|
||||
all = ft_calloc(ft_strlen(id_str) + ft_strlen(msg) + ft_strlen(time) + 1, 1);
|
||||
i = -1;
|
||||
while (time[++i])
|
||||
all[i] = time[i];
|
||||
while (id_str[++i - ft_strlen(time)])
|
||||
all[i] = id_str[i - ft_strlen(time)];
|
||||
while (msg[++i - ft_strlen(time) - ft_strlen(id_str)])
|
||||
all[i] = msg[i - ft_strlen(time) - ft_strlen(id_str)];
|
||||
write(1, all, ft_strlen(all));
|
||||
i = ft_itoa_bozo(send, (get_utime() - time) / 1000 );
|
||||
send[i++] = ' ';
|
||||
i += ft_itoa_bozo(send + i, id + 1);
|
||||
send[i++] = ' ';
|
||||
while (*msg)
|
||||
send[i++] = *msg++;
|
||||
write(1, send, i);
|
||||
}
|
||||
|
||||
void *philosopher(t_philo *philo)
|
||||
|
@ -57,54 +64,53 @@ void *philosopher(t_philo *philo)
|
|||
|
||||
id = *philo->only->id;
|
||||
next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo;
|
||||
usleep((*philo->only->id % 2) * 50);
|
||||
time = get_time();
|
||||
time = get_utime();
|
||||
last_eat_time = time;
|
||||
while (get_time() - last_eat_time < philo->same->config->time_die && !*philo->same->death)
|
||||
if (id % 2 == 1 || (philo->same->config->nbr_philo % 2 == 1 && id == philo->same->config->nbr_philo - 1))
|
||||
{
|
||||
message(time, id, "is thinking\n");
|
||||
usleep(philo->same->config->time_eat);
|
||||
}
|
||||
while (get_utime() - last_eat_time < philo->same->config->time_die && !*philo->same->death)
|
||||
{
|
||||
message (id, "is thinking\n");
|
||||
if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[id]))
|
||||
{
|
||||
// printf("%ld %d is taking %d fork\n" , get_usec(time), id, id);
|
||||
// message(time, id, "is taking %d fork\n");
|
||||
if (!*philo->same->death && !pthread_mutex_lock(&philo->same->mutex[next_id]))
|
||||
{
|
||||
//printf("%ld %d is taking %d fork\n" , get_usec(time), id, next_id);
|
||||
printf("%ld %d is eating\n" , get_usec(get_time() - time), id);
|
||||
if (get_time() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
|
||||
//message(time, id, "is taking %d fork\n");
|
||||
message(time, id, "is eating\n");
|
||||
if (get_utime() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
|
||||
{
|
||||
printf("%lld | %d dieing 1\n", get_time() - last_eat_time, id);
|
||||
message(time, id, "dieing 1\n");
|
||||
break ;
|
||||
}
|
||||
else
|
||||
usleep(philo->same->config->time_eat);
|
||||
last_eat_time = get_time();
|
||||
last_eat_time = get_utime();
|
||||
pthread_mutex_unlock(&philo->same->mutex[next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||
printf("%ld %d is sleeping\n" , get_usec(get_time() - time), id);
|
||||
if (get_time() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
|
||||
message(time, id, "is sleeping\n");
|
||||
if (get_utime() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
|
||||
{
|
||||
printf("%lld | %d dieing 2\n",get_time() - last_eat_time, id);
|
||||
message(time, id, "dieing 2\n");
|
||||
break ;
|
||||
}
|
||||
else
|
||||
usleep(philo->same->config->time_sleep);
|
||||
printf("after sleep last_eat_time: %lld\n" , get_time() - last_eat_time);
|
||||
}
|
||||
else
|
||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||
}
|
||||
{
|
||||
printf("%lld | %d dieing 3\n",get_time() - last_eat_time, id);
|
||||
break;
|
||||
}
|
||||
message(time, id, "is thinking\n");
|
||||
}
|
||||
if (!*philo->same->death)
|
||||
{
|
||||
*philo->same->death = 1;
|
||||
printf("%ld %d died\n" , get_usec(get_time() - time), id);
|
||||
message(time, id, "died\n");
|
||||
}
|
||||
//else
|
||||
//printf("%ld %d stop\n" , get_usec(time), id);
|
||||
//message(time, id, " stop\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
@ -170,10 +176,12 @@ t_philo *init_philo(t_same *same, t_only *only)
|
|||
|
||||
int manage_threads(t_config *config)
|
||||
{
|
||||
(void)config;
|
||||
message(1, "testfuck\n");
|
||||
return (0);
|
||||
/*pthread_t *threads;
|
||||
/*(void)config;
|
||||
long long time = get_utime();
|
||||
message(time, 0, "frghjuikfgvbjhnkiugtfgv\n");
|
||||
ft_putnbr_fd(get_utime() - time, 1);
|
||||
write(1, "\n", 1);*/
|
||||
pthread_t *threads;
|
||||
t_same *same;
|
||||
int i;
|
||||
|
||||
|
@ -199,5 +207,5 @@ int manage_threads(t_config *config)
|
|||
}
|
||||
}
|
||||
destroy_mutex(same->mutex, 3);
|
||||
return (0);*/
|
||||
return (0);
|
||||
}
|
||||
|
|
|
@ -6,15 +6,16 @@
|
|||
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# #
|
||||
# Updated: 2023/03/13 14:03:30 by erey-bet ### ########.fr #
|
||||
# Updated: 2023/03/27 02:53:45 by erey-bet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = ft_atoi.c ft_calloc.c ft_putstr_fd.c ft_putnbr_fd.c ft_atoi_check.c \
|
||||
ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c ft_itoa.c
|
||||
ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c ft_itoa.c \
|
||||
ft_strcat.c ft_strcpy.c ft_itoa_bozo.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
CFLAGS = -g -Wall -Wextra -Werror
|
||||
NAME = utils.a
|
||||
|
||||
all: ${NAME}
|
||||
|
|
|
@ -6,33 +6,54 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/24 16:35:39 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/26 20:17:58 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
static long long abso(long long n)
|
||||
{
|
||||
if (n < 0)
|
||||
return (n * -1);
|
||||
return (n);
|
||||
}
|
||||
|
||||
static void get_size(long long n, long long *q, long long *i)
|
||||
{
|
||||
*q = 1;
|
||||
*i = 1;
|
||||
while (n / *q > 9)
|
||||
{
|
||||
*q = *q * 10;
|
||||
(*i)++;
|
||||
}
|
||||
}
|
||||
|
||||
char *ft_itoa(long long n)
|
||||
{
|
||||
char *str;
|
||||
int sign;
|
||||
long long q;
|
||||
long long i;
|
||||
|
||||
q = 10;
|
||||
i = 0;
|
||||
while (n / q > 0)
|
||||
{
|
||||
q = q * 10;
|
||||
i++;
|
||||
}
|
||||
str = malloc(sizeof(char) * i + 1);
|
||||
sign = 1;
|
||||
if (n < 0)
|
||||
sign = -1;
|
||||
n = abso(n);
|
||||
get_size(n, &q, &i);
|
||||
str = malloc(sizeof(char) * (i + 2));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (sign == -1)
|
||||
str[i++] = '-';
|
||||
while (q > 1)
|
||||
{;
|
||||
{
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
q = q / 10;
|
||||
}
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
|
38
utils/ft_itoa_bozo.c
Normal file
38
utils/ft_itoa_bozo.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa_bozo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 15:48:37 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
int ft_itoa_bozo(char *str, long long n)
|
||||
{
|
||||
long long v;
|
||||
long long q;
|
||||
long long i;
|
||||
|
||||
i = 0;
|
||||
v = n;
|
||||
if (n < 0)
|
||||
{
|
||||
str[i++] = '-';
|
||||
v *= -1;
|
||||
}
|
||||
q = 1;
|
||||
while (n / q > 9)
|
||||
q *= 10;
|
||||
while (q > 0)
|
||||
{
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
q /= 10;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (i);
|
||||
}
|
23
utils/ft_strcat.c
Normal file
23
utils/ft_strcat.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/26 23:26:04 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/27 00:04:05 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
void ft_strcat(char *dest, const char *src)
|
||||
{
|
||||
int len_dest;
|
||||
|
||||
if (!dest || !src)
|
||||
return ;
|
||||
len_dest = ft_strlen(dest);
|
||||
ft_strcpy(dest + len_dest, src);
|
||||
}
|
28
utils/ft_strcpy.c
Normal file
28
utils/ft_strcpy.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/17 19:03:49 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/26 23:56:14 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
void ft_strcpy(char *dest, const char *src)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!dest || !src)
|
||||
return ;
|
||||
i = 0;
|
||||
while (src[i] != '\0')
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/24 16:26:31 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/27 04:06:39 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -26,5 +26,8 @@ void ft_putnbr_fd(int n, int fd);
|
|||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
char *ft_itoa(long long n);
|
||||
void ft_strcpy(char *dest, const char *src);
|
||||
void ft_strcat(char *dest, const char *src);
|
||||
int ft_itoa_bozo(char *str, long long n);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue