source: CIVL/examples/concurrency/dining.cvl@ ea8d8ce

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since ea8d8ce was 2321281, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

fixed $elaborate for examples.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2355 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.0 KB
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
8$input int BOUND = 4; // upper bound on number of philosophers
9$input int n; // number of philosophers
10$assume(2<=n && n<=BOUND);
11
12// Each fork will be on the table (-1) or in a hand (0-(n-1)).
13int forks[n];
14
15void dine(int id) {
16 int left = id;
17 int right = (id + 1) % n;
18
19 while (1) {
20 if (id % 2 == 0) {
21 $when (forks[left] < 0) {forks[left] = id;}
22 $when (forks[right] < 0) {forks[right] = id;}
23 forks[right] = -1;
24 forks[left] = -1;
25 } else {
26 $when (forks[right] < 0) {forks[right] = id;}
27 $when (forks[left] < 0) {forks[left] = id;}
28 forks[right] = -1;
29 forks[left] = -1;
30 }
31 }
32}
33
34/* Put all forks on the table. */
35void init() {
36 $for (int i : 0 .. n-1)
37 forks[i] = -1;
38}
39
40void main() {
41 $elaborate(n);
42 init();
43 $parfor(int i: 0 .. n-1)
44 dine(i);
45}
Note: See TracBrowser for help on using the repository browser.