# Assume there are three processes: Pa, Pb, and Pc. Only Pa can output # the letter A, Pb B, and Pc C. # Utilizing only semaphores (and no other variables) the processes are # synchronized so that the output satisfies the following conditions: # # a) A B must be output before any C's can be output. # b) B's and C's must alternate in the output string, that is, after the first # B is output, another B cannot be output until a C is output. Similarly # once a C is output, another C cannot be output until a B is output. # c) The total number of B's and C's which have been output at any given point # in the output string cannot exceed the number of A's which have been # output up to that point. # # Examples # # AACB -- invalid, violates a) # ABACAC -- invalid, violates b) # AABCABC -- invalid, violates c) # AABCAAABC -- valid # AAAABCBC -- valid # AB -- valid # resource ABC() sem B := 0, C := 1 # binary semaphores: only take on values 0 and 1 sem sum := 0 # counting semaphore: can have any positive value process Pa do true -> nap(int(random(5000))) writes("A") V(sum) od end Pa process Pb do true -> nap(int(random(10000))) P(C); P(sum) writes("B") V(B) od end Pb process Pc do true -> nap(int(random(10000))) P(B); P(sum) writes("C") V(C) od end Pc end ABC /* % sr -o ABC ABC.sr % ./ABC ABAACAAAABCBACAABCAABCBACBACABAACBAACABCAAAAABCBAAACBAACBCA ^C */