Pliki_SOP/zajecia7/zad1.lst

53 lines
1.3 KiB
Plaintext
Raw Normal View History

2018-12-05 18:09:07 +01:00
BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
Source file: zad1.cm Mon Nov 26 16:14:36 2018
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
20 31 void prod(int n){
21 31 int i = 0;
22 34 int cnt = 1;
23 37 for(i = 0; i < n; i=i+1){
24 51 wait(s2);
25 53 queue_add(cnt);
26 57 cnt = cnt +1;
27 62 signal(s);
28 64 }
29 65 }
30 66
31 66 void kons(int n){
32 66 int i = 0;
33 69 int liczba;
34 69 for(i = 0; i < n; i = i+1){
35 83 wait(s);
36 85 liczba = queue_get();
37 90 signal(s2);
38 92 cout << "Pobralem" <<liczba<< endl;
39 96 }
40 97 }
41 98
42 98 main()
43 99 {
44 99 initialsem(s2, 5);
45 102 initialsem(s, 0);
46 105 cobegin {
47 106 prod(50); kons(50);
48 114 }
49 115 }