60 lines
825 B
Plaintext
60 lines
825 B
Plaintext
semaphore s2;
|
|
semaphore s;
|
|
semaphore t;
|
|
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;
|
|
}
|
|
|
|
void prod1(int i, int n){
|
|
for(i; i <= n; i=i+1){
|
|
wait(s2);
|
|
queue_add(i);
|
|
signal(s);
|
|
}
|
|
signal(t);
|
|
}
|
|
|
|
void prod2(int i, int n){
|
|
wait(t);
|
|
for(i; i<=n; i=i+1){
|
|
wait(s2);
|
|
queue_add(i);
|
|
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);
|
|
initialsem(t, 0);
|
|
cobegin {
|
|
prod1(1, 25); prod2(101, 125); kons(50);
|
|
}
|
|
}
|