Avancement, crash message a corriger

This commit is contained in:
Etienne Rey-bethbeder 2023-03-24 16:41:34 +01:00
parent 41c7d2d517
commit 4ffaf6a61f
5 changed files with 84 additions and 48 deletions

BIN
mandatory/.threads.c.swp Normal file

Binary file not shown.

View file

@ -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/13 18:33:50 by erey-bet ### ########.fr */ /* Updated: 2023/03/24 16:38:39 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,68 +17,94 @@ long get_usec(long long time)
return (time % 1000000); return (time % 1000000);
} }
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);
}
void message(int id, char *msg)
{
char *all;
char *id_str;
char *time;
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] = id_str[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));
}
void *philosopher(t_philo *philo) void *philosopher(t_philo *philo)
{ {
long long *time; long long time;
long long last_eat_time; long long last_eat_time;
int id; int id;
int next_id; int next_id;
time = philo->same->time;
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;
while (*time <= 0) usleep((*philo->only->id % 2) * 50);
; time = get_time();
usleep((*philo->only->id % 2)); last_eat_time = time;
last_eat_time = *time; while (get_time() - last_eat_time < philo->same->config->time_die && !*philo->same->death)
while (1)
{ {
printf("%ld %d is thinking\n" , get_usec(*time), id); 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); // printf("%ld %d is taking %d fork\n" , get_usec(time), id, id);
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); //printf("%ld %d is taking %d fork\n" , get_usec(time), id, next_id);
printf("%ld %d is eating\n" , get_usec(*time), id); printf("%ld %d is eating\n" , get_usec(get_time() - time), id);
last_eat_time = *time; if (get_time() - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
if (*time - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
{ {
printf("%lld | %d dieing 1\n",*time - last_eat_time, id); printf("%lld | %d dieing 1\n", get_time() - last_eat_time, id);
break; break ;
} }
else else
usleep(philo->same->config->time_eat); usleep(philo->same->config->time_eat);
printf("after eat last_eat_time: %lld\n" , *time - last_eat_time); last_eat_time = get_time();
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(*time), id); printf("%ld %d is sleeping\n" , get_usec(get_time() - time), id);
if (*time - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death) if (get_time() - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
{ {
printf("%lld | %d dieing 2\n",*time - last_eat_time, id); printf("%lld | %d dieing 2\n",get_time() - last_eat_time, id);
break; break;
} }
else else
usleep(philo->same->config->time_sleep); usleep(philo->same->config->time_sleep);
printf("after sleep last_eat_time: %lld\n" , *time - last_eat_time); 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]);
} }
if (*time - last_eat_time >= philo->same->config->time_die || *philo->same->death)
{ {
printf("%lld | %d dieing 3\n",*time - last_eat_time, id); printf("%lld | %d dieing 3\n",get_time() - last_eat_time, id);
break; break;
} }
} }
if (!*philo->same->death) if (!*philo->same->death)
{ {
*philo->same->death = 1; *philo->same->death = 1;
printf("%ld %d died\n" , get_usec(*time), id); printf("%ld %d died\n" , get_usec(get_time() - time), id);
} }
//else //else
//printf("%ld %d stop\n" , get_usec(*time), id); //printf("%ld %d stop\n" , get_usec(time), id);
return (NULL); return (NULL);
} }
@ -100,25 +126,6 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr)
pthread_mutex_destroy(&mutex[i]); pthread_mutex_destroy(&mutex[i]);
} }
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);
}
void *timer(t_philo *timer)
{
*timer->same->time = get_time();
while (!*timer->same->death)
*timer->same->time = get_time();
return (NULL);
}
t_same *create_struct_same(t_config *config) t_same *create_struct_same(t_config *config)
{ {
t_same *same; t_same *same;
@ -170,8 +177,7 @@ int manage_threads(t_config *config)
threads = ft_calloc(config->nbr_philo + 1, sizeof(pthread_t)); threads = ft_calloc(config->nbr_philo + 1, sizeof(pthread_t));
if (threads == NULL) if (threads == NULL)
return (1); return (1);
same = create_struct_same(config); same = create_struct_same(config);
pthread_create(&threads[0], NULL, (void *)&timer, init_philo(same, create_struct_only(-1)));
i = -1; i = -1;
while (++i < config->nbr_philo) while (++i < config->nbr_philo)
{ {

View file

@ -11,7 +11,7 @@
# **************************************************************************** # # **************************************************************************** #
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_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c ft_itoa.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra -Werror

29
utils/ft_itoa.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "utils.h"
char *ft_itoa(long long n)
{
char *str;
long long q;
long long i;
q = 10;
while (n / q > 0)
q = q * 10;
str = malloc(sizeof(char) * q + 1);
i = 0;
while (q > 0)
str[i++] = (n / q % 10) + 48;
return (str);
}

View file

@ -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/12 16:49:53 by erey-bet ### ########.fr */ /* Updated: 2023/03/24 16:26:31 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,5 +25,6 @@ void *ft_memset(void *ptr, int v, size_t count);
void ft_putnbr_fd(int n, int fd); 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);
#endif #endif