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

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

cleaned up examples and tests.

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

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