This commit is contained in:
Etienne Rey-bethbeder 2023-04-09 22:41:29 +02:00
parent 30c70d01da
commit 29832e45c3
7 changed files with 83 additions and 50 deletions

View file

@ -1,4 +1,4 @@
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
UTILS = utils/utils.a UTILS = utils/utils.a
CC = clang CC = clang
@ -132,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/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,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/04/08 19:38:07 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 19:29:44 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,6 +67,10 @@ void message(t_philo *philo, char *msg);
int 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);
int 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);
/* THREADS */ /* THREADS */
void *philosopher(t_philo *philo); void *philosopher(t_philo *philo);

View file

@ -6,42 +6,12 @@
/* 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/07 15:19:34 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 19:45:30 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #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]);
}
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]);
}
}
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]);
return (fork);
}
int finish(t_philo *philo, long long last_eat_time) int finish(t_philo *philo, long long last_eat_time)
{ {
int dead; int dead;
@ -77,9 +47,9 @@ void init_philo(t_philo *philo, t_only *only,
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));
} }
} }
@ -89,7 +59,7 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only,
only->last_eat_time = get_utime(); 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);
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)
@ -104,11 +74,11 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only,
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);
} }
@ -117,23 +87,29 @@ void loop_philosopher(t_philo *philo, t_only *only,
{ {
while (!finish(philo, only->last_eat_time)) while (!finish(philo, only->last_eat_time))
{ {
while (!finish(philo, only->last_eat_time) && get_fork(philo, only->id)) while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->id))
;
if (get_fork(philo, only->id))
{ {
take_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))
; ;
while (!finish(philo, only->last_eat_time) && get_fork(philo, only->next_id)) while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->next_id))
;
if (get_fork(philo, only->next_id))
{ {
if (verif_finish(philo, only)) if (verif_finish(philo, only))
break ; break ;
take_fork(philo, only->next_id);
message(philo, "has taken a fork\n"); message(philo, "has taken a fork\n");
message(philo, "is eating\n"); message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo)) if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
break ; break ;
} }
drop_fork(philo, only->id, -1); else
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");

View file

@ -6,7 +6,7 @@
/* 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/04/08 16:10:05 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 18:09:18 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,6 +55,8 @@ int free_all(pthread_t *threads, t_same *same, t_philo **philo)
free(threads); free(threads);
if (same && same->mutex) if (same && same->mutex)
free(same->mutex); free(same->mutex);
if (same && same->forks)
free(same->forks);
if (same) if (same)
free(same); free(same);
if (philo) if (philo)

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/08 16:05:23 by erey-bet ### ########.fr */ /* Updated: 2023/04/09 19:29:54 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,8 +61,7 @@ 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);
@ -74,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);

51
philo/utils_threads2.c Normal file
View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* forks_threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/09 20:49:44 by erey-bet #+# #+# */
/* Updated: 2023/04/09 20:49:47 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]);
}
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]);
}
}
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);
}
void ft_sleep(long long time_to_sleep)
{
if (time_to_sleep < 0)
return ;
usleep(time_to_sleep);
}