Pliki_SOP/Zadania_domowe/zajecia8/zad1.lst

60 lines
1.4 KiB
Plaintext
Raw Normal View History

2018-12-08 22:58:07 +01:00
BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
Source file: zad1.cm Sat Dec 8 22:41:39 2018
line pc
1 0 const int N = 5;
2 0 semaphore s2;
3 0 semaphore s;
4 0 monitor m{
5 0 condition c;
6 0 int buffer[N];
7 0 int write_pos;
8 0 int read_pos;
9 0 int bf;
10 0
11 0 void queue_add(int item){
12 1 buffer[write_pos] = item;
13 6 write_pos = (write_pos + 1) % N;
14 13 }
15 15
16 15 int queue_get(){
17 16 int item;
18 16 item = buffer[read_pos];
19 22 read_pos = (read_pos + 1) % N;
20 29 return item;
21 33 }
22 35
23 35 init{
24 35 write_pos = 0;
25 38 read_pos = 0;
26 41 bf=0;
27 44 }
28 45 }
29 45
30 45 void prod(int i, int n){
31 45 for(i; i<=n; i=i+1){
32 56 wait(s2);
33 58 queue_add(i);
34 62 signal(s);
35 64 }
36 65 }
37 66
38 66 void kons(int n){
39 66 int i;
40 66 int liczba;
41 66 for(i = 1; i<=n; i=i+1){
42 80 wait(s);
43 82 liczba=queue_get();
44 87 signal(s2);
45 89 cout << "Pobralem " <<liczba<< endl;
46 93 }
47 94 }
48 95
49 95 main()
50 96 {
51 96 initialsem(s2, 5);
52 99 initialsem(s, 0);
53 102 cobegin {
54 103 prod(1, 5); prod(6, 10); prod(11, 15); kons(15);
55 122 }
56 123 }