chaque philosophe peut imprimer son num
This commit is contained in:
28
README.md
28
README.md
@@ -29,18 +29,18 @@ thread :
|
|||||||
|
|
||||||
external function :
|
external function :
|
||||||
|
|
||||||
memset : fill memory with a constant byte
|
- `memset` : fill memory with a constant byte
|
||||||
printf : format and print data
|
- `printf` : format and print data
|
||||||
malloc : allocate dynamic memory
|
- `malloc` : allocate dynamic memory
|
||||||
free : free dynamic memory
|
- `free` : free dynamic memory
|
||||||
write : write to a file descriptor
|
- `write` : write to a file descriptor
|
||||||
usleep : suspend execution for microseconds intervals
|
- `usleep` : suspend execution for microseconds intervals
|
||||||
gettimeofday : get time
|
- `gettimeofday` : get time
|
||||||
pthread_create : create a new thread
|
- `pthread_create` : create a new thread
|
||||||
pthread_detach :
|
- `pthread_detach` :
|
||||||
pthread_join
|
- `pthread_join`
|
||||||
pthread_mutex_init
|
- `pthread_mutex_init`
|
||||||
pthread_mutex_destroy
|
- `pthread_mutex_destroy`
|
||||||
pthread_mutex_lock
|
- `pthread_mutex_lock`
|
||||||
pthread_mutex_unlock
|
- `pthread_mutex_unlock`
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
typedef struct s_philo
|
typedef struct s_philo
|
||||||
{
|
{
|
||||||
char *str;
|
char *str;
|
||||||
|
int nbr;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
23
srcs/main.c
23
srcs/main.c
@@ -6,18 +6,33 @@ void *philo_exec(void *arg)
|
|||||||
|
|
||||||
philo = (t_philo*)arg;
|
philo = (t_philo*)arg;
|
||||||
while (1)
|
while (1)
|
||||||
|
{
|
||||||
write(1, philo->str, ft_strlen(philo->str));
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
pthread_t id;
|
pthread_t *tid;
|
||||||
int ret;
|
int ret;
|
||||||
|
int n_philo;
|
||||||
|
int i;
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
|
||||||
philo = malloc(sizeof(philo));
|
n_philo = 3;
|
||||||
philo->str = "i'm philosopher\n";
|
philo = malloc(sizeof(philo) * n_philo);
|
||||||
ret = pthread_create(&id, NULL, &philo_exec, 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)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
|
|||||||
Reference in New Issue
Block a user