chaque philosophe peut imprimer son num

This commit is contained in:
hugogogo
2021-12-12 10:28:34 +01:00
parent fc987249ac
commit 2294c73e6a
3 changed files with 34 additions and 18 deletions

View File

@@ -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`

View File

@@ -11,6 +11,7 @@
typedef struct s_philo
{
char *str;
int nbr;
} t_philo;
#endif

View File

@@ -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)