-
Notifications
You must be signed in to change notification settings - Fork 0
/
philosophers.c
105 lines (81 loc) · 1.76 KB
/
philosophers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <semaphore.h>
#define phil_n 5
#define max_dur_eating 4
#define max_dur_thought 3
void *philosopher(void *args);
void take_forks(int id);
void put_forks(int id);
void think(int id);
void eat(int id);
pthread_mutex_t forks[phil_n];
sem_t waiter;
int main()
{
int args[phil_n] = {};
pthread_t phil[phil_n] = {};
if (sem_init(&waiter, 0, phil_n - 1) < 0)
{
perror("sem_init() fail");
exit(EXIT_FAILURE);
}
for (int i = 0; i < phil_n; i++)
{
args[i] = i;
if (pthread_create(&phil[i], NULL, philosopher, &args[i]) != 0)
{
fprintf(stderr, "pthread_create() fail\n");
exit(EXIT_FAILURE);
}
}
// Never happens
for (int i = 0; i < phil_n; i++)
pthread_join(phil[i], (void **) NULL);
return 0;
}
void *philosopher(void *args)
{
assert(args);
int id = *(int *) args;
while (1)
{
think(id);
take_forks(id);
eat(id);
put_forks(id);
}
}
void take_forks(int id)
{
if (sem_wait(&waiter) < 0)
{
perror("sem_wait() fail");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&forks[id]);
pthread_mutex_lock(&forks[(id + 1) % phil_n]);
}
void put_forks(int id)
{
pthread_mutex_unlock(&forks[(id + 1) % phil_n]);
pthread_mutex_unlock(&forks[id]);
if (sem_post(&waiter) < 0)
{
perror("sem_post() fail");
exit(EXIT_FAILURE);
}
}
void think(int id)
{
printf("Philosopher № %d is thinking\n", id);
sleep(rand() % (max_dur_thought + 1));
}
void eat(int id)
{
printf("Philosopher № %d is eating\n", id);
sleep(rand() % (max_dur_eating + 1));
}