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
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 *
4 * civl verify -inputB=4 dining.cvl
5 */
[793cfc2]6#include <civlc.h>
7
[72c01cc]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];
[793cfc2]14
15void dine(int id) {
16 int left = id;
17 int right = (id + 1) % n;
[72c01cc]18
[24ca21c]19 $atomic{
[72c01cc]20 while (1) {
[793cfc2]21 if (id % 2 == 0) {
[72c01cc]22 $when (forks[left] == 0) {forks[left] = 1;}
23 $when (forks[right] == 0) {forks[right] = 1;}
24 forks[right] = 0;
25 forks[left] = 0;
[793cfc2]26 } else {
[72c01cc]27 $when (forks[right] == 0) {forks[right] = 1;}
28 $when (forks[left] == 0) {forks[left] = 1;}
29 forks[right] = 0;
30 forks[left] = 0;
[793cfc2]31 }
[24ca21c]32 }}
[793cfc2]33}
34
[72c01cc]35/* Put all forks on the table. */
[793cfc2]36void init() {
[72c01cc]37 for (int i = 0; i < n; i++) forks[i] = 0;
[793cfc2]38}
39
40void main() {
41 $proc philosophers[n];
[24ca21c]42 $atomic{
[793cfc2]43 init();
[72c01cc]44 for (int i = 0; i < n; i++)
[24ca21c]45 philosophers[i] = $spawn dine(i);}
46 $atomic{
[72c01cc]47 for (int i = 0; i < n; i++)
[24ca21c]48 $wait philosophers[i];}
[793cfc2]49}
Note: See TracBrowser for help on using the repository browser.