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

main test-branch
Last change on this file since bb03188 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: 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#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)).
14int forks[n];
15
16void 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. */
36void init() {
37 $for (int i : 0 .. n-1)
38 forks[i] = -1;
39}
40
41void main() {
42 init();
43 $parfor(int i: 0 .. n-1)
44 dine(i);
45}
Note: See TracBrowser for help on using the repository browser.