Pliki_SOP/Zadania_domowe/zajecia7/zad1.lst
2018-12-05 17:53:14 +01:00

62 lines
1.5 KiB
Plaintext

BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
Source file: zad1.cm Wed Dec 5 14:22:03 2018
line pc
1 0 semaphore s2;
2 0 semaphore s;
3 0 semaphore t;
4 0 const int N = 5;
5 0 int buffer[N];
6 0 int write_pos = 0;
7 0 int read_pos = 0;
8 0 void queue_add(int item)
9 0 {
10 0 buffer[write_pos] = item;
11 5 write_pos = (write_pos + 1) % N;
12 12 }
13 13 int queue_get()
14 13 {
15 13 int item;
16 13 item = buffer[read_pos];
17 19 read_pos = (read_pos + 1) % N;
18 26 return item;
19 30 }
20 31
21 31 void prod1(int i, int n){
22 31 for(i; i <= n; i=i+1){
23 42 wait(s2);
24 44 queue_add(i);
25 48 signal(s);
26 50 }
27 51 signal(t);
28 53 }
29 54
30 54 void prod2(int i, int n){
31 54 wait(t);
32 56 for(i; i<=n; i=i+1){
33 67 wait(s2);
34 69 queue_add(i);
35 73 signal(s);
36 75 }
37 76 }
38 77
39 77 void kons(int n){
40 77 int i = 0;
41 80 int liczba;
42 80 for(i = 0; i < n; i = i+1){
43 94 wait(s);
44 96 liczba = queue_get();
45 101 signal(s2);
46 103 cout << "Pobralem" <<liczba<< endl;
47 107 }
48 108 }
49 109
50 109 main()
51 110 {
52 110 initialsem(s2, 5);
53 113 initialsem(s, 0);
54 116 initialsem(t, 0);
55 119 cobegin {
56 120 prod1(1, 25); prod2(101, 125); kons(50);
57 134 }
58 135 }