Add
This commit is contained in:
parent
2be4d454f9
commit
e88ec90f41
|
@ -6,16 +6,9 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:43:51 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/12 16:54:49 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/13 13:43:32 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
void *philosopher(t_philo *philo)
|
||||
{
|
||||
write(1, "Hey i'm ", 8);
|
||||
ft_putnbr_fd(philo->id, 1);
|
||||
write(1, "\n", 1);
|
||||
return (NULL);
|
||||
}
|
||||
|
|
|
@ -6,13 +6,19 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/12 16:56:46 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/13 14:01:59 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include "../utils/utils.h"
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include <sys/wait.h>
|
||||
|
||||
typedef struct s_config
|
||||
{
|
||||
int nbr_philo;
|
||||
|
@ -25,15 +31,10 @@ typedef struct s_config
|
|||
typedef struct s_philo
|
||||
{
|
||||
t_config conf;
|
||||
int id;
|
||||
pthread_mutex_t mutex;
|
||||
int *id;
|
||||
} t_philo;
|
||||
|
||||
# include "../utils/utils.h"
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
# include<sys/wait.h>
|
||||
|
||||
int parsing(char *argv[], t_config *conf);
|
||||
int manage_threads(t_config *conf);
|
||||
void *philosopher(t_philo *philo);
|
||||
|
|
|
@ -6,33 +6,50 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/12 16:57:52 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/03/13 18:33:50 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
#include <pthread.h>
|
||||
|
||||
void *philosopher(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&philo->mutex);
|
||||
write(1, "Hey i'm ", 8);
|
||||
ft_putnbr_fd(*philo->id, 1);
|
||||
write(1, "\n", 1);
|
||||
pthread_mutex_unlock(&philo->mutex);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int manage_threads(t_config *config)
|
||||
{
|
||||
pthread_t *threads;
|
||||
t_philo philo;
|
||||
pthread_mutex_t mutex_first_fork;
|
||||
pthread_mutex_t mutex_second_fork;
|
||||
pthread_mutex_t mutex_dead;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
|
||||
threads = malloc(config->nbr_philo * sizeof(pthread_t));
|
||||
if (threads == NULL)
|
||||
return (1);
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
philo = ft_calloc(sizeof(t_philo), 1);
|
||||
if (!philo)
|
||||
return (1);
|
||||
philo->conf = *config;
|
||||
philo->mutex = mutex;
|
||||
i = -1;
|
||||
philo.conf = *config;
|
||||
while (++i < config->nbr_philo)
|
||||
{
|
||||
philo.id = i;
|
||||
if (pthread_create(&threads[i], NULL, (void *)&philosopher, &philo) != 0)
|
||||
philo->id = &i;
|
||||
if (pthread_create(&threads[i], NULL, (void *)&philosopher, philo) != 0)
|
||||
{
|
||||
write(2, "Error thread creation\n", 21);
|
||||
return (1);
|
||||
}
|
||||
write(1, "Thread created\n", 15);
|
||||
}
|
||||
while (--i > -1)
|
||||
{
|
||||
|
@ -41,7 +58,7 @@ int manage_threads(t_config *config)
|
|||
write(2, "Error thread finish\n", 20);
|
||||
return (1);
|
||||
}
|
||||
write(1, "Thread finish execution\n", 24);
|
||||
}
|
||||
pthread_mutex_destroy(&mutex);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@
|
|||
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# #
|
||||
# Updated: 2023/03/08 21:29:13 by erey-bet ### ########.fr #
|
||||
# Updated: 2023/03/13 14:03:30 by erey-bet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = ft_atoi.c ft_calloc.c ft_putstr_fd.c ft_putnbr_fd.c ft_atoi_check.c \
|
||||
ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c
|
||||
ft_calloc.c ft_memset.c ft_putchar_fd.c ft_strlen.c ft_bzero.c
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = clang
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
|
Loading…
Reference in a new issue