Pliki_SOP/zajecia7/zad1.cm

50 lines
693 B
Plaintext
Raw Normal View History

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;
}
void prod(int n){
int i = 0;
int cnt = 1;
for(i = 0; i < n; i=i+1){
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 {
prod(50); kons(50);
}
}