47 lines
1.2 KiB
Plaintext
47 lines
1.2 KiB
Plaintext
BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
|
|
Source file: main.cm Sat Dec 1 11:07:53 2018
|
|
line pc
|
|
1 0 binarysem sem_produce;
|
|
2 0 binarysem sem_consume;
|
|
3 0
|
|
4 0 int g_value;
|
|
5 0
|
|
6 0 void produce(char id)
|
|
7 0 {
|
|
8 0 int r;
|
|
9 0
|
|
10 0 wait(sem_produce);
|
|
11 2
|
|
12 2 r = random(100);
|
|
13 6 cout << "Producer " << id << " made: " << r << endl;
|
|
14 13 g_value = r;
|
|
15 16
|
|
16 16 signal(sem_consume);
|
|
17 18 }
|
|
18 19
|
|
19 19 void consume(char id)
|
|
20 19 {
|
|
21 19 wait(sem_consume);
|
|
22 21
|
|
23 21 cout << "Consumer " << id << " ate: " << g_value << endl;
|
|
24 28
|
|
25 28 signal(sem_produce);
|
|
26 30 }
|
|
27 31
|
|
28 31 main()
|
|
29 32 {
|
|
30 32 initialsem(sem_produce, 1);
|
|
31 35 initialsem(sem_consume, 1);
|
|
32 38
|
|
33 38 cobegin {
|
|
34 39 produce('A');
|
|
35 43 produce('B');
|
|
36 47
|
|
37 47 consume('A');
|
|
38 51 consume('B');
|
|
39 55 consume('C');
|
|
40 59 }
|
|
41 60
|
|
42 60 cout << "done." << endl;
|
|
43 62 }
|