commit eee4d08902b8b4c64cb030404f3a2d411497da03 Author: kinou-p Date: Sun Feb 27 15:32:03 2022 +0100 first diff --git a/main.c b/main.c new file mode 100644 index 0000000..4436c16 --- /dev/null +++ b/main.c @@ -0,0 +1,159 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/20 19:10:43 by apommier #+# #+# */ +/* Updated: 2022/02/20 19:10:43 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philosophers.h" + +s_arg *create_data(int argc, char **argv) +{ + s_arg *data; + + data = malloc(sizeof(s_arg)); + *data->death = 1; + data->nb_philo = ft_atoi(argv[1]); + data->time_to_die = ft_atoi(argv[2]); + data->time_to_eat = ft_atoi(argv[3]); + data->time_to_sleep = ft_atoi(argv[4]); + data->must_eat = 0; + if (argc == 6) + data->must_eat = ft_atoi(argv[5]); + else + data->must_eat = -1; + //data->time_start = get_time(); + return (data); +} + +void put_event(s_philo philo, char *event) +{ + printf("%.4ldms Philo N°%d %s\n", get_time() - philo.data->time_start, philo.philo_id, event); +} + +void philo_eat(s_philo *philo_tmp) +{ + s_philo philo; + + philo = *philo_tmp; + pthread_mutex_lock(philo.right_fork); + put_event(philo, "has taken a fork"); + pthread_mutex_lock(philo.left_fork); + put_event(philo, "has taken a fork"); + printf("old last eat of --%d-- = %ld\n", philo.philo_id, *philo_tmp->last_eat - philo.data->time_start); + *philo_tmp->last_eat = get_time(); + printf("last eat of --%d-- = %ld\n", philo.philo_id, *philo_tmp->last_eat - philo.data->time_start); + put_event(philo, "is eating"); + usleep(philo.data->time_to_eat * 1000); + pthread_mutex_unlock(philo.right_fork); + pthread_mutex_unlock(philo.left_fork); + put_event(philo, "is sleeping"); + usleep(philo.data->time_to_sleep * 1000); + put_event(philo, "is thinking"); +} + +void *death_check(void *tmp) +{ + s_philo *philo; + + philo = (s_philo *)tmp; + while (*philo->data->death) + { + if (get_time() - *philo->last_eat > philo->data->time_to_die) + { + printf("PHILO IS DEAD last eat: %ld-- ", *philo->last_eat - philo->data->time_start); + put_event(*philo, "PHILO IS DEAD\n\n\n--\n"); + exit(1); + } + } + return (0); +} + +void *routine(void *tmp) +{ + s_philo *philo; + pthread_t death; + + philo = (s_philo *)tmp; + + *philo->last_eat = get_time(); + pthread_create(&death, 0, &death_check, philo); + pthread_detach(death); + while (1 && *philo->data->death && philo->must_eat) + { + philo_eat(philo); + philo->must_eat--; + } + return (0); +} + +s_philo *create_philo(s_arg *data) +{ + int i; + s_philo *philo; + + i = 0; + philo = malloc(sizeof(s_philo) * data->nb_philo); + if (!philo) + return (0); + while (i < data->nb_philo) + { + philo[i].philo_id = i + 1; + philo[i].data = data; + philo[i].must_eat = data->must_eat; + philo[i].left_fork = malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(philo[i].left_fork, 0); + i++; + } + i = 0; + while (i < data->nb_philo - 1) + { + philo[i].right_fork = philo[i + 1].left_fork; + i++; + } + philo[i].left_fork = philo[0].right_fork; + return (philo); +} + +int start_philo(s_philo *philo) +{ + int i; + + i = 0; + philo->data->time_start = get_time(); + while (i < philo[i].data->nb_philo - 1) + { + pthread_create(&philo[i].thread, 0, &routine, &philo[i]); + usleep(10); + i++; + } + i = 0; + while (i < philo[i].data->nb_philo - 1) + { + pthread_join(philo[i].thread, 0); + } +} + +int main(int argc, char **argv) +{ + s_arg *data; + s_philo *philo; + + get_time(); + data = create_data(argc, argv); + philo = create_philo(data); + start_philo(philo); +} + + + + //put_event(*philo, "starting"); + //if ( philo->philo_id == 3) + // *philo->data->test = 0; + //printf("je suis le numero %d\n", philo->philo_id); + //printf("test = %d\n", *philo->data->test); \ No newline at end of file diff --git a/philosophers.h b/philosophers.h new file mode 100644 index 0000000..131459b --- /dev/null +++ b/philosophers.h @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philosophers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/20 19:10:49 by apommier #+# #+# */ +/* Updated: 2022/02/20 19:10:49 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +typedef struct s_list +{ + int nb_philo; + int time_to_die; + int time_to_eat; + int time_to_sleep; + int must_eat; + long time_start; + int death[1]; +} s_arg; + +typedef struct t_list +{ + int philo_id; + pthread_t thread; + s_arg *data; + int must_eat; + long last_eat[1]; + pthread_mutex_t *right_fork; + pthread_mutex_t *left_fork; + +} s_philo; + +//utils fonctions +long ft_atoi(const char *nptr); +long get_time(void); \ No newline at end of file diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..5915cb3 --- /dev/null +++ b/utils.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/25 03:28:46 by apommier #+# #+# */ +/* Updated: 2022/02/25 03:28:46 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philosophers.h" + +long ft_atoi(const char *nptr) +{ + int i; + long nbr; + long minus; + + minus = 1; + nbr = 0; + i = 0; + while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32) + i++; + if (nptr[i] == '+') + i++; + else if (nptr[i] == '-') + { + i++; + minus = -1; + } + while (nptr[i] >= '0' && nptr[i] <= '9') + { + nbr = nbr * 10 + nptr[i] - '0'; + i++; + } + return (minus * nbr); +} + +long get_time(void) +{ + struct timeval time; + + gettimeofday(&time, NULL); + //printf("time : %ld\n", time.tv_sec * 1000 + time.tv_usec / 1000); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} \ No newline at end of file