2018-12-05 18:09:07 +01:00
|
|
|
semaphore s2;
|
|
|
|
semaphore s;
|
|
|
|
const int N = 5;
|
|
|
|
int buffer[N];
|
|
|
|
int write_pos = 0;
|
|
|
|
int read_pos = 0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-08 22:58:07 +01:00
|
|
|
void prod(int i, int n){
|
2018-12-05 18:09:07 +01:00
|
|
|
int cnt = 1;
|
2018-12-08 22:58:07 +01:00
|
|
|
for(i; i <= n; i=i+1){
|
2018-12-05 18:09:07 +01:00
|
|
|
wait(s2);
|
|
|
|
queue_add(cnt);
|
|
|
|
cnt = cnt +1;
|
|
|
|
signal(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kons(int n){
|
|
|
|
int i = 0;
|
|
|
|
int liczba;
|
|
|
|
for(i = 0; i < n; i = i+1){
|
|
|
|
wait(s);
|
|
|
|
liczba = queue_get();
|
|
|
|
signal(s2);
|
|
|
|
cout << "Pobralem" <<liczba<< endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
initialsem(s2, 5);
|
|
|
|
initialsem(s, 0);
|
|
|
|
cobegin {
|
2018-12-08 22:58:07 +01:00
|
|
|
prod(1, 25); kons(25);
|
2018-12-05 18:09:07 +01:00
|
|
|
}
|
|
|
|
}
|