Compare commits

...

10 commits

Author SHA1 Message Date
Etienne Rey-bethbeder bbbd622259 _ 2023-04-11 20:04:49 +02:00
Etienne Rey-bethbeder 140889a1fd _ 2023-04-10 17:39:30 +02:00
Etienne Rey-bethbeder 29832e45c3 _ 2023-04-09 22:41:29 +02:00
Xamora 30c70d01da _ 2023-04-09 18:03:27 +02:00
Etienne Rey-bethbeder c30de10070 _ 2023-04-08 19:41:07 +02:00
Etienne Rey-bethbeder 61035874a8 Suppression include inutile 2023-04-08 16:41:27 +02:00
Etienne Rey-bethbeder d1200f346b Correction et sécurisation du code en cas de crash et d'erreur de malloc 2023-04-08 16:15:26 +02:00
Xamora c2f741e949 Correction mutex ecriture 2023-04-07 11:31:48 +02:00
Xamora af6b1bacf3 Correction mutex ecriture 2023-04-07 11:31:40 +02:00
Etienne Rey-bethbeder 3f0ec62059 corrigé mixed up 2023-04-06 14:02:19 +02:00
26 changed files with 218 additions and 95 deletions

View file

@ -1,7 +1,5 @@
SRCS = mandatory/main.c mandatory/parsing.c mandatory/threads.c mandatory/manage_threads.c mandatory/utils_threads.c mandatory/utils_manage_threads.c SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c utils_threads3.c
SRCS_BONUS =
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
UTILS = utils/utils.a UTILS = utils/utils.a
CC = clang CC = clang
CFLAGS = -g -Wall -Wextra -Werror -pthread CFLAGS = -g -Wall -Wextra -Werror -pthread
@ -13,10 +11,6 @@ ${NAME}: ${OBJS}
make -C utils make -C utils
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS} ${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS}
bonus: ${OBJS_BONUS}
make -C utils
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${UTILS}
clean: clean:
rm -f ${OBJS} rm -f ${OBJS}
rm -f ${OBJS_BONUS} rm -f ${OBJS_BONUS}
@ -138,6 +132,6 @@ coffee:
bozo : bozo :
@wget -q -O bozo.gif https://i.kym-cdn.com/photos/images/newsfeed/002/322/200/e51.gif @wget -q -O bozo.gif https://i.kym-cdn.com/photos/images/newsfeed/002/322/200/e51.gif
@xdg-open bozo.gif @xdg-open bozo.gif
@@sleep 4.26 @@sleep 2.13
@pkill eog @pkill eog
@rm bozo.gif @rm bozo.gif

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */ /* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */
/* Updated: 2023/03/30 00:07:31 by erey-bet ### ########.fr */ /* Updated: 2023/04/07 14:41:17 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,9 +16,9 @@ int main(int argc, char *argv[])
{ {
t_config config; t_config config;
if (argc < 5) if (argc < 5 || argc > 6)
{ {
write(2, "Not enought argument\n", 21); write(2, "Wrong number of argument\n", 25);
return (1); return (1);
} }
if (parsing(argv, &config)) if (parsing(argv, &config))

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */ /* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */
/* Updated: 2023/03/31 17:52:22 by erey-bet ### ########.fr */ /* Updated: 2023/04/10 15:55:42 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,19 +19,18 @@ static t_same *create_struct_same(t_config *config)
same = ft_calloc(sizeof(t_same), 1); same = ft_calloc(sizeof(t_same), 1);
if (!same) if (!same)
return (NULL); return (NULL);
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 2); same->forks = init_forks(config);
if (!same->forks)
return (same);
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 3);
if (!same->mutex) if (!same->mutex)
return (NULL); return (same);
init_mutex(same->mutex, config->nbr_philo + 2); if (init_mutex(same->mutex, config->nbr_philo + 3))
return (same);
same->config = config; same->config = config;
same->death = ft_calloc(sizeof(int), 1); same->death = 0;
if (!same->death) same->all_eat = 0;
return (NULL); same->time = get_utime();
*same->death = 0;
same->all_eat = ft_calloc(sizeof(int), 1);
if (!same->all_eat)
return (NULL);
*same->all_eat = 0;
return (same); return (same);
} }
@ -51,15 +50,22 @@ static t_philo *init_philo(t_same *same, t_only *only)
{ {
t_philo *philo; t_philo *philo;
if (!same)
{
if (only)
free(only);
return (NULL);
}
if (!only)
{
free(same);
return (NULL);
}
philo = ft_calloc(sizeof(t_philo), 1); philo = ft_calloc(sizeof(t_philo), 1);
if (!philo) if (!philo)
return (NULL); return (NULL);
philo->same = same; philo->same = same;
if (!philo->same)
return (NULL);
philo->only = only; philo->only = only;
if (!philo->only)
return (NULL);
return (philo); return (philo);
} }
@ -75,7 +81,7 @@ int manage_threads(t_config *config)
return (1); return (1);
philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *)); philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *));
if (philo == NULL) if (philo == NULL)
return (1); return (free_all(thds, NULL, NULL));
same = create_struct_same(config); same = create_struct_same(config);
i = -1; i = -1;
while (++i < config->nbr_philo) while (++i < config->nbr_philo)
@ -83,7 +89,7 @@ int manage_threads(t_config *config)
philo[i] = init_philo(same, create_struct_only(i)); philo[i] = init_philo(same, create_struct_only(i));
if (!philo[i] || pthread_create(&thds[i], NULL, if (!philo[i] || pthread_create(&thds[i], NULL,
(void *)&philosopher, philo[i]) != 0) (void *)&philosopher, philo[i]) != 0)
return (1); return (free_all(thds, same, philo));
} }
philo[i] = NULL; philo[i] = NULL;
while (--i > -1) while (--i > -1)

View file

@ -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/31 15:13:30 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 18:20:49 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,9 @@ int get_argv(char **argv, long *conf, char *message, int multiplier)
if (ft_atoi_check(*argv)) if (ft_atoi_check(*argv))
{ {
*conf = (long)ft_atoi(*argv) * multiplier; *conf = (long)ft_atoi(*argv) * multiplier;
if (*conf < 1) if (*conf < 0)
return (error("Wrong number of ", message));
else if (*conf < 1 && multiplier == 1)
return (error("Wrong number of ", message)); return (error("Wrong number of ", message));
return (0); return (0);
} }

View file

@ -6,21 +6,17 @@
/* 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/04/01 11:58:47 by erey-bet ### ########.fr */ /* Updated: 2023/04/10 11:21:38 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PHILO_H #ifndef PHILO_H
# define PHILO_H # define PHILO_H
# include "../utils/utils.h" # include "utils/utils.h"
# include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <pthread.h>
# include <sys/wait.h>
# include <sys/time.h> # include <sys/time.h>
# include <pthread.h> # include <pthread.h>
# include <stdio.h>
typedef struct s_config typedef struct s_config
{ {
@ -34,17 +30,17 @@ typedef struct s_config
typedef struct s_same typedef struct s_same
{ {
t_config *config; t_config *config;
int *forks;
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
int *all_eat; int all_eat;
int *death; int death;
long long time;
} t_same; } t_same;
typedef struct s_only typedef struct s_only
{ {
int id; int id;
int next_id; int next_id;
long long time;
long long last_eat_time; long long last_eat_time;
int eat; int eat;
} t_only; } t_only;
@ -68,9 +64,19 @@ int verif_finish(t_philo *philo, t_only *only);
int finish(t_philo *philo, long long last_eat_time); int finish(t_philo *philo, long long last_eat_time);
void message_die(t_philo *philo, char *msg); void message_die(t_philo *philo, char *msg);
void message(t_philo *philo, char *msg); void message(t_philo *philo, char *msg);
void init_mutex(pthread_mutex_t *mutex, int nbr); int init_mutex(pthread_mutex_t *mutex, int nbr);
void destroy_mutex(pthread_mutex_t *mutex, int nbr); void destroy_mutex(pthread_mutex_t *mutex, int nbr);
void free_all(pthread_t *threads, t_same *same, t_philo **philo); int free_all(pthread_t *threads, t_same *same, t_philo **philo);
int get_fork(t_philo *philo, int id_fork);
void take_fork(t_philo *philo, int id_fork);
void drop_fork(t_philo *philo, int id_fork, int next_id_fork);
void ft_sleep(long long time_to_sleep);
int *init_forks(t_config *config);
int before_eat(t_philo *philo, t_only *only,
t_config *config, int nbr_philo);
int in_loop_eat_sleep(t_philo *philo, t_only *only,
t_config *config, int nbr_philo);
void wait_fork(t_philo *philo, t_only *only, int id);
/* THREADS */ /* THREADS */
void *philosopher(t_philo *philo); void *philosopher(t_philo *philo);

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/04/01 11:57:59 by erey-bet ### ########.fr */ /* Updated: 2023/04/10 17:22:22 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,11 +22,11 @@ int finish(t_philo *philo, long long last_eat_time)
if (philo->same->config->must_eat > -1) if (philo->same->config->must_eat > -1)
{ {
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
all_eat = *philo->same->all_eat; all_eat = philo->same->all_eat;
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
} }
pthread_mutex_lock(&philo->same->mutex[nbr_philo]); pthread_mutex_lock(&philo->same->mutex[nbr_philo]);
dead = *philo->same->death; dead = philo->same->death;
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
return (get_utime() - last_eat_time >= philo->same->config->time_die return (get_utime() - last_eat_time >= philo->same->config->time_die
|| dead || dead
@ -40,47 +40,47 @@ void init_philo(t_philo *philo, t_only *only,
{ {
*nbr_philo = philo->same->config->nbr_philo; *nbr_philo = philo->same->config->nbr_philo;
only->next_id = (only->id + 1) % *nbr_philo; only->next_id = (only->id + 1) % *nbr_philo;
only->time = get_utime(); only->last_eat_time = get_utime();
only->last_eat_time = only->time; if (only->id == only->next_id)
return ;
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1 if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
&& only->id == *nbr_philo - 1)) && only->id == *nbr_philo - 1))
{ {
message(philo, "is thinking\n"); message(philo, "is thinking\n");
if (get_utime() - only->last_eat_time + config->time_eat if (get_utime() - only->last_eat_time + config->time_eat
>= config->time_die) >= config->time_die)
usleep(config->time_die - (get_utime() - only->last_eat_time)); ft_sleep(config->time_die - (get_utime() - only->last_eat_time));
else else
usleep(config->time_eat / (only->id % 2 + 1)); ft_sleep(config->time_eat / (only->id % 2 + 1));
} }
} }
int in_loop_eat_sleep(t_philo *philo, t_only *only, int in_loop_eat_sleep(t_philo *philo, t_only *only,
t_config *config, int nbr_philo) t_config *config, int nbr_philo)
{ {
only->last_eat_time = get_utime();
if (!verif_die_eating(philo, only, config)) if (!verif_die_eating(philo, only, config))
return (0); return (0);
usleep(config->time_eat); ft_sleep(config->time_eat);
only->last_eat_time = get_utime();
if (verif_finish(philo, only)) if (verif_finish(philo, only))
return (0); return (0);
if (config->must_eat > -1 && only->eat < config->must_eat) if (config->must_eat > -1 && only->eat < config->must_eat)
{ {
only->eat++; only->eat++;
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
*philo->same->all_eat += 1; philo->same->all_eat += 1;
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
} }
message(philo, "is sleeping\n"); message(philo, "is sleeping\n");
pthread_mutex_unlock(&philo->same->mutex[only->next_id]); drop_fork(philo, only->id, only->next_id);
pthread_mutex_unlock(&philo->same->mutex[only->id]);
if (get_utime() - only->last_eat_time + config->time_sleep if (get_utime() - only->last_eat_time + config->time_sleep
>= config->time_die) >= config->time_die)
{ {
usleep(config->time_die - (get_utime() - only->last_eat_time)); ft_sleep(config->time_die - (get_utime() - only->last_eat_time));
return (0); return (0);
} }
else else
usleep(config->time_sleep); ft_sleep(config->time_sleep);
return (1); return (1);
} }
@ -89,25 +89,22 @@ void loop_philosopher(t_philo *philo, t_only *only,
{ {
while (!finish(philo, only->last_eat_time)) while (!finish(philo, only->last_eat_time))
{ {
if (!finish(philo, only->last_eat_time) wait_fork(philo, only, only->id);
&& !pthread_mutex_lock(&philo->same->mutex[only->id])) if (get_fork(philo, only->id))
{ {
take_fork(philo, only->id);
message(philo, "has taken a fork\n"); message(philo, "has taken a fork\n");
while (only->id == only->next_id while (only->id == only->next_id
&& !finish(philo, only->last_eat_time)) && !finish(philo, only->last_eat_time))
; ;
if (!finish(philo, only->last_eat_time) wait_fork(philo, only, only->next_id);
&& !pthread_mutex_lock(&philo->same->mutex[only->next_id])) if (get_fork(philo, only->next_id))
{ {
if (verif_finish(philo, only)) if (!before_eat(philo, only, config, nbr_philo))
break ;
message(philo, "has taken a fork\n");
message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
break ; break ;
} }
else else
pthread_mutex_unlock(&philo->same->mutex[only->id]); drop_fork(philo, only->id, -1);
} }
if (!finish(philo, only->last_eat_time)) if (!finish(philo, only->last_eat_time))
message(philo, "is thinking\n"); message(philo, "is thinking\n");
@ -124,15 +121,13 @@ void *philosopher(t_philo *philo)
config = philo->same->config; config = philo->same->config;
init_philo(philo, only, config, &nbr_philo); init_philo(philo, only, config, &nbr_philo);
loop_philosopher(philo, only, config, nbr_philo); loop_philosopher(philo, only, config, nbr_philo);
pthread_mutex_lock(&philo->same->mutex[nbr_philo + 1]);
pthread_mutex_lock(&philo->same->mutex[nbr_philo]); pthread_mutex_lock(&philo->same->mutex[nbr_philo]);
if (!*philo->same->death && (config->must_eat == -1 if (!philo->same->death && (config->must_eat == -1
|| (config->must_eat > -1 && only->eat < config->must_eat))) || (config->must_eat > -1 && only->eat < config->must_eat)))
{ {
*philo->same->death = 1; philo->same->death = 1;
message_die(philo, "died\n"); message_die(philo, "died\n");
} }
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]); pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
return (NULL); return (NULL);
} }

View file

@ -30,7 +30,7 @@ int ft_lltos(char *str, long long n)
q *= 10; q *= 10;
while (q > 0) while (q > 0)
{ {
str[i++] = (n / q % 10) + 48; str[i++] = (v / q % 10) + 48;
q /= 10; q /= 10;
} }
str[i] = '\0'; str[i] = '\0';

View file

@ -6,19 +6,35 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */ /* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
/* Updated: 2023/03/31 17:51:30 by erey-bet ### ########.fr */ /* Updated: 2023/04/10 09:53:55 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
void init_mutex(pthread_mutex_t *mutex, int nbr) int *init_forks(t_config *config)
{
int *forks;
int i;
forks = ft_calloc(sizeof(int), config->nbr_philo);
if (!forks)
return (NULL);
i = 0;
while (i < config->nbr_philo)
forks[i++] = 1;
return (forks);
}
int init_mutex(pthread_mutex_t *mutex, int nbr)
{ {
int i; int i;
i = -1; i = -1;
while (++i < nbr) while (++i < nbr)
pthread_mutex_init(&mutex[i], NULL); if (pthread_mutex_init(&mutex[i], NULL) != 0)
return (1);
return (0);
} }
void destroy_mutex(pthread_mutex_t *mutex, int nbr) void destroy_mutex(pthread_mutex_t *mutex, int nbr)
@ -30,21 +46,34 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr)
pthread_mutex_destroy(&mutex[i]); pthread_mutex_destroy(&mutex[i]);
} }
void free_all(pthread_t *threads, t_same *same, t_philo **philo) static void free_philo(t_philo **philo)
{ {
int i; int i;
destroy_mutex(same->mutex, 3);
free(threads);
free(same->mutex);
free(same->death);
free(same->all_eat);
free(same);
i = -1; i = -1;
while (philo[++i]) while (philo[++i])
{ {
free(philo[i]->only); if (philo[i]->only)
free(philo[i]); free(philo[i]->only);
if (philo[i])
free(philo[i]);
} }
free(philo); free(philo);
} }
int free_all(pthread_t *threads, t_same *same, t_philo **philo)
{
if (same && same->mutex)
destroy_mutex(same->mutex, same->config->nbr_philo + 3);
if (threads)
free(threads);
if (same && same->mutex)
free(same->mutex);
if (same && same->forks)
free(same->forks);
if (same)
free(same);
if (philo)
free_philo(philo);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */ /* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
/* Updated: 2023/04/01 12:08:51 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 19:29:54 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,24 +22,31 @@ long long get_utime(void)
void message_die(t_philo *philo, char *msg) void message_die(t_philo *philo, char *msg)
{ {
char send[1024]; t_config *config;
int i; char send[1024];
int i;
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); config = philo->same->config;
pthread_mutex_lock(&philo->same->mutex[config->nbr_philo + 2]);
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
send[i++] = ' '; send[i++] = ' ';
i += ft_lltos(send + i, philo->only->id + 1); i += ft_lltos(send + i, philo->only->id + 1);
send[i++] = ' '; send[i++] = ' ';
while (*msg) while (*msg)
send[i++] = *msg++; send[i++] = *msg++;
write(1, send, i); write(1, send, i);
pthread_mutex_unlock(&philo->same->mutex[config->nbr_philo + 2]);
} }
void message(t_philo *philo, char *msg) void message(t_philo *philo, char *msg)
{ {
char send[1024]; t_config *config;
int i; char send[1024];
int i;
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); config = philo->same->config;
pthread_mutex_lock(&philo->same->mutex[config->nbr_philo + 2]);
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
send[i++] = ' '; send[i++] = ' ';
i += ft_lltos(send + i, philo->only->id + 1); i += ft_lltos(send + i, philo->only->id + 1);
send[i++] = ' '; send[i++] = ' ';
@ -47,14 +54,14 @@ void message(t_philo *philo, char *msg)
send[i++] = *msg++; send[i++] = *msg++;
if (!finish(philo, philo->only->last_eat_time)) if (!finish(philo, philo->only->last_eat_time))
write(1, send, i); write(1, send, i);
pthread_mutex_unlock(&philo->same->mutex[config->nbr_philo + 2]);
} }
int verif_finish(t_philo *philo, t_only *only) int verif_finish(t_philo *philo, t_only *only)
{ {
if (finish(philo, only->last_eat_time)) if (finish(philo, only->last_eat_time))
{ {
pthread_mutex_unlock(&philo->same->mutex[only->next_id]); drop_fork(philo, only->id, only->next_id);
pthread_mutex_unlock(&philo->same->mutex[only->id]);
return (1); return (1);
} }
return (0); return (0);
@ -66,8 +73,7 @@ int verif_die_eating(t_philo *philo, t_only *only, t_config *config)
>= config->time_die) >= config->time_die)
{ {
usleep(config->time_die - (get_utime() - only->last_eat_time)); usleep(config->time_die - (get_utime() - only->last_eat_time));
pthread_mutex_unlock(&philo->same->mutex[only->next_id]); drop_fork(philo, only->id, only->next_id);
pthread_mutex_unlock(&philo->same->mutex[only->id]);
return (0); return (0);
} }
return (1); return (1);

46
philo/utils_threads2.c Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_threads2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/09 20:49:44 by erey-bet #+# #+# */
/* Updated: 2023/04/10 17:31:37 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void take_fork(t_philo *philo, int id_fork)
{
pthread_mutex_lock(&philo->same->mutex[id_fork]);
philo->same->forks[id_fork] = 0;
pthread_mutex_unlock(&philo->same->mutex[id_fork]);
usleep(10);
}
void drop_fork(t_philo *philo, int id_fork, int next_id_fork)
{
pthread_mutex_lock(&philo->same->mutex[id_fork]);
philo->same->forks[id_fork] = 1;
pthread_mutex_unlock(&philo->same->mutex[id_fork]);
if (next_id_fork >= 0)
{
pthread_mutex_lock(&philo->same->mutex[next_id_fork]);
philo->same->forks[next_id_fork] = 1;
pthread_mutex_unlock(&philo->same->mutex[next_id_fork]);
}
usleep(10);
}
int get_fork(t_philo *philo, int id_fork)
{
int fork;
pthread_mutex_lock(&philo->same->mutex[id_fork]);
fork = philo->same->forks[id_fork];
pthread_mutex_unlock(&philo->same->mutex[id_fork]);
usleep(10);
return (fork);
}

39
philo/utils_threads3.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_threads3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 10:39:43 by erey-bet #+# #+# */
/* Updated: 2023/04/10 10:39:56 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void ft_sleep(long long time_to_sleep)
{
if (time_to_sleep < 0)
return ;
usleep(time_to_sleep);
}
int before_eat(t_philo *philo, t_only *only,
t_config *config, int nbr_philo)
{
if (verif_finish(philo, only))
return (0);
take_fork(philo, only->next_id);
message(philo, "has taken a fork\n");
message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
return (0);
return (1);
}
void wait_fork(t_philo *philo, t_only *only, int id)
{
while (!finish(philo, only->last_eat_time) && !get_fork(philo, id))
;
}