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

49 lines
688 B
Plaintext

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 i, int n){
int cnt = 1;
for(i; 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(1, 25); kons(25);
}
}