Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9abb157c3 | ||
|
|
5f86d2de1c | ||
|
|
1dc4de3236 | ||
|
|
ef2eb4e921 | ||
|
|
2fa49b3242 | ||
|
|
41726f5e41 | ||
|
|
774bdb0a4a | ||
|
|
c3ef767bdb | ||
|
|
7dc4c5a84f | ||
|
|
d9142b9af6 | ||
|
|
836c02f3f5 | ||
|
|
e45ca1331f | ||
|
|
232684b73a | ||
|
|
eec1a73786 | ||
|
|
77f2e6b978 | ||
|
|
6d3280631c | ||
|
|
b192efa4a9 | ||
|
|
ab0c4efa35 | ||
|
|
bf80fde30b | ||
|
|
2f747d0563 | ||
|
|
6561340fb9 | ||
|
|
03633fa5b2 | ||
|
|
475817540c | ||
|
|
d8475537e0 | ||
|
|
b998de9340 | ||
|
|
82fef42d2a | ||
|
|
cb615cb6b3 | ||
|
|
3ba76ccc4e | ||
|
|
6a4aad9503 | ||
|
|
be439700e9 | ||
|
|
02b377a582 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,3 +12,4 @@ Thumbs.db
|
||||
*.lnk
|
||||
*.zip
|
||||
philo
|
||||
!philo/
|
||||
|
||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "libft"]
|
||||
path = libft
|
||||
url = git@bitbucket.org:LuckyLaszlo/libft.git
|
||||
BIN
correction.png
Normal file
BIN
correction.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 793 KiB |
@@ -1,16 +0,0 @@
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
# include "philo_struct.h"
|
||||
# include "philo_proto.h"
|
||||
# include "philo_macro.h"
|
||||
|
||||
#endif
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef PHILO_MACRO_H
|
||||
# define PHILO_MACRO_H
|
||||
|
||||
# define GRAY "\e[0;30m"
|
||||
# define RED "\e[0;31m"
|
||||
# define GREEN "\e[0;32m"
|
||||
# define YELLOW "\e[0;33m"
|
||||
# define BLUE "\e[0;34m"
|
||||
# define PURPLE "\e[0;35m"
|
||||
# define CYAN "\e[0;36m"
|
||||
# define WHITE "\e[0;37m"
|
||||
|
||||
# define B_GRAY "\e[1;30m"
|
||||
# define B_RED "\e[1;31m"
|
||||
# define B_GREEN "\e[1;32m"
|
||||
# define B_YELLOW "\e[1;33m"
|
||||
# define B_BLUE "\e[1;34m"
|
||||
# define B_PURPLE "\e[1;35m"
|
||||
# define B_CYAN "\e[1;36m"
|
||||
# define B_WHITE "\e[1;37m"
|
||||
|
||||
# define RESET "\e[0m"
|
||||
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
#ifndef PHILO_PROTO_H
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// init.c
|
||||
t_philo *init(int ac, char **av, pthread_t **id);
|
||||
|
||||
// exec.c
|
||||
void *philo_exec(void *arg);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#ifndef PHILO_STRUCT_H
|
||||
# define PHILO_STRUCT_H
|
||||
|
||||
typedef pthread_mutex_t t_mtx;
|
||||
|
||||
typedef struct s_params
|
||||
{
|
||||
int n_phi; // number_of_philosophers
|
||||
int t_die; // time_to_die
|
||||
int t_eat; // time_to_eat
|
||||
int t_slp; // time_to_sleep
|
||||
int n_eat; // [number_of_times_each_philosopher_must_eat]
|
||||
} t_params;
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
t_params *params;
|
||||
int p_nbr;
|
||||
t_mtx m_fork;
|
||||
t_mtx *m_print;
|
||||
long int t_start_s;
|
||||
long int t_start_u;
|
||||
long int t_last_meal_s;
|
||||
long int t_last_meal_u;
|
||||
struct s_philo *prev;
|
||||
struct s_philo *next;
|
||||
} t_philo;
|
||||
|
||||
#endif
|
||||
1
libft
1
libft
Submodule libft deleted from af88c1ce23
@@ -2,28 +2,27 @@ NAME = philo
|
||||
|
||||
CC = clang
|
||||
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3
|
||||
|
||||
VPATH = $(DIR_SRCS)
|
||||
DIR_SRCS = srcs
|
||||
|
||||
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
|
||||
INCLUDES = -I$(HEADERS_D)
|
||||
|
||||
HEADERS_D = ./headers
|
||||
HEADERS_D = .
|
||||
HEADERS = philo.h \
|
||||
philo_struct.h \
|
||||
philo_proto.h \
|
||||
philo_macro.h
|
||||
|
||||
LIBS = -L $(LIBFT_D) -lft \
|
||||
-lpthread
|
||||
|
||||
LIBFT_D = ./libft
|
||||
LIBFT = $(LIBFT_D)/libft.a
|
||||
LIBS = -lpthread
|
||||
|
||||
SRCS = main.c \
|
||||
init.c \
|
||||
exec.c
|
||||
launch.c \
|
||||
exec.c \
|
||||
utils.c \
|
||||
generic.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
@@ -32,13 +31,7 @@ OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
# ------ RULES -------
|
||||
# --------------------
|
||||
|
||||
all: subsystem $(NAME)
|
||||
|
||||
subsystem:
|
||||
@cd $(LIBFT_D) && $(MAKE)
|
||||
|
||||
$(LIBFT): # dispensable. utile seulement pour un appel direct à $(NAME), si $(LIBFT) n'existe pas
|
||||
cd $(LIBFT_D) && $(MAKE)
|
||||
all: $(NAME)
|
||||
|
||||
$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
@@ -48,11 +41,12 @@ $(DIR_OBJS):
|
||||
|
||||
$(OBJS): $(HEADERS:%=$(HEADERS_D)/%)
|
||||
|
||||
$(NAME): $(OBJS) $(LIBFT)
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(OBJS) -o $(NAME) $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
rm -rf $(OBJS)
|
||||
rm -rf $(DIR_OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
129
philo/exec.c
Normal file
129
philo/exec.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/02/01 18:08:58 by hulamy #+# #+# */
|
||||
/* Updated: 2022/02/02 00:28:14 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static void action_delay(t_philo *philo, int action_time)
|
||||
{
|
||||
struct timeval stime;
|
||||
int death_time;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
death_time = philo->params->t_die - diff_time(philo, &stime);
|
||||
if (death_time > action_time)
|
||||
usleep(action_time * 1000);
|
||||
else if (death_time > 0)
|
||||
usleep(philo->params->t_die * 1000);
|
||||
}
|
||||
|
||||
static int ret_err_unlock(t_mtx *fork1, t_mtx *fork2)
|
||||
{
|
||||
if (fork2)
|
||||
pthread_mutex_unlock(fork2);
|
||||
if (fork1)
|
||||
pthread_mutex_unlock(fork1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int eat(t_philo *philo, t_mtx *fork1, t_mtx *fork2)
|
||||
{
|
||||
pthread_mutex_lock(fork1);
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(fork1, NULL));
|
||||
if (fork1 == fork2)
|
||||
return (ret_err_unlock(fork1, NULL));
|
||||
pthread_mutex_lock(fork2);
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(fork1, fork2));
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating"))
|
||||
return (ret_err_unlock(fork1, fork2));
|
||||
action_delay(philo, philo->params->t_eat);
|
||||
pthread_mutex_lock(&(philo->m_eat));
|
||||
philo->eat_count++;
|
||||
pthread_mutex_unlock(&(philo->m_eat));
|
||||
pthread_mutex_unlock(fork2);
|
||||
pthread_mutex_unlock(fork1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** just after the assignation of the two forks :
|
||||
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
{
|
||||
fork1 = &(philo->next->m_fork);
|
||||
fork2 = &(philo->m_fork);
|
||||
}
|
||||
|
||||
** just before the while :
|
||||
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
|
||||
** just at the end of the while
|
||||
|
||||
usleep(1 * 1000);
|
||||
*/
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_mtx *fork1;
|
||||
t_mtx *fork2;
|
||||
|
||||
philo = (t_philo *)arg;
|
||||
init_time(philo);
|
||||
fork1 = &(philo->m_fork);
|
||||
fork2 = &(philo->next->m_fork);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
if (eat(philo, fork1, fork2) != 0)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping"))
|
||||
break ;
|
||||
action_delay(philo, philo->params->t_slp);
|
||||
if (print_message(philo, B_GREEN, "is thinking"))
|
||||
break ;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
3 610 200 100 . 3 610 200 100 . 3 610 200 200
|
||||
. .
|
||||
-(1) (2)- -(3) . -(1) -(2) -(3) . -(1) (2)- -(3)
|
||||
. .
|
||||
(ms) . .
|
||||
. .
|
||||
0: +-(1)-+ (2)- +-(3)- . +-(1)-+ -(2) +-(3)- . +-(1)-+ (2)- +-(3)-
|
||||
| . | . |
|
||||
100: | (2)- +-(3)- . | -(2) +-(3)- . | (2)- +-(3)-
|
||||
| . | . |
|
||||
200: ((|)) (2)- +-(3)-+ . ((|)) +-(2)- +-(3)-+ . ((|)) (2)- +-(3)-+
|
||||
| | . | | . | |
|
||||
300: -(1) (2)- | . -(1) +-(2)- | . | (2)- |
|
||||
| . | . | |
|
||||
400: +-(1)-+ -(2)-+ ((|)) . +-(1)- +-(2)-+ ((|)) . +-(1) +-(2)-+ ((|))
|
||||
| | . | | . | |
|
||||
500: | -(2)-+ -(3) . +-(1)- | -(3) . +-(1)- | |
|
||||
| . | . | |
|
||||
600: ((|)) +-(2)-+ -(3) . +-(1)-+ ((|)) +-(3)- . +-(1)-+ ((|)) +-(3)
|
||||
| | . | | . | |
|
||||
700: +-(1)- | -(3) . | -(2) +-(3)- . | | +-(3)-
|
||||
| . | . | |
|
||||
800: +-(1)-+ ((|)) +-(3)- . ((|)) +-(2)- +-(3)-+ . ((|)) +-(2)- +-(3)-+
|
||||
| | xxxxxxx . | | . | |
|
||||
900: | (2)- . -(1) +-(2)- | . | +-(2)- |
|
||||
|
||||
*/
|
||||
76
philo/generic.c
Normal file
76
philo/generic.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/30 15:47:44 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void init_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
pthread_mutex_lock(&(philo->global->m_init_time));
|
||||
if (philo->global->t_start.ts == 0)
|
||||
{
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
}
|
||||
pthread_mutex_lock(&(philo->m_time));
|
||||
philo->t_last_meal.ts = philo->global->t_start.ts;
|
||||
philo->t_last_meal.tu = philo->global->t_start.tu;
|
||||
pthread_mutex_unlock(&(philo->m_time));
|
||||
pthread_mutex_unlock(&(philo->global->m_init_time));
|
||||
}
|
||||
|
||||
void update_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
pthread_mutex_lock(&(philo->m_time));
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->t_last_meal.ts = stime.tv_sec;
|
||||
philo->t_last_meal.tu = stime.tv_usec;
|
||||
pthread_mutex_unlock(&(philo->m_time));
|
||||
}
|
||||
|
||||
int diff_time(t_philo *philo, struct timeval *new)
|
||||
{
|
||||
int t_diff;
|
||||
t_time old;
|
||||
|
||||
pthread_mutex_lock(&philo->m_time);
|
||||
old = philo->t_last_meal;
|
||||
t_diff = (new->tv_sec - old.ts) * 1000 + (new->tv_usec - old.tu) / 1000;
|
||||
pthread_mutex_unlock(&philo->m_time);
|
||||
return (t_diff);
|
||||
}
|
||||
|
||||
int print_message(t_philo *philo, char *clr, char *msg)
|
||||
{
|
||||
long int time_stamp;
|
||||
struct timeval stime;
|
||||
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
pthread_mutex_lock(&(philo->global->m_stop));
|
||||
if (philo->global->stop)
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->global->m_stop));
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
pthread_mutex_unlock(&(philo->global->m_stop));
|
||||
gettimeofday(&stime, NULL);
|
||||
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
||||
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (0);
|
||||
}
|
||||
133
philo/init.c
Normal file
133
philo/init.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
|
||||
/* Updated: 2022/02/02 00:18:04 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||
{
|
||||
t_philo *new;
|
||||
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->params = params;
|
||||
new->global = global;
|
||||
new->p_nbr = i + 1;
|
||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||
return (NULL);
|
||||
if (pthread_mutex_init(&(new->m_time), NULL) != 0)
|
||||
return (NULL);
|
||||
if (pthread_mutex_init(&(new->m_eat), NULL) != 0)
|
||||
return (NULL);
|
||||
new->t_last_meal.ts = 0;
|
||||
new->t_last_meal.tu = 0;
|
||||
new->eat_count = 0;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
static t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_philo *tmp;
|
||||
t_philo *start;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
tmp = lst_add_philo(params, global, i);
|
||||
if (philo)
|
||||
philo->next = tmp;
|
||||
else
|
||||
start = tmp;
|
||||
philo = tmp;
|
||||
i++;
|
||||
}
|
||||
if (philo)
|
||||
philo->next = start;
|
||||
return (philo);
|
||||
}
|
||||
|
||||
static t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_params *params;
|
||||
int i;
|
||||
|
||||
if (ac < 5 || ac > 6)
|
||||
return (NULL);
|
||||
if (!ft_isdigit_2d_arr(av + 1))
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
if (ft_int_overflow(av[i]))
|
||||
return (NULL);
|
||||
i++;
|
||||
}
|
||||
params = malloc(sizeof(t_params));
|
||||
if (!params)
|
||||
return (NULL);
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
params->n_eat = -1;
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
return (params);
|
||||
}
|
||||
|
||||
static t_global *init_global(void)
|
||||
{
|
||||
t_global *global;
|
||||
|
||||
global = malloc(sizeof(t_global));
|
||||
if (!global)
|
||||
return (NULL);
|
||||
global->stop = 0;
|
||||
global->satiated_count = 0;
|
||||
global->t_start.ts = 0;
|
||||
global->t_start.tu = 0;
|
||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||
return (NULL);
|
||||
if (pthread_mutex_init(&(global->m_init_time), NULL) != 0)
|
||||
return (NULL);
|
||||
if (pthread_mutex_init(&(global->m_stop), NULL) != 0)
|
||||
return (NULL);
|
||||
return (global);
|
||||
}
|
||||
|
||||
t_philo *init(int ac, char **av, pthread_t **id)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_params *params;
|
||||
t_global *global;
|
||||
|
||||
philo = NULL;
|
||||
params = NULL;
|
||||
global = NULL;
|
||||
params = init_params(ac, av);
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
global = init_global();
|
||||
if (global == NULL)
|
||||
return (return_free(params, NULL, NULL));
|
||||
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||
if (*id == NULL)
|
||||
return (return_free(params, global, NULL));
|
||||
philo = init_chain_philo(params, global);
|
||||
if (philo == NULL)
|
||||
return (return_free(params, global, *id));
|
||||
return (philo);
|
||||
}
|
||||
90
philo/launch.c
Normal file
90
philo/launch.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* launch.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
|
||||
/* Updated: 2022/02/01 23:44:28 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
static int is_finished(t_philo *philo, struct timeval *stime)
|
||||
{
|
||||
long int time_stamp;
|
||||
int time;
|
||||
|
||||
if (philo->global->satiated_count == philo->params->n_phi)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_stop));
|
||||
philo->global->stop = 1;
|
||||
pthread_mutex_unlock(&(philo->global->m_stop));
|
||||
return (1);
|
||||
}
|
||||
time = diff_time(philo, stime);
|
||||
if (time >= philo->params->t_die)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
philo->global->stop = 1;
|
||||
time_stamp = (stime->tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime->tv_usec - philo->global->t_start.tu) / 1000;
|
||||
printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_satiated(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_eat));
|
||||
if (philo->eat_count == philo->params->n_eat)
|
||||
{
|
||||
philo->global->satiated_count++;
|
||||
pthread_mutex_unlock(&(philo->m_eat));
|
||||
return (1);
|
||||
}
|
||||
pthread_mutex_unlock(&(philo->m_eat));
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void pere_fouettard(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
int i;
|
||||
int satiated;
|
||||
|
||||
while (philo->global->stop == 0)
|
||||
{
|
||||
i = 0;
|
||||
satiated = 1;
|
||||
gettimeofday(&stime, NULL);
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
if (satiated == 1 && is_satiated(philo) == 0)
|
||||
satiated = 0;
|
||||
if (is_finished(philo, &stime))
|
||||
break ;
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
usleep(1 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void launch(t_philo *philo, pthread_t *id)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
pere_fouettard(philo);
|
||||
}
|
||||
66
philo/main.c
Normal file
66
philo/main.c
Normal file
@@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:40 by hulamy #+# #+# */
|
||||
/* Updated: 2022/02/02 00:14:59 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void *return_free(void *e1, void *e2, void *e3)
|
||||
{
|
||||
if (e1)
|
||||
free(e1);
|
||||
if (e2)
|
||||
free(e2);
|
||||
if (e3)
|
||||
free(e3);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void free_philo(t_philo *philo, int n)
|
||||
{
|
||||
t_philo *tmp;
|
||||
int i;
|
||||
|
||||
if (philo && philo->params)
|
||||
free(philo->params);
|
||||
if (philo && philo->global)
|
||||
free(philo->global);
|
||||
i = 1;
|
||||
while (i <= n && philo)
|
||||
{
|
||||
if (i < n)
|
||||
tmp = philo->next;
|
||||
free(philo);
|
||||
if (i < n)
|
||||
philo = tmp;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pthread_t *id;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
|
||||
philo = init(ac, av, &id);
|
||||
if (philo == NULL)
|
||||
return (0);
|
||||
launch(philo, id);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_join(id[i], NULL);
|
||||
i++;
|
||||
}
|
||||
free_philo(philo, philo->params->n_phi);
|
||||
free(id);
|
||||
return (0);
|
||||
}
|
||||
27
philo/philo.h
Normal file
27
philo/philo.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:01 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:27:02 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <sys/time.h>
|
||||
|
||||
# include "philo_struct.h"
|
||||
# include "philo_proto.h"
|
||||
# include "philo_macro.h"
|
||||
|
||||
#endif
|
||||
36
philo/philo_macro.h
Normal file
36
philo/philo_macro.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_macro.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:08 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:27:09 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_MACRO_H
|
||||
# define PHILO_MACRO_H
|
||||
|
||||
# define GRAY "\e[0;30m"
|
||||
# define RED "\e[0;31m"
|
||||
# define GREEN "\e[0;32m"
|
||||
# define YELLOW "\e[0;33m"
|
||||
# define BLUE "\e[0;34m"
|
||||
# define PURPLE "\e[0;35m"
|
||||
# define CYAN "\e[0;36m"
|
||||
# define WHITE "\e[0;37m"
|
||||
|
||||
# define B_GRAY "\e[1;30m"
|
||||
# define B_RED "\e[1;31m"
|
||||
# define B_GREEN "\e[1;32m"
|
||||
# define B_YELLOW "\e[1;33m"
|
||||
# define B_BLUE "\e[1;34m"
|
||||
# define B_PURPLE "\e[1;35m"
|
||||
# define B_CYAN "\e[1;36m"
|
||||
# define B_WHITE "\e[1;37m"
|
||||
|
||||
# define RESET "\e[0m"
|
||||
|
||||
#endif
|
||||
41
philo/philo_proto.h
Normal file
41
philo/philo_proto.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_proto.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */
|
||||
/* Updated: 2022/02/02 00:15:43 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_PROTO_H
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// main.c
|
||||
void *return_free(void *e1, void *e2, void *e3);
|
||||
|
||||
// init.c
|
||||
t_philo *init(int ac, char **av, pthread_t **id);
|
||||
|
||||
// launch.c
|
||||
void launch(t_philo *philo, pthread_t *id);
|
||||
|
||||
// exec.c
|
||||
void *philo_exec(void *arg);
|
||||
|
||||
// generic.c
|
||||
void init_time(t_philo *philo);
|
||||
void update_time(t_philo *philo);
|
||||
int diff_time(t_philo *philo, struct timeval *new);
|
||||
int print_message(t_philo *philo, char *clr, char *msg);
|
||||
|
||||
// utils.c
|
||||
int ft_isdigit_2d_arr(char **str);
|
||||
size_t ft_strlen(char *str);
|
||||
int ft_strncmp(char *s1, char *s2, size_t n);
|
||||
int ft_int_overflow(char *str);
|
||||
int ft_atoi(const char *str);
|
||||
|
||||
#endif
|
||||
56
philo/philo_struct.h
Normal file
56
philo/philo_struct.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_struct.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/31 10:20:22 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_STRUCT_H
|
||||
# define PHILO_STRUCT_H
|
||||
|
||||
typedef pthread_mutex_t t_mtx;
|
||||
|
||||
typedef struct s_time
|
||||
{
|
||||
long int ts;
|
||||
long int tu;
|
||||
} t_time;
|
||||
|
||||
typedef struct s_params
|
||||
{
|
||||
int n_phi;
|
||||
int t_die;
|
||||
int t_eat;
|
||||
int t_slp;
|
||||
int n_eat;
|
||||
} t_params;
|
||||
|
||||
typedef struct s_global
|
||||
{
|
||||
int stop;
|
||||
int satiated_count;
|
||||
t_time t_start;
|
||||
t_mtx m_print;
|
||||
t_mtx m_init_time;
|
||||
t_mtx m_stop;
|
||||
} t_global;
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
t_params *params;
|
||||
t_global *global;
|
||||
int p_nbr;
|
||||
t_mtx m_fork;
|
||||
t_mtx m_time;
|
||||
t_mtx m_eat;
|
||||
t_time t_last_meal;
|
||||
int eat_count;
|
||||
struct s_philo *next;
|
||||
} t_philo;
|
||||
|
||||
#endif
|
||||
103
philo/utils.c
Normal file
103
philo/utils.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 18:04:08 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int ft_isdigit_2d_arr(char **str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
j = 0;
|
||||
while (str[i][j])
|
||||
{
|
||||
if (str[i][j] < '0' || str[i][j] > '9')
|
||||
return (0);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
size_t ft_strlen(char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_strncmp(char *s1, char *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
int res;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while (s1[i] && s1[i] == s2[i] && i < n - 1)
|
||||
i++;
|
||||
if (n != 0)
|
||||
res = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||
return (res);
|
||||
}
|
||||
|
||||
int ft_int_overflow(char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *int_xtrem;
|
||||
|
||||
int_xtrem = "2147483647";
|
||||
if (str[0] == '-')
|
||||
int_xtrem = "2147483648";
|
||||
if (str[0] == '+' || str[0] == '-')
|
||||
str++;
|
||||
len = ft_strlen(str);
|
||||
if (len < 10)
|
||||
return (0);
|
||||
else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
int is_negative;
|
||||
|
||||
is_negative = 0;
|
||||
result = 0;
|
||||
i = 0;
|
||||
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
if (str[i] == '+')
|
||||
i++;
|
||||
else if (str[i] == '-')
|
||||
{
|
||||
is_negative = 1;
|
||||
i++;
|
||||
}
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
result = (result * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
if (is_negative)
|
||||
result = result * -1;
|
||||
return (result);
|
||||
}
|
||||
82
srcs/exec.c
82
srcs/exec.c
@@ -1,82 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
void print_message(t_philo *philo, char *msg)
|
||||
{
|
||||
long int time_stamp;
|
||||
struct timeval stime;
|
||||
char *color;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
time_stamp = (stime.tv_sec - philo->t_start_s) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->t_start_u) / 1000;
|
||||
color = WHITE;
|
||||
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
|
||||
color = B_YELLOW;
|
||||
if (ft_strnstr(msg, "sleeping", ft_strlen(msg)))
|
||||
color = B_BLUE;
|
||||
if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
|
||||
color = B_GREEN;
|
||||
pthread_mutex_lock(philo->m_print);
|
||||
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);
|
||||
pthread_mutex_unlock(philo->m_print);
|
||||
}
|
||||
|
||||
// long int t_start_s;
|
||||
// long int t_start_u;
|
||||
// long int t_last_meal_s;
|
||||
// long int t_last_meal_u;
|
||||
void init_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->t_start_s = stime.tv_sec;
|
||||
philo->t_start_u = stime.tv_usec;
|
||||
}
|
||||
|
||||
void update_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->t_last_meal_s = stime.tv_sec;
|
||||
philo->t_last_meal_u = stime.tv_usec;
|
||||
}
|
||||
|
||||
void take_forks(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
print_message(philo, "has taken a fork");
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
print_message(philo, "has taken a fork");
|
||||
|
||||
print_message(philo, "is eating");
|
||||
usleep(philo->params->t_eat * 1000);
|
||||
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
}
|
||||
|
||||
// int n_phi; // number_of_philosophers
|
||||
// int t_die; // time_to_die
|
||||
// int t_eat; // time_to_eat
|
||||
// int t_slp; // time_to_sleep
|
||||
// int n_eat; // [number_of_times_each_philosopher_must_eat]
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo*)arg;
|
||||
init_time(philo);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
take_forks(philo);
|
||||
|
||||
print_message(philo, "is sleeping");
|
||||
usleep(philo->params->t_slp * 1000);
|
||||
print_message(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
81
srcs/init.c
81
srcs/init.c
@@ -1,81 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i)
|
||||
{
|
||||
t_philo *new;
|
||||
|
||||
new = malloc(sizeof(t_philo) * 1);
|
||||
if (new == NULL)
|
||||
return (NULL);
|
||||
new->params = params;
|
||||
new->p_nbr = i + 1;
|
||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||
return (NULL);
|
||||
new->m_print = m_print;
|
||||
if (philo)
|
||||
philo->next = new;
|
||||
new->prev = philo;
|
||||
return (new);
|
||||
}
|
||||
|
||||
// looping chained list
|
||||
t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
||||
{
|
||||
t_philo *end;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
philo = lst_add_philo(philo, params, m_print, i);
|
||||
i++;
|
||||
}
|
||||
end = philo;
|
||||
while (philo->prev != NULL)
|
||||
philo = philo->prev;
|
||||
philo->prev = end;
|
||||
end->next = philo;
|
||||
return (philo);
|
||||
}
|
||||
|
||||
t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_params *params;
|
||||
|
||||
if (ac == 5 || ac == 6)
|
||||
{
|
||||
params = malloc(sizeof(t_params));
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
}
|
||||
else
|
||||
return (NULL);
|
||||
return (params);
|
||||
}
|
||||
|
||||
t_philo *init(int ac, char **av, pthread_t **id)
|
||||
{
|
||||
t_philo *philo;
|
||||
t_params *params;
|
||||
t_mtx *m_print;
|
||||
|
||||
params = init_params(ac, av);
|
||||
if (params == NULL)
|
||||
return (NULL);
|
||||
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||
if (*id == NULL)
|
||||
return (NULL);
|
||||
m_print = malloc(sizeof(t_mtx));
|
||||
if (pthread_mutex_init(m_print, NULL) != 0)
|
||||
return (NULL);
|
||||
philo = init_chain_philo(params, m_print);
|
||||
if (philo == NULL)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
}
|
||||
36
srcs/main.c
36
srcs/main.c
@@ -1,36 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
void launch_threads(t_philo *philo, pthread_t *id, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pthread_t *id;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
int n;
|
||||
|
||||
philo = init(ac, av, &id);
|
||||
if (philo == NULL)
|
||||
return (0);
|
||||
n = philo->params->n_phi;
|
||||
launch_threads(philo, id, n);
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
pthread_join(id[i], NULL);
|
||||
i++;
|
||||
}
|
||||
write(1, "main function\n", 14);
|
||||
return (0);
|
||||
}
|
||||
23
srcs/main0.c
23
srcs/main0.c
@@ -1,23 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo*)arg;
|
||||
while (1)
|
||||
write(1, philo->str, ft_strlen(philo->str));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
pthread_t id;
|
||||
t_philo *philo;
|
||||
|
||||
philo = malloc(sizeof(philo));
|
||||
philo->str = "i'm philosopher\n";
|
||||
pthread_create(&id, NULL, &philo_exec, philo);
|
||||
while (1)
|
||||
write(1, "main function\n", 14);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo*)arg;
|
||||
while (1)
|
||||
write(1, philo->str, ft_strlen(philo->str));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
pthread_t id;
|
||||
t_philo *philo;
|
||||
|
||||
philo = malloc(sizeof(philo));
|
||||
philo->str = "i'm philosopher\n";
|
||||
pthread_create(&id, NULL, &philo_exec, philo);
|
||||
pthread_join(id, NULL);
|
||||
while (1)
|
||||
write(1, "main function\n", 14);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo*)arg;
|
||||
while (1)
|
||||
{
|
||||
write(1, philo->str, ft_strlen(philo->str));
|
||||
write(1, " nb ", 4);
|
||||
ft_putnbr_fd(philo->nbr, 1);
|
||||
write(1, "\n", 1);
|
||||
sleep (1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
pthread_t *tid;
|
||||
int n_philo;
|
||||
int i;
|
||||
t_philo *philo;
|
||||
|
||||
n_philo = 3;
|
||||
philo = malloc(sizeof(philo) * n_philo);
|
||||
tid = malloc(sizeof(pthread_t) * n_philo);
|
||||
i = 0;
|
||||
while (i < n_philo)
|
||||
{
|
||||
philo[i].str = "i'm philosopher";
|
||||
philo[i].nbr = i;
|
||||
pthread_create(&tid[i], NULL, &philo_exec, &philo[i]);
|
||||
i++;
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
write(1, "main function\n", 14);
|
||||
sleep (1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
#include "philo.h"
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
philo = (t_philo*)arg;
|
||||
while (1)
|
||||
{
|
||||
write(1, philo->str, ft_strlen(philo->str));
|
||||
write(1, " nb ", 4);
|
||||
ft_putnbr_fd(philo->nbr, 1);
|
||||
write(1, "\n", 1);
|
||||
sleep (1);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
pthread_t *tid;
|
||||
int n_philo;
|
||||
int i;
|
||||
t_philo *philo;
|
||||
|
||||
n_philo = 3;
|
||||
philo = malloc(sizeof(philo) * n_philo);
|
||||
tid = malloc(sizeof(pthread_t) * n_philo);
|
||||
i = 0;
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
while (i < n_philo)
|
||||
{
|
||||
philo[i].str = "i'm philosopher";
|
||||
philo[i].nbr = i;
|
||||
pthread_create(&tid[i], NULL, &philo_exec, &philo[i]);
|
||||
i++;
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
write(1, "main function\n", 14);
|
||||
sleep (1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user