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> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */
|
/* 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]);
|
conf->nbr_philo = ft_atoi(argv[1]);
|
||||||
if (ft_atoi_check(argv[2]))
|
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]))
|
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]))
|
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]))
|
if (ft_atoi_check(argv[5]))
|
||||||
conf->must_eat = ft_atoi(argv[5]);
|
conf->must_eat = ft_atoi(argv[5]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
/* 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
|
typedef struct s_config
|
||||||
{
|
{
|
||||||
int nbr_philo;
|
int nbr_philo;
|
||||||
int time_die;
|
long time_die;
|
||||||
int time_eat;
|
long time_eat;
|
||||||
int time_sleep;
|
long time_sleep;
|
||||||
int must_eat;
|
long must_eat;
|
||||||
} t_config;
|
} t_config;
|
||||||
|
|
||||||
typedef struct s_same
|
typedef struct s_same
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
/* 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);
|
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()
|
long long get_time()
|
||||||
{
|
{
|
||||||
struct timeval time;
|
struct timeval time;
|
||||||
long long new_time;
|
|
||||||
|
|
||||||
gettimeofday(&time, NULL);
|
gettimeofday(&time, NULL);
|
||||||
new_time = time.tv_sec * 1000000;
|
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||||
new_time += time.tv_usec;
|
|
||||||
return (new_time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void message(int id, char *msg)
|
void message(long long time, int id, char *msg)
|
||||||
{
|
{
|
||||||
char *all;
|
char send[1024];
|
||||||
char *id_str;
|
|
||||||
char *time;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
id_str = ft_itoa(id);
|
i = ft_itoa_bozo(send, (get_utime() - time) / 1000 );
|
||||||
time = ft_itoa(get_time());
|
send[i++] = ' ';
|
||||||
all = ft_calloc(ft_strlen(id_str) + ft_strlen(msg) + ft_strlen(time) + 1, 1);
|
i += ft_itoa_bozo(send + i, id + 1);
|
||||||
i = -1;
|
send[i++] = ' ';
|
||||||
while (time[++i])
|
while (*msg)
|
||||||
all[i] = time[i];
|
send[i++] = *msg++;
|
||||||
while (id_str[++i - ft_strlen(time)])
|
write(1, send, i);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *philosopher(t_philo *philo)
|
void *philosopher(t_philo *philo)
|
||||||
|
@ -57,54 +64,53 @@ void *philosopher(t_philo *philo)
|
||||||
|
|
||||||
id = *philo->only->id;
|
id = *philo->only->id;
|
||||||
next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo;
|
next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo;
|
||||||
usleep((*philo->only->id % 2) * 50);
|
time = get_utime();
|
||||||
time = get_time();
|
|
||||||
last_eat_time = time;
|
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]))
|
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]))
|
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);
|
//message(time, id, "is taking %d fork\n");
|
||||||
printf("%ld %d is eating\n" , get_usec(get_time() - time), id);
|
message(time, id, "is eating\n");
|
||||||
if (get_time() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
|
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 ;
|
break ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
usleep(philo->same->config->time_eat);
|
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[next_id]);
|
||||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||||
printf("%ld %d is sleeping\n" , get_usec(get_time() - time), id);
|
message(time, id, "is sleeping\n");
|
||||||
if (get_time() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
|
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;
|
break ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
usleep(philo->same->config->time_sleep);
|
usleep(philo->same->config->time_sleep);
|
||||||
printf("after sleep last_eat_time: %lld\n" , get_time() - last_eat_time);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
pthread_mutex_unlock(&philo->same->mutex[id]);
|
pthread_mutex_unlock(&philo->same->mutex[id]);
|
||||||
}
|
}
|
||||||
{
|
message(time, id, "is thinking\n");
|
||||||
printf("%lld | %d dieing 3\n",get_time() - last_eat_time, id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!*philo->same->death)
|
if (!*philo->same->death)
|
||||||
{
|
{
|
||||||
*philo->same->death = 1;
|
*philo->same->death = 1;
|
||||||
printf("%ld %d died\n" , get_usec(get_time() - time), id);
|
message(time, id, "died\n");
|
||||||
}
|
}
|
||||||
//else
|
//else
|
||||||
//printf("%ld %d stop\n" , get_usec(time), id);
|
//message(time, id, " stop\n");
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,10 +176,12 @@ t_philo *init_philo(t_same *same, t_only *only)
|
||||||
|
|
||||||
int manage_threads(t_config *config)
|
int manage_threads(t_config *config)
|
||||||
{
|
{
|
||||||
(void)config;
|
/*(void)config;
|
||||||
message(1, "testfuck\n");
|
long long time = get_utime();
|
||||||
return (0);
|
message(time, 0, "frghjuikfgvbjhnkiugtfgv\n");
|
||||||
/*pthread_t *threads;
|
ft_putnbr_fd(get_utime() - time, 1);
|
||||||
|
write(1, "\n", 1);*/
|
||||||
|
pthread_t *threads;
|
||||||
t_same *same;
|
t_same *same;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -199,5 +207,5 @@ int manage_threads(t_config *config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
destroy_mutex(same->mutex, 3);
|
destroy_mutex(same->mutex, 3);
|
||||||
return (0);*/
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,16 @@
|
||||||
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
|
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# #
|
# 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 \
|
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}
|
OBJS = ${SRCS:.c=.o}
|
||||||
CC = clang
|
CC = clang
|
||||||
CFLAGS = -Wall -Wextra -Werror
|
CFLAGS = -g -Wall -Wextra -Werror
|
||||||
NAME = utils.a
|
NAME = utils.a
|
||||||
|
|
||||||
all: ${NAME}
|
all: ${NAME}
|
||||||
|
|
|
@ -6,33 +6,54 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
|
/* 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"
|
#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 *ft_itoa(long long n)
|
||||||
{
|
{
|
||||||
char *str;
|
char *str;
|
||||||
|
int sign;
|
||||||
long long q;
|
long long q;
|
||||||
long long i;
|
long long i;
|
||||||
|
|
||||||
q = 10;
|
sign = 1;
|
||||||
i = 0;
|
if (n < 0)
|
||||||
while (n / q > 0)
|
sign = -1;
|
||||||
{
|
n = abso(n);
|
||||||
q = q * 10;
|
get_size(n, &q, &i);
|
||||||
i++;
|
str = malloc(sizeof(char) * (i + 2));
|
||||||
}
|
|
||||||
str = malloc(sizeof(char) * i + 1);
|
|
||||||
if (!str)
|
if (!str)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i = 0;
|
i = 0;
|
||||||
|
if (sign == -1)
|
||||||
|
str[i++] = '-';
|
||||||
while (q > 1)
|
while (q > 1)
|
||||||
{;
|
{
|
||||||
str[i++] = (n / q % 10) + 48;
|
str[i++] = (n / q % 10) + 48;
|
||||||
q = q / 10;
|
q = q / 10;
|
||||||
}
|
}
|
||||||
|
str[i++] = (n / q % 10) + 48;
|
||||||
|
str[i] = '\0';
|
||||||
return (str);
|
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> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */
|
/* 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_putstr_fd(char *s, int fd);
|
||||||
void ft_putchar_fd(char c, int fd);
|
void ft_putchar_fd(char c, int fd);
|
||||||
char *ft_itoa(long long n);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue