Process Coordination & Semaphores

Background: consider the following Producer and Consumer programs, which share access to the buffer array:

item nextProduced;
while( true ) {
    /* Produce an item and store it in nextProduced */
    nextProduced = makeNewItem( . . . );
    /* Wait for space to become available */
    while( ( ( in + 1 ) % BUFFER_SIZE ) == out )
         ; /* Do nothing */
    /* And then store the item and repeat the loop. */
    buffer[ in ] = nextProduced;
    in = ( in + 1 ) % BUFFER_SIZE;
}

item nextConsumed;
while( true ) {
    /* Wait for an item to become available */
    while( in == out )
         ; /* Do nothing */
    /* Get the next available item */
    nextConsumed = buffer[ out ];
    out = ( out + 1 ) % BUFFER_SIZE;
    /* Consume the item in nextConsumed
         ( Do something with it ) */
}