From Binary Semaphores to Counting Semaphores
BS1 : binary semaphore = 1;
BS2 : binary semaphore = 0;
Count: integer = initial value of counting semaphore.
wait( BS1 ); Count := Count - 1;
signal (BS1 ); wait( BS2 );
wait( BS1 ); Count := Count + 1;
if Count ? 0 then signal( BS2 ) else signal( BS1 );