-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads_mutexes.c
45 lines (39 loc) · 1.1 KB
/
threads_mutexes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
pthread_mutex_t chopsticks[NUM_PHILOSOPHERS];
void *philosopher(void *num) {
int id;
id = *(int*)num;
while(1)
{
printf("Philosopher %d is thinking\n", id);
sleep(rand() % 3);
printf("Philosopher %d is hungry\n", id);
pthread_mutex_lock(&chopsticks[id]);
pthread_mutex_lock(&chopsticks[(id + 1) % NUM_PHILOSOPHERS]);
printf("Philosopher %d is eating\n", id);
sleep(rand() % 3);
pthread_mutex_unlock(&chopsticks[id]);
pthread_mutex_unlock(&chopsticks[(id + 1) % NUM_PHILOSOPHERS]);
}
}
int main(void)
{
int i;
int nums[NUM_PHILOSOPHERS];
pthread_t philosophers[NUM_PHILOSOPHERS];
for(i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_mutex_init(&chopsticks[i], NULL);
for(i = 0; i < NUM_PHILOSOPHERS; i++)
{
nums[i] = i;
pthread_create(&philosophers[i], NULL, philosopher, &nums[i]);
}
for(i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_join(philosophers[i], NULL);
for(i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_mutex_destroy(&chopsticks[i]);
return (0);
}