2018-12-05 18:09:07 +01:00
|
|
|
BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
|
2018-12-08 22:58:07 +01:00
|
|
|
Source file: zad1.cm Sat Dec 8 22:50:54 2018
|
2018-12-05 18:09:07 +01:00
|
|
|
line pc
|
|
|
|
1 0 semaphore s2;
|
|
|
|
2 0 semaphore s;
|
|
|
|
3 0 const int N = 5;
|
|
|
|
4 0 int buffer[N];
|
|
|
|
5 0 int write_pos = 0;
|
|
|
|
6 0 int read_pos = 0;
|
|
|
|
7 0 void queue_add(int item)
|
|
|
|
8 0 {
|
|
|
|
9 0 buffer[write_pos] = item;
|
|
|
|
10 5 write_pos = (write_pos + 1) % N;
|
|
|
|
11 12 }
|
|
|
|
12 13 int queue_get()
|
|
|
|
13 13 {
|
|
|
|
14 13 int item;
|
|
|
|
15 13 item = buffer[read_pos];
|
|
|
|
16 19 read_pos = (read_pos + 1) % N;
|
|
|
|
17 26 return item;
|
|
|
|
18 30 }
|
|
|
|
19 31
|
2018-12-08 22:58:07 +01:00
|
|
|
20 31 void prod(int i, int n){
|
|
|
|
21 31 int cnt = 1;
|
|
|
|
22 34 for(i; i <= n; i=i+1){
|
|
|
|
23 45 wait(s2);
|
|
|
|
24 47 queue_add(cnt);
|
|
|
|
25 51 cnt = cnt +1;
|
|
|
|
26 56 signal(s);
|
|
|
|
27 58 }
|
|
|
|
28 59 }
|
|
|
|
29 60
|
|
|
|
30 60 void kons(int n){
|
|
|
|
31 60 int i = 0;
|
|
|
|
32 63 int liczba;
|
|
|
|
33 63 for(i = 0; i < n; i = i+1){
|
|
|
|
34 77 wait(s);
|
|
|
|
35 79 liczba = queue_get();
|
|
|
|
36 84 signal(s2);
|
|
|
|
37 86 cout << "Pobralem" <<liczba<< endl;
|
|
|
|
38 90 }
|
|
|
|
39 91 }
|
|
|
|
40 92
|
|
|
|
41 92 main()
|
|
|
|
42 93 {
|
|
|
|
43 93 initialsem(s2, 5);
|
|
|
|
44 96 initialsem(s, 0);
|
|
|
|
45 99 cobegin {
|
|
|
|
46 100 prod(1, 25); kons(25);
|
|
|
|
47 109 }
|
|
|
|
48 110 }
|