Avancement, crash message a corriger
This commit is contained in:
parent
41c7d2d517
commit
4ffaf6a61f
BIN
mandatory/.threads.c.swp
Normal file
BIN
mandatory/.threads.c.swp
Normal file
Binary file not shown.
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
long long *time;
|
||||
long long time;
|
||||
long long last_eat_time;
|
||||
int id;
|
||||
int next_id;
|
||||
|
||||
time = philo->same->time;
|
||||
id = *philo->only->id;
|
||||
next_id = (*philo->only->id + 1) % philo->same->config->nbr_philo;
|
||||
while (*time <= 0)
|
||||
;
|
||||
usleep((*philo->only->id % 2));
|
||||
last_eat_time = *time;
|
||||
while (1)
|
||||
usleep((*philo->only->id % 2) * 50);
|
||||
time = get_time();
|
||||
last_eat_time = time;
|
||||
while (get_time() - last_eat_time < philo->same->config->time_die && !*philo->same->death)
|
||||
{
|
||||
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]))
|
||||
{
|
||||
// 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]))
|
||||
{
|
||||
//printf("%ld %d is taking %d fork\n" , get_usec(*time), id, next_id);
|
||||
printf("%ld %d is eating\n" , get_usec(*time), id);
|
||||
last_eat_time = *time;
|
||||
if (*time - last_eat_time + philo->same->config->time_eat >= philo->same->config->time_die || *philo->same->death)
|
||||
//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)
|
||||
{
|
||||
printf("%lld | %d dieing 1\n",*time - last_eat_time, id);
|
||||
break;
|
||||
printf("%lld | %d dieing 1\n", get_time() - last_eat_time, id);
|
||||
break ;
|
||||
}
|
||||
else
|
||||
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[id]);
|
||||
printf("%ld %d is sleeping\n" , get_usec(*time), id);
|
||||
if (*time - last_eat_time + philo->same->config->time_sleep >= philo->same->config->time_die || *philo->same->death)
|
||||
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)
|
||||
{
|
||||
printf("%lld | %d dieing 2\n",*time - last_eat_time, id);
|
||||
printf("%lld | %d dieing 2\n",get_time() - last_eat_time, id);
|
||||
break;
|
||||
}
|
||||
else
|
||||
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
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (!*philo->same->death)
|
||||
{
|
||||
*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
|
||||
//printf("%ld %d stop\n" , get_usec(*time), id);
|
||||
//printf("%ld %d stop\n" , get_usec(time), id);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
@ -100,25 +126,6 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
|||
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 *same;
|
||||
|
@ -170,8 +177,7 @@ int manage_threads(t_config *config)
|
|||
threads = ft_calloc(config->nbr_philo + 1, sizeof(pthread_t));
|
||||
if (threads == NULL)
|
||||
return (1);
|
||||
same = create_struct_same(config);
|
||||
pthread_create(&threads[0], NULL, (void *)&timer, init_philo(same, create_struct_only(-1)));
|
||||
same = create_struct_same(config);
|
||||
i = -1;
|
||||
while (++i < config->nbr_philo)
|
||||
{
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
# **************************************************************************** #
|
||||
|
||||
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}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
|
29
utils/ft_itoa.c
Normal file
29
utils/ft_itoa.c
Normal 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);
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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_putstr_fd(char *s, int fd);
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
char *ft_itoa(long long n);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue