source:
CIVL/examples/concurrency/dining.cvl
| Last change on this file was ea777aa, checked in by , 3 years ago | |
|---|---|
|
|
| File size: 1.0 KB | |
| Rev | Line | |
|---|---|---|
| [72c01cc] | 1 | /* Dining philosophers with fork switching. Even philosophers pick |
| 2 | * up left fork first, odd pick up right first. This breaks deadlock. | |
| 3 | * | |
| [69bf2e6] | 4 | * civl verify -inputBOUND=4 dining.cvl |
| [72c01cc] | 5 | */ |
| [e6b02c8] | 6 | #include <civlc.cvh> |
| [7b92a9d] | 7 | #include <domain.cvh> |
| [793cfc2] | 8 | |
| [0baeebd] | 9 | $input int BOUND = 4; // upper bound on number of philosophers |
| 10 | $input int n; // number of philosophers | |
| [3ff27cf] | 11 | $assume(2<=n && n<=BOUND); |
| [72c01cc] | 12 | |
| [1de5580] | 13 | // Each fork will be on the table (-1) or in a hand (0-(n-1)). |
| [72c01cc] | 14 | int forks[n]; |
| [793cfc2] | 15 | |
| 16 | void dine(int id) { | |
| 17 | int left = id; | |
| 18 | int right = (id + 1) % n; | |
| [72c01cc] | 19 | |
| 20 | while (1) { | |
| [793cfc2] | 21 | if (id % 2 == 0) { |
| [1de5580] | 22 | $when (forks[left] < 0) {forks[left] = id;} |
| 23 | $when (forks[right] < 0) {forks[right] = id;} | |
| 24 | forks[right] = -1; | |
| 25 | forks[left] = -1; | |
| [793cfc2] | 26 | } else { |
| [1de5580] | 27 | $when (forks[right] < 0) {forks[right] = id;} |
| 28 | $when (forks[left] < 0) {forks[left] = id;} | |
| 29 | forks[right] = -1; | |
| 30 | forks[left] = -1; | |
| [793cfc2] | 31 | } |
| [05ae5d1] | 32 | } |
| [793cfc2] | 33 | } |
| 34 | ||
| [72c01cc] | 35 | /* Put all forks on the table. */ |
| [793cfc2] | 36 | void init() { |
| [7b9eb4c] | 37 | $for (int i : 0 .. n-1) |
| 38 | forks[i] = -1; | |
| [793cfc2] | 39 | } |
| 40 | ||
| [7b9eb4c] | 41 | void main() { |
| [17f5ed1] | 42 | init(); |
| [7b9eb4c] | 43 | $parfor(int i: 0 .. n-1) |
| 44 | dine(i); | |
| [793cfc2] | 45 | } |
Note:
See TracBrowser
for help on using the repository browser.
