binary_semaphore S1=1; binary_semaphore S2=0; binary_semaphore S3=1; int C=initial value of S;
wait(S3); wait(S1); C--; if (C < 0) { signal(S1); wait(S2); } else { signal(S1); } signal(S3);
wait(S1); C++; if (C <= 0) { signal(S2); } signal(S1);
boolean choosing[n]; int number[n];
while (1) { choosing[i]=true; number[i]=max(number[0],number[i],...,number[n-1])+1; choosing[i]=false; for (j=0; j<n; j++) { while (choosing[j]); while ((number[j]!=0) && ((number[j],j) < (number[i],i))); /* critical section */ number[i]=0; /* non-critical section */ }