Prześlij pliki do 'Wykłady'

This commit is contained in:
Marcin Gogolewski 2018-12-01 16:15:51 +00:00
parent c2ba50aafe
commit a924f0b96c
1 changed files with 40 additions and 0 deletions

40
Wykłady/prod-kons.cm Normal file
View File

@ -0,0 +1,40 @@
monitor prod_kons {
int produkt;
int nowys;
condition nowy, uzyty;
void dodaj_element(int pr){
if(nowys==1) waitc(uzyty);
produkt=pr;
nowys=1;
signalc(nowy);
}
int pobierz_element(){
if(!nowys) waitc(nowy);
nowys=0;
signalc(uzyty);
return produkt;
}
init {
nowys = 0;
}
}
void producent(){
int i;
for(i=0;i<50;i++){
dodaj_element(i);
}
}
void konsument(){
int i;
for(i=0;i<50;i++){
cout << pobierz_element() << endl;;
}
}
main(){
cobegin{
producent(); konsument();
}
}