From 2294c73e6a3384929c8df97b52b4d9d59837c840 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 12 Dec 2021 10:28:34 +0100 Subject: [PATCH] chaque philosophe peut imprimer son num --- README.md | 28 ++++++++++++++-------------- headers/philo.h | 1 + srcs/main.c | 23 +++++++++++++++++++---- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 334ef4c..32f6d4e 100644 --- a/README.md +++ b/README.md @@ -29,18 +29,18 @@ thread : external function : -memset : fill memory with a constant byte -printf : format and print data -malloc : allocate dynamic memory -free : free dynamic memory -write : write to a file descriptor -usleep : suspend execution for microseconds intervals -gettimeofday : get time -pthread_create : create a new thread -pthread_detach : -pthread_join -pthread_mutex_init -pthread_mutex_destroy -pthread_mutex_lock -pthread_mutex_unlock +- `memset` : fill memory with a constant byte +- `printf` : format and print data +- `malloc` : allocate dynamic memory +- `free` : free dynamic memory +- `write` : write to a file descriptor +- `usleep` : suspend execution for microseconds intervals +- `gettimeofday` : get time +- `pthread_create` : create a new thread +- `pthread_detach` : +- `pthread_join` +- `pthread_mutex_init` +- `pthread_mutex_destroy` +- `pthread_mutex_lock` +- `pthread_mutex_unlock` diff --git a/headers/philo.h b/headers/philo.h index e052c10..3977202 100644 --- a/headers/philo.h +++ b/headers/philo.h @@ -11,6 +11,7 @@ typedef struct s_philo { char *str; + int nbr; } t_philo; #endif diff --git a/srcs/main.c b/srcs/main.c index f5fc7e1..55fc1c6 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,18 +6,33 @@ void *philo_exec(void *arg) 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); + } } int main(void) { - pthread_t id; + pthread_t *tid; int ret; + int n_philo; + int i; t_philo *philo; - philo = malloc(sizeof(philo)); - philo->str = "i'm philosopher\n"; - ret = pthread_create(&id, NULL, &philo_exec, 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; + ret = pthread_create(&tid[i], NULL, &philo_exec, &philo[i]); + i++; + } if (ret == 0) { while (1)