source: CIVL/examples/concurrency/dining.cvl@ 397e220

1.23 2.0 main test-branch
Last change on this file since 397e220 was 3ff27cf, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

updated examples since $assert/$assume has been changed to functions; fixed the model builder for the new side-effect remover.

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

  • Property mode set to 100644
File size: 1.1 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; i < n; i++) forks[i] = -1;
37}
38
39void main() {
40 $proc philosophers[n];
41
42 init();
43 for (int i = 0; i < n; i++)
44 philosophers[i] = $spawn dine(i);
45 for (int i = 0; i < n; i++)
46 $wait(philosophers[i]);
47}
Note: See TracBrowser for help on using the repository browser.