main
|
Last change
on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
984 bytes
|
| 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 n = 4; // number of philosophers
|
|---|
| 10 |
|
|---|
| 11 | // Each fork will be on the table (-1) or in a hand (0-(n-1)).
|
|---|
| 12 | int forks[n];
|
|---|
| 13 |
|
|---|
| 14 | void dine(int id) {
|
|---|
| 15 | int left = id;
|
|---|
| 16 | int right = (id + 1) % n;
|
|---|
| 17 |
|
|---|
| 18 | while (1) {
|
|---|
| 19 | if (id % 2 == 0) {
|
|---|
| 20 | $when (forks[left] < 0) {forks[left] = id;}
|
|---|
| 21 | $when (forks[right] < 0) {forks[right] = id;}
|
|---|
| 22 | forks[right] = -1;
|
|---|
| 23 | forks[left] = -1;
|
|---|
| 24 | } else {
|
|---|
| 25 | $when (forks[right] < 0) {forks[right] = id;}
|
|---|
| 26 | $when (forks[left] < 0) {forks[left] = id;}
|
|---|
| 27 | forks[right] = -1;
|
|---|
| 28 | forks[left] = -1;
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /* Put all forks on the table. */
|
|---|
| 34 | void init() {
|
|---|
| 35 | $for (int i : 0 .. n-1)
|
|---|
| 36 | forks[i] = -1;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | void main() {
|
|---|
| 40 | init();
|
|---|
| 41 | $run dine(0);
|
|---|
| 42 | $run dine(1);
|
|---|
| 43 | $run dine(2);
|
|---|
| 44 | $run dine(3);
|
|---|
| 45 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.