Pliki_SOP/Zadania_domowe/zajecia8/zad1.cm
Jakub Adamski b465891332 zad dom 8
2018-12-08 22:58:07 +01:00

55 lines
733 B
Plaintext

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);
}
}