chaque philosophe peut imprimer son num
This commit is contained in:
28
README.md
28
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`
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
typedef struct s_philo
|
||||
{
|
||||
char *str;
|
||||
int nbr;
|
||||
} t_philo;
|
||||
|
||||
#endif
|
||||
|
||||
23
srcs/main.c
23
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)
|
||||
|
||||
Reference in New Issue
Block a user