-
Notifications
You must be signed in to change notification settings - Fork 0
/
philosopher.pml
48 lines (36 loc) · 1.25 KB
/
philosopher.pml
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
/* Dining Philosophers Problem */
/*
Student 1:Adam Wendelin
Student 2:Victoria Catalán
*/
/*
Our assertion says that you have to "own" the fork to use it. Nobody should be able to grab the fork in your hand, while you're about to eat.
We originally had a deadlock when every philosopher grabbed the fork to the right. We solved it by making the philosopher grab the fork with the lowest index instead.
Spin shows no deadlocks anymore.
*/
#define n 5
int cutlery[n] = -1;
proctype phil(int id) {
int left=id; int right = (id +1) % n;
Think:
printf("Philosopher %d is thinking\n", id);
if :: right < left;
atomic{cutlery[right] == -1 -> cutlery[right] = id};
atomic{cutlery[left] == -1 -> cutlery[left] = id};
:: left<right;
atomic{cutlery[left] == -1 -> cutlery[left] = id};
atomic{cutlery[right] == -1 -> cutlery[right] = id};
fi;
Eat: assert (cutlery[right] == id && cutlery[left] == id);
printf("Philosopher %d is eating\n", id);
Stop: cutlery[left] = -1; cutlery[right] = -1;
goto Think;
}
init{
int i = 0;
do
:: i>= n -> break
:: else -> run phil(i);
i++
od
}