source: CIVL/examples/concurrency/dining.cvl@ 23207c6

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 23207c6 was 24ca21c, checked in by Manchun Zheng <zmanchun@…>, 13 years ago

improve output. Add atomic to some examples.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@391 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 -inputB=4 dining.cvl
5 */
6#include <civlc.h>
7
8$input int B; // upper bound on number of philosophers
9$input int n; // number of philosophers
10$assume 2<=n && n<=B;
11
12// Each fork will be on the table (0) or in a hand (1).
13int forks[n];
14
15void dine(int id) {
16 int left = id;
17 int right = (id + 1) % n;
18
19 $atomic{
20 while (1) {
21 if (id % 2 == 0) {
22 $when (forks[left] == 0) {forks[left] = 1;}
23 $when (forks[right] == 0) {forks[right] = 1;}
24 forks[right] = 0;
25 forks[left] = 0;
26 } else {
27 $when (forks[right] == 0) {forks[right] = 1;}
28 $when (forks[left] == 0) {forks[left] = 1;}
29 forks[right] = 0;
30 forks[left] = 0;
31 }
32 }}
33}
34
35/* Put all forks on the table. */
36void init() {
37 for (int i = 0; i < n; i++) forks[i] = 0;
38}
39
40void main() {
41 $proc philosophers[n];
42 $atomic{
43 init();
44 for (int i = 0; i < n; i++)
45 philosophers[i] = $spawn dine(i);}
46 $atomic{
47 for (int i = 0; i < n; i++)
48 $wait philosophers[i];}
49}
Note: See TracBrowser for help on using the repository browser.