source: CIVL/examples/concurrency/boundedBuffer_bad.cvl@ 8a8a3a5

1.23 2.0 main test-branch
Last change on this file since 8a8a3a5 was 6eefc77, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

cleaned up concurrency examples.

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

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/**
2 * boundedBuffer_bad.cvl:
3 * This program impelments a bounded-buffer.
4 * It describes two processes, the producer and consumer,
5 * which share a common, fixed-size buffer
6 * used as a queue.
7 *
8 * The producer's job is to generate a piece of data,
9 * put it into the buffer and start again.
10 * At the same time, the consumer is consuming the data
11 * (i.e., removing it from the buffer) one piece at a time. *
12 * The problem is to make sure that the producer won't try
13 * to add data into the buffer if it's full and that the
14 * consumer won't try to remove data from an empty buffer.
15 *
16 * When there is more than one producer or consumer,
17 * there is a race condition.
18 */
19#include<civlc.cvh>
20$input int BUFFER_SIZE = 100;
21$input int NPRODUCER = 5;
22$input int NCONSUMER = 5;
23int fillCount = 0;
24int emptyCount = BUFFER_SIZE;
25
26void producer() {
27 while ($true) {
28 // produce item
29 $when(emptyCount > 0);
30 $assert(emptyCount > 0);
31 emptyCount--;
32 // put item into buffer
33 fillCount++;
34 }
35}
36
37void consumer() {
38 while ($true) {
39 $when(fillCount > 0);
40 $assert(fillCount > 0);
41 fillCount--;
42 // remove item from buffer
43 emptyCount++;
44 // consume item
45 }
46}
47
48void main(){
49 $proc producers[NPRODUCER];
50 $proc consumers[NCONSUMER];
51
52 for(int i = 0; i < NCONSUMER; i++)
53 consumers[i] = $spawn consumer();
54 for(int i = 0; i < NPRODUCER; i++)
55 producers[i] = $spawn producer();
56 $waitall(consumers, NCONSUMER);
57 $waitall(producers, NPRODUCER);
58}
Note: See TracBrowser for help on using the repository browser.