source: CIVL/examples/concurrency/dining.cvl

main
Last change on this file 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
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 */
[e6b02c8]6#include <civlc.cvh>
[7b92a9d]7#include <domain.cvh>
[793cfc2]8
[0baeebd]9$input int BOUND = 4; // upper bound on number of philosophers
10$input int n; // number of philosophers
[3ff27cf]11$assume(2<=n && n<=BOUND);
[72c01cc]12
[1de5580]13// Each fork will be on the table (-1) or in a hand (0-(n-1)).
[72c01cc]14int forks[n];
[793cfc2]15
16void dine(int id) {
17 int left = id;
18 int right = (id + 1) % n;
[72c01cc]19
20 while (1) {
[793cfc2]21 if (id % 2 == 0) {
[1de5580]22 $when (forks[left] < 0) {forks[left] = id;}
23 $when (forks[right] < 0) {forks[right] = id;}
24 forks[right] = -1;
25 forks[left] = -1;
[793cfc2]26 } else {
[1de5580]27 $when (forks[right] < 0) {forks[right] = id;}
28 $when (forks[left] < 0) {forks[left] = id;}
29 forks[right] = -1;
30 forks[left] = -1;
[793cfc2]31 }
[05ae5d1]32 }
[793cfc2]33}
34
[72c01cc]35/* Put all forks on the table. */
[793cfc2]36void init() {
[7b9eb4c]37 $for (int i : 0 .. n-1)
38 forks[i] = -1;
[793cfc2]39}
40
[7b9eb4c]41void main() {
[17f5ed1]42 init();
[7b9eb4c]43 $parfor(int i: 0 .. n-1)
44 dine(i);
[793cfc2]45}
Note: See TracBrowser for help on using the repository browser.