-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiningPhilosopers.c
98 lines (79 loc) · 3.01 KB
/
diningPhilosopers.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 5
pthread_mutex_t lock[5];
// The function executed by each thread
void *Philosopher(void *threadid) {
int tid;
tid = (int)threadid;
int left = tid;
int right = (tid + 1) % 5;
while(true) {
int randThink = rand() % 20;
int randEat = rand() % 20;
printf("I'm philosopher %d and I'm thinking for %d\n", tid, randThink);
sleep(randThink);
printf("I'm philosopher %d and I'm finished thinking, waiting to eat\n", tid);
switch (tid % 2) {
case 0:
pthread_mutex_lock(&lock[left]);
// printf("I'm philosopher %d and I picked up my left spoon\n", tid);
pthread_mutex_lock(&lock[right]);
// printf("I'm philosopher %d and I picked up my right spoon\n", tid);
break;
default:
pthread_mutex_lock(&lock[right]);
// printf("I'm philosopher %d and I picked up my right spoon\n", tid);
pthread_mutex_lock(&lock[left]);
// printf("I'm philosopher %d and I picked up my left spoon\n", tid);
}
// Eat using the spoons
printf("I'm philosopher %d and I'm eating for %d\n", tid, randEat);
sleep(randEat);
printf("I'm philosopher %d and I finished eating\n", tid);
switch (tid % 2) {
case 0:
pthread_mutex_unlock(&lock[left]);
// printf("I'm philosopher %d and I put down my left spoon\n", tid);
pthread_mutex_unlock(&lock[right]);
// printf("I'm philosopher %d and I put down my right spoon\n", tid);
break;
default:
pthread_mutex_unlock(&lock[right]);
// printf("I'm philosopher %d and I put down my right spoon\n", tid);
pthread_mutex_unlock(&lock[left]);
// printf("I'm philosopher %d and I put down my left spoon\n", tid);
}
}
// Terminate thread
pthread_exit(NULL);
}
// Main Function
int main (int argc, char *argv[]) {
srand(time(NULL));
// Will create 5 threads, so will need a reference to each
pthread_t threads[NUM_THREADS];
long n;
for(n = 0; n < NUM_THREADS; ++n) {
// Print
if (pthread_mutex_init(&lock[n], NULL) != 0) {
printf("\n mutex init has failed\n");
return 1;
}
printf("In main: creating thread Philosopher %ld\n", n);
// Create a thread here, and get the thread to run the PrintHello
// function. Note the way the parameters are passed
int rc = pthread_create(&threads[n], NULL, Philosopher, (void *)n);
// A non-zero return code means something went wrong
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
// Useful to call this in the main to ensure that all threads are kept alive.
// Otherwise threads that are still going will be automatically terminated
pthread_exit(NULL);
}