Compare commits
No commits in common. "bbbd622259b24b6c6be796b8590ad283d0212045" and "9f5f416a7797672e84c9d92cc24936261a278ed5" have entirely different histories.
bbbd622259
...
9f5f416a77
|
@ -1,5 +1,7 @@
|
|||
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c utils_threads3.c
|
||||
SRCS = mandatory/main.c mandatory/parsing.c mandatory/threads.c mandatory/manage_threads.c mandatory/utils_threads.c mandatory/utils_manage_threads.c
|
||||
SRCS_BONUS =
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
|
||||
UTILS = utils/utils.a
|
||||
CC = clang
|
||||
CFLAGS = -g -Wall -Wextra -Werror -pthread
|
||||
|
@ -11,6 +13,10 @@ ${NAME}: ${OBJS}
|
|||
make -C utils
|
||||
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS}
|
||||
|
||||
bonus: ${OBJS_BONUS}
|
||||
make -C utils
|
||||
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${UTILS}
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS}
|
||||
rm -f ${OBJS_BONUS}
|
||||
|
@ -132,6 +138,6 @@ coffee:
|
|||
bozo :
|
||||
@wget -q -O bozo.gif https://i.kym-cdn.com/photos/images/newsfeed/002/322/200/e51.gif
|
||||
@xdg-open bozo.gif
|
||||
@@sleep 2.13
|
||||
@@sleep 4.26
|
||||
@pkill eog
|
||||
@rm bozo.gif
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/07 14:41:17 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/30 00:07:31 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -16,9 +16,9 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
t_config config;
|
||||
|
||||
if (argc < 5 || argc > 6)
|
||||
if (argc < 5)
|
||||
{
|
||||
write(2, "Wrong number of argument\n", 25);
|
||||
write(2, "Not enought argument\n", 21);
|
||||
return (1);
|
||||
}
|
||||
if (parsing(argv, &config))
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/10 15:55:42 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 17:52:22 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -19,18 +19,19 @@ static t_same *create_struct_same(t_config *config)
|
|||
same = ft_calloc(sizeof(t_same), 1);
|
||||
if (!same)
|
||||
return (NULL);
|
||||
same->forks = init_forks(config);
|
||||
if (!same->forks)
|
||||
return (same);
|
||||
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 3);
|
||||
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 2);
|
||||
if (!same->mutex)
|
||||
return (same);
|
||||
if (init_mutex(same->mutex, config->nbr_philo + 3))
|
||||
return (same);
|
||||
return (NULL);
|
||||
init_mutex(same->mutex, config->nbr_philo + 2);
|
||||
same->config = config;
|
||||
same->death = 0;
|
||||
same->all_eat = 0;
|
||||
same->time = get_utime();
|
||||
same->death = ft_calloc(sizeof(int), 1);
|
||||
if (!same->death)
|
||||
return (NULL);
|
||||
*same->death = 0;
|
||||
same->all_eat = ft_calloc(sizeof(int), 1);
|
||||
if (!same->all_eat)
|
||||
return (NULL);
|
||||
*same->all_eat = 0;
|
||||
return (same);
|
||||
}
|
||||
|
||||
|
@ -50,22 +51,15 @@ static t_philo *init_philo(t_same *same, t_only *only)
|
|||
{
|
||||
t_philo *philo;
|
||||
|
||||
if (!same)
|
||||
{
|
||||
if (only)
|
||||
free(only);
|
||||
return (NULL);
|
||||
}
|
||||
if (!only)
|
||||
{
|
||||
free(same);
|
||||
return (NULL);
|
||||
}
|
||||
philo = ft_calloc(sizeof(t_philo), 1);
|
||||
if (!philo)
|
||||
return (NULL);
|
||||
philo->same = same;
|
||||
if (!philo->same)
|
||||
return (NULL);
|
||||
philo->only = only;
|
||||
if (!philo->only)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
}
|
||||
|
||||
|
@ -81,7 +75,7 @@ int manage_threads(t_config *config)
|
|||
return (1);
|
||||
philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *));
|
||||
if (philo == NULL)
|
||||
return (free_all(thds, NULL, NULL));
|
||||
return (1);
|
||||
same = create_struct_same(config);
|
||||
i = -1;
|
||||
while (++i < config->nbr_philo)
|
||||
|
@ -89,7 +83,7 @@ int manage_threads(t_config *config)
|
|||
philo[i] = init_philo(same, create_struct_only(i));
|
||||
if (!philo[i] || pthread_create(&thds[i], NULL,
|
||||
(void *)&philosopher, philo[i]) != 0)
|
||||
return (free_all(thds, same, philo));
|
||||
return (1);
|
||||
}
|
||||
philo[i] = NULL;
|
||||
while (--i > -1)
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/09 18:20:49 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 15:13:30 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -25,9 +25,7 @@ int get_argv(char **argv, long *conf, char *message, int multiplier)
|
|||
if (ft_atoi_check(*argv))
|
||||
{
|
||||
*conf = (long)ft_atoi(*argv) * multiplier;
|
||||
if (*conf < 0)
|
||||
return (error("Wrong number of ", message));
|
||||
else if (*conf < 1 && multiplier == 1)
|
||||
if (*conf < 1)
|
||||
return (error("Wrong number of ", message));
|
||||
return (0);
|
||||
}
|
|
@ -6,17 +6,21 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/10 11:21:38 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/01 11:58:47 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include "utils/utils.h"
|
||||
# include "../utils/utils.h"
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <sys/wait.h>
|
||||
# include <sys/time.h>
|
||||
# include <pthread.h>
|
||||
# include <stdio.h>
|
||||
|
||||
typedef struct s_config
|
||||
{
|
||||
|
@ -30,17 +34,17 @@ typedef struct s_config
|
|||
typedef struct s_same
|
||||
{
|
||||
t_config *config;
|
||||
int *forks;
|
||||
pthread_mutex_t *mutex;
|
||||
int all_eat;
|
||||
int death;
|
||||
long long time;
|
||||
int *all_eat;
|
||||
int *death;
|
||||
|
||||
} t_same;
|
||||
|
||||
typedef struct s_only
|
||||
{
|
||||
int id;
|
||||
int next_id;
|
||||
long long time;
|
||||
long long last_eat_time;
|
||||
int eat;
|
||||
} t_only;
|
||||
|
@ -64,19 +68,9 @@ int verif_finish(t_philo *philo, t_only *only);
|
|||
int finish(t_philo *philo, long long last_eat_time);
|
||||
void message_die(t_philo *philo, char *msg);
|
||||
void message(t_philo *philo, char *msg);
|
||||
int init_mutex(pthread_mutex_t *mutex, int nbr);
|
||||
void init_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 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);
|
||||
void free_all(pthread_t *threads, t_same *same, t_philo **philo);
|
||||
|
||||
/* THREADS */
|
||||
void *philosopher(t_philo *philo);
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/10 17:22:22 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/01 11:57:59 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)
|
||||
{
|
||||
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_lock(&philo->same->mutex[nbr_philo]);
|
||||
dead = philo->same->death;
|
||||
dead = *philo->same->death;
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
|
||||
return (get_utime() - last_eat_time >= philo->same->config->time_die
|
||||
|| dead
|
||||
|
@ -40,47 +40,47 @@ void init_philo(t_philo *philo, t_only *only,
|
|||
{
|
||||
*nbr_philo = philo->same->config->nbr_philo;
|
||||
only->next_id = (only->id + 1) % *nbr_philo;
|
||||
only->last_eat_time = get_utime();
|
||||
if (only->id == only->next_id)
|
||||
return ;
|
||||
only->time = get_utime();
|
||||
only->last_eat_time = only->time;
|
||||
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
|
||||
&& only->id == *nbr_philo - 1))
|
||||
{
|
||||
message(philo, "is thinking\n");
|
||||
if (get_utime() - only->last_eat_time + config->time_eat
|
||||
>= config->time_die)
|
||||
ft_sleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
else
|
||||
ft_sleep(config->time_eat / (only->id % 2 + 1));
|
||||
usleep(config->time_eat / (only->id % 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
int in_loop_eat_sleep(t_philo *philo, t_only *only,
|
||||
t_config *config, int nbr_philo)
|
||||
{
|
||||
only->last_eat_time = get_utime();
|
||||
if (!verif_die_eating(philo, only, config))
|
||||
return (0);
|
||||
ft_sleep(config->time_eat);
|
||||
usleep(config->time_eat);
|
||||
only->last_eat_time = get_utime();
|
||||
if (verif_finish(philo, only))
|
||||
return (0);
|
||||
if (config->must_eat > -1 && only->eat < config->must_eat)
|
||||
{
|
||||
only->eat++;
|
||||
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]);
|
||||
}
|
||||
message(philo, "is sleeping\n");
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
if (get_utime() - only->last_eat_time + config->time_sleep
|
||||
>= config->time_die)
|
||||
{
|
||||
ft_sleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
ft_sleep(config->time_sleep);
|
||||
usleep(config->time_sleep);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
@ -89,22 +89,25 @@ void loop_philosopher(t_philo *philo, t_only *only,
|
|||
{
|
||||
while (!finish(philo, only->last_eat_time))
|
||||
{
|
||||
wait_fork(philo, only, only->id);
|
||||
if (get_fork(philo, only->id))
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->id]))
|
||||
{
|
||||
take_fork(philo, only->id);
|
||||
message(philo, "has taken a fork\n");
|
||||
while (only->id == only->next_id
|
||||
&& !finish(philo, only->last_eat_time))
|
||||
;
|
||||
wait_fork(philo, only, only->next_id);
|
||||
if (get_fork(philo, only->next_id))
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->next_id]))
|
||||
{
|
||||
if (!before_eat(philo, only, config, nbr_philo))
|
||||
if (verif_finish(philo, only))
|
||||
break ;
|
||||
message(philo, "has taken a fork\n");
|
||||
message(philo, "is eating\n");
|
||||
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
|
||||
break ;
|
||||
}
|
||||
else
|
||||
drop_fork(philo, only->id, -1);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
}
|
||||
if (!finish(philo, only->last_eat_time))
|
||||
message(philo, "is thinking\n");
|
||||
|
@ -121,13 +124,15 @@ void *philosopher(t_philo *philo)
|
|||
config = philo->same->config;
|
||||
init_philo(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]);
|
||||
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)))
|
||||
{
|
||||
philo->same->death = 1;
|
||||
*philo->same->death = 1;
|
||||
message_die(philo, "died\n");
|
||||
}
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[nbr_philo + 1]);
|
||||
return (NULL);
|
||||
}
|
|
@ -6,35 +6,19 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/10 09:53:55 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/31 17:51:30 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
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)
|
||||
void init_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (++i < nbr)
|
||||
if (pthread_mutex_init(&mutex[i], NULL) != 0)
|
||||
return (1);
|
||||
return (0);
|
||||
pthread_mutex_init(&mutex[i], NULL);
|
||||
}
|
||||
|
||||
void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
||||
|
@ -46,34 +30,21 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
|||
pthread_mutex_destroy(&mutex[i]);
|
||||
}
|
||||
|
||||
static void free_philo(t_philo **philo)
|
||||
void free_all(pthread_t *threads, t_same *same, t_philo **philo)
|
||||
{
|
||||
int i;
|
||||
|
||||
destroy_mutex(same->mutex, 3);
|
||||
free(threads);
|
||||
free(same->mutex);
|
||||
free(same->death);
|
||||
free(same->all_eat);
|
||||
free(same);
|
||||
i = -1;
|
||||
while (philo[++i])
|
||||
{
|
||||
if (philo[i]->only)
|
||||
free(philo[i]->only);
|
||||
if (philo[i])
|
||||
free(philo[i]);
|
||||
free(philo[i]->only);
|
||||
free(philo[i]);
|
||||
}
|
||||
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);
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/09 19:29:54 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/01 12:08:51 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -22,31 +22,24 @@ long long get_utime(void)
|
|||
|
||||
void message_die(t_philo *philo, char *msg)
|
||||
{
|
||||
t_config *config;
|
||||
char send[1024];
|
||||
int i;
|
||||
char send[1024];
|
||||
int i;
|
||||
|
||||
config = philo->same->config;
|
||||
pthread_mutex_lock(&philo->same->mutex[config->nbr_philo + 2]);
|
||||
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
|
||||
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000);
|
||||
send[i++] = ' ';
|
||||
i += ft_lltos(send + i, philo->only->id + 1);
|
||||
send[i++] = ' ';
|
||||
while (*msg)
|
||||
send[i++] = *msg++;
|
||||
write(1, send, i);
|
||||
pthread_mutex_unlock(&philo->same->mutex[config->nbr_philo + 2]);
|
||||
}
|
||||
|
||||
void message(t_philo *philo, char *msg)
|
||||
{
|
||||
t_config *config;
|
||||
char send[1024];
|
||||
int i;
|
||||
char send[1024];
|
||||
int i;
|
||||
|
||||
config = philo->same->config;
|
||||
pthread_mutex_lock(&philo->same->mutex[config->nbr_philo + 2]);
|
||||
i = ft_lltos(send, (get_utime() - philo->same->time) / 1000);
|
||||
i = ft_lltos(send, (get_utime() - philo->only->time) / 1000);
|
||||
send[i++] = ' ';
|
||||
i += ft_lltos(send + i, philo->only->id + 1);
|
||||
send[i++] = ' ';
|
||||
|
@ -54,14 +47,14 @@ void message(t_philo *philo, char *msg)
|
|||
send[i++] = *msg++;
|
||||
if (!finish(philo, philo->only->last_eat_time))
|
||||
write(1, send, i);
|
||||
pthread_mutex_unlock(&philo->same->mutex[config->nbr_philo + 2]);
|
||||
}
|
||||
|
||||
int verif_finish(t_philo *philo, t_only *only)
|
||||
{
|
||||
if (finish(philo, only->last_eat_time))
|
||||
{
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
|
@ -73,7 +66,8 @@ int verif_die_eating(t_philo *philo, t_only *only, t_config *config)
|
|||
>= config->time_die)
|
||||
{
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
|
@ -1,46 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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))
|
||||
;
|
||||
}
|
|
@ -30,7 +30,7 @@ int ft_lltos(char *str, long long n)
|
|||
q *= 10;
|
||||
while (q > 0)
|
||||
{
|
||||
str[i++] = (v / q % 10) + 48;
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
q /= 10;
|
||||
}
|
||||
str[i] = '\0';
|
Loading…
Reference in a new issue