main
test-branch
| Line | |
|---|
| 1 | /* Dining philosophers with fork switching. Even philosophers pick
|
|---|
| 2 | * up left fork first, odd pick up right first. This breaks deadlock.
|
|---|
| 3 | *
|
|---|
| 4 | * civl verify -inputBOUND=4 dining.cvl
|
|---|
| 5 | */
|
|---|
| 6 | #include <civlc.cvh>
|
|---|
| 7 | #include <domain.cvh>
|
|---|
| 8 |
|
|---|
| 9 | $input int BOUND = 4; // upper bound on number of philosophers
|
|---|
| 10 | $input int n; // number of philosophers
|
|---|
| 11 | $assume(2<=n && n<=BOUND);
|
|---|
| 12 |
|
|---|
| 13 | // Each fork will be on the table (-1) or in a hand (0-(n-1)).
|
|---|
| 14 | int forks[n];
|
|---|
| 15 |
|
|---|
| 16 | void dine(int id) {
|
|---|
| 17 | int left = id;
|
|---|
| 18 | int right = (id + 1) % n;
|
|---|
| 19 |
|
|---|
| 20 | while (1) {
|
|---|
| 21 | if (id % 2 == 0) {
|
|---|
| 22 | $when (forks[left] < 0) {forks[left] = id;}
|
|---|
| 23 | $when (forks[right] < 0) {forks[right] = id;}
|
|---|
| 24 | forks[right] = -1;
|
|---|
| 25 | forks[left] = -1;
|
|---|
| 26 | } else {
|
|---|
| 27 | $when (forks[right] < 0) {forks[right] = id;}
|
|---|
| 28 | $when (forks[left] < 0) {forks[left] = id;}
|
|---|
| 29 | forks[right] = -1;
|
|---|
| 30 | forks[left] = -1;
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /* Put all forks on the table. */
|
|---|
| 36 | void init() {
|
|---|
| 37 | $for (int i : 0 .. n-1)
|
|---|
| 38 | forks[i] = -1;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void main() {
|
|---|
| 42 | init();
|
|---|
| 43 | $parfor(int i: 0 .. n-1)
|
|---|
| 44 | dine(i);
|
|---|
| 45 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.