Compare commits
10 commits
9f5f416a77
...
bbbd622259
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bbbd622259 | ||
![]() |
140889a1fd | ||
![]() |
29832e45c3 | ||
![]() |
30c70d01da | ||
![]() |
c30de10070 | ||
![]() |
61035874a8 | ||
![]() |
d1200f346b | ||
![]() |
c2f741e949 | ||
![]() |
af6b1bacf3 | ||
![]() |
3f0ec62059 |
|
@ -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_BONUS =
|
||||
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c utils_threads3.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
|
||||
UTILS = utils/utils.a
|
||||
CC = clang
|
||||
CFLAGS = -g -Wall -Wextra -Werror -pthread
|
||||
|
@ -13,10 +11,6 @@ ${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}
|
||||
|
@ -138,6 +132,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 4.26
|
||||
@@sleep 2.13
|
||||
@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/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;
|
||||
|
||||
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);
|
||||
}
|
||||
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/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);
|
||||
if (!same)
|
||||
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)
|
||||
return (NULL);
|
||||
init_mutex(same->mutex, config->nbr_philo + 2);
|
||||
return (same);
|
||||
if (init_mutex(same->mutex, config->nbr_philo + 3))
|
||||
return (same);
|
||||
same->config = config;
|
||||
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;
|
||||
same->death = 0;
|
||||
same->all_eat = 0;
|
||||
same->time = get_utime();
|
||||
return (same);
|
||||
}
|
||||
|
||||
|
@ -51,15 +50,22 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -75,7 +81,7 @@ int manage_threads(t_config *config)
|
|||
return (1);
|
||||
philo = ft_calloc(config->nbr_philo + 1, sizeof(t_philo *));
|
||||
if (philo == NULL)
|
||||
return (1);
|
||||
return (free_all(thds, NULL, NULL));
|
||||
same = create_struct_same(config);
|
||||
i = -1;
|
||||
while (++i < config->nbr_philo)
|
||||
|
@ -83,7 +89,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 (1);
|
||||
return (free_all(thds, same, philo));
|
||||
}
|
||||
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/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))
|
||||
{
|
||||
*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 (0);
|
||||
}
|
|
@ -6,21 +6,17 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
# define PHILO_H
|
||||
|
||||
# include "../utils/utils.h"
|
||||
# include <stdlib.h>
|
||||
# include "utils/utils.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
|
||||
{
|
||||
|
@ -34,17 +30,17 @@ typedef struct s_config
|
|||
typedef struct s_same
|
||||
{
|
||||
t_config *config;
|
||||
int *forks;
|
||||
pthread_mutex_t *mutex;
|
||||
int *all_eat;
|
||||
int *death;
|
||||
|
||||
int all_eat;
|
||||
int death;
|
||||
long long time;
|
||||
} t_same;
|
||||
|
||||
typedef struct s_only
|
||||
{
|
||||
int id;
|
||||
int next_id;
|
||||
long long time;
|
||||
long long last_eat_time;
|
||||
int eat;
|
||||
} 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);
|
||||
void message_die(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 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 */
|
||||
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/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)
|
||||
{
|
||||
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->time = get_utime();
|
||||
only->last_eat_time = only->time;
|
||||
only->last_eat_time = get_utime();
|
||||
if (only->id == only->next_id)
|
||||
return ;
|
||||
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)
|
||||
usleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
ft_sleep(config->time_die - (get_utime() - only->last_eat_time));
|
||||
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,
|
||||
t_config *config, int nbr_philo)
|
||||
{
|
||||
only->last_eat_time = get_utime();
|
||||
if (!verif_die_eating(philo, only, config))
|
||||
return (0);
|
||||
usleep(config->time_eat);
|
||||
only->last_eat_time = get_utime();
|
||||
ft_sleep(config->time_eat);
|
||||
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");
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
if (get_utime() - only->last_eat_time + config->time_sleep
|
||||
>= 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);
|
||||
}
|
||||
else
|
||||
usleep(config->time_sleep);
|
||||
ft_sleep(config->time_sleep);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
@ -89,25 +89,22 @@ void loop_philosopher(t_philo *philo, t_only *only,
|
|||
{
|
||||
while (!finish(philo, only->last_eat_time))
|
||||
{
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->id]))
|
||||
wait_fork(philo, only, only->id);
|
||||
if (get_fork(philo, 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))
|
||||
;
|
||||
if (!finish(philo, only->last_eat_time)
|
||||
&& !pthread_mutex_lock(&philo->same->mutex[only->next_id]))
|
||||
wait_fork(philo, only, only->next_id);
|
||||
if (get_fork(philo, only->next_id))
|
||||
{
|
||||
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))
|
||||
if (!before_eat(philo, only, config, nbr_philo))
|
||||
break ;
|
||||
}
|
||||
else
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
drop_fork(philo, only->id, -1);
|
||||
}
|
||||
if (!finish(philo, only->last_eat_time))
|
||||
message(philo, "is thinking\n");
|
||||
|
@ -124,15 +121,13 @@ 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);
|
||||
}
|
|
@ -30,7 +30,7 @@ int ft_lltos(char *str, long long n)
|
|||
q *= 10;
|
||||
while (q > 0)
|
||||
{
|
||||
str[i++] = (n / q % 10) + 48;
|
||||
str[i++] = (v / q % 10) + 48;
|
||||
q /= 10;
|
||||
}
|
||||
str[i] = '\0';
|
|
@ -6,19 +6,35 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
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;
|
||||
|
||||
i = -1;
|
||||
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)
|
||||
|
@ -30,21 +46,34 @@ void destroy_mutex(pthread_mutex_t *mutex, int nbr)
|
|||
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;
|
||||
|
||||
destroy_mutex(same->mutex, 3);
|
||||
free(threads);
|
||||
free(same->mutex);
|
||||
free(same->death);
|
||||
free(same->all_eat);
|
||||
free(same);
|
||||
i = -1;
|
||||
while (philo[++i])
|
||||
{
|
||||
free(philo[i]->only);
|
||||
free(philo[i]);
|
||||
if (philo[i]->only)
|
||||
free(philo[i]->only);
|
||||
if (philo[i])
|
||||
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/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)
|
||||
{
|
||||
char send[1024];
|
||||
int i;
|
||||
t_config *config;
|
||||
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++] = ' ';
|
||||
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)
|
||||
{
|
||||
char send[1024];
|
||||
int i;
|
||||
t_config *config;
|
||||
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++] = ' ';
|
||||
i += ft_lltos(send + i, philo->only->id + 1);
|
||||
send[i++] = ' ';
|
||||
|
@ -47,14 +54,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))
|
||||
{
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
|
@ -66,8 +73,7 @@ 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));
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->next_id]);
|
||||
pthread_mutex_unlock(&philo->same->mutex[only->id]);
|
||||
drop_fork(philo, only->id, only->next_id);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
46
philo/utils_threads2.c
Normal file
46
philo/utils_threads2.c
Normal 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
39
philo/utils_threads3.c
Normal 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))
|
||||
;
|
||||
}
|
Loading…
Reference in a new issue