zad dom 8

This commit is contained in:
Jakub Adamski 2018-12-08 22:58:07 +01:00
parent 5a065add41
commit b465891332
6 changed files with 146 additions and 35 deletions

View File

@ -0,0 +1,54 @@
const int N = 5;
semaphore s2;
semaphore s;
monitor m{
condition c;
int buffer[N];
int write_pos;
int read_pos;
void queue_add(int item){
buffer[write_pos] = item;
write_pos = (write_pos + 1) % N;
}
int queue_get(){
int item;
item = buffer[read_pos];
read_pos = (read_pos + 1) % N;
return item;
}
init{
write_pos = 0;
read_pos = 0;
}
}
void prod(int i, int n){
for(i; i<=n; i=i+1){
wait(s2);
queue_add(i);
signal(s);
}
}
void kons(int n){
int i;
int liczba;
for(i = 1; i<=n; i=i+1){
wait(s);
liczba=queue_get();
signal(s2);
cout << "Pobralem " <<liczba<< endl;
}
}
main()
{
initialsem(s2, 5);
initialsem(s, 0);
cobegin {
prod(1, 5); prod(6, 10); prod(11, 15); kons(15);
}
}

View File

@ -0,0 +1,59 @@
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 }

Binary file not shown.

View File

@ -17,10 +17,9 @@ int queue_get()
return item; return item;
} }
void prod(int n){ void prod(int i, int n){
int i = 0;
int cnt = 1; int cnt = 1;
for(i = 0; i < n; i=i+1){ for(i; i <= n; i=i+1){
wait(s2); wait(s2);
queue_add(cnt); queue_add(cnt);
cnt = cnt +1; cnt = cnt +1;
@ -44,6 +43,6 @@ main()
initialsem(s2, 5); initialsem(s2, 5);
initialsem(s, 0); initialsem(s, 0);
cobegin { cobegin {
prod(50); kons(50); prod(1, 25); kons(25);
} }
} }

View File

@ -1,5 +1,5 @@
BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012 BACI System: C-- to PCODE Compiler in C, 11:30 1 Oct 2012
Source file: zad1.cm Mon Nov 26 16:14:36 2018 Source file: zad1.cm Sat Dec 8 22:50:54 2018
line pc line pc
1 0 semaphore s2; 1 0 semaphore s2;
2 0 semaphore s; 2 0 semaphore s;
@ -20,33 +20,32 @@ Source file: zad1.cm Mon Nov 26 16:14:36 2018
17 26 return item; 17 26 return item;
18 30 } 18 30 }
19 31 19 31
20 31 void prod(int n){ 20 31 void prod(int i, int n){
21 31 int i = 0; 21 31 int cnt = 1;
22 34 int cnt = 1; 22 34 for(i; i <= n; i=i+1){
23 37 for(i = 0; i < n; i=i+1){ 23 45 wait(s2);
24 51 wait(s2); 24 47 queue_add(cnt);
25 53 queue_add(cnt); 25 51 cnt = cnt +1;
26 57 cnt = cnt +1; 26 56 signal(s);
27 62 signal(s); 27 58 }
28 64 } 28 59 }
29 65 } 29 60
30 66 30 60 void kons(int n){
31 66 void kons(int n){ 31 60 int i = 0;
32 66 int i = 0; 32 63 int liczba;
33 69 int liczba; 33 63 for(i = 0; i < n; i = i+1){
34 69 for(i = 0; i < n; i = i+1){ 34 77 wait(s);
35 83 wait(s); 35 79 liczba = queue_get();
36 85 liczba = queue_get(); 36 84 signal(s2);
37 90 signal(s2); 37 86 cout << "Pobralem" <<liczba<< endl;
38 92 cout << "Pobralem" <<liczba<< endl; 38 90 }
39 96 } 39 91 }
40 97 } 40 92
41 98 41 92 main()
42 98 main() 42 93 {
43 99 { 43 93 initialsem(s2, 5);
44 99 initialsem(s2, 5); 44 96 initialsem(s, 0);
45 102 initialsem(s, 0); 45 99 cobegin {
46 105 cobegin { 46 100 prod(1, 25); kons(25);
47 106 prod(50); kons(50); 47 109 }
48 114 } 48 110 }
49 115 }

Binary file not shown.