resource counter() const int N=10 # Anzahl der Prozesse int count = 0 sem mutex[N] = (1,[N-1]0) # Semaphore zum Schutz von count # increment count by 100 process inc1000 [id=1 to N] { int local int top = 100 for [j=1 to N] { # increment global counter - critical section P(mutex[id]) local = count; writes(id,": get counter = ",local); write() for [k = 1 to top] { local += 1 }; nap(int(random()*100)) count = local; writes(id,": put counter = ",local); write() V(mutex[(id mod N) +1]) # wait for some time - non-critical section nap(int(random()*100)) } } final { write(count) } end counter