Communications

Multiple Communications Sources with ALT

Occam allows a process to receive messages from multiple possible sources using the ALT constructor. This enables the creation of software which may respond to different input signals on different channels. For example, the following program waits for communications on the "count" channel. Every time one is received, it increments its counter. When a message arrives on the "total" channel, the value is sent on the "output" channel (Brookes, 1989, p. 42):

      ALT                                                        
        count ? tempvariable
           counter := counter + 1
        total ? tempvariable
           SEQ
              output ! counter
              counter := 0
    

Guarded ALT Conditions

The ALT constructor also allows other conditions to be placed on the selection of a message from a channel. The conditions are placed before the input condition followed by a & and the communication input statement. Thus, a particular channel may be read only when the condition is true.

In the following code, a channel called "enable" is used to turn the counter on or off by setting the boolean variable on.flag. If the flag boolean is TRUE, then any incoming count messages increment the counter. If the flag is FALSE, then incoming messages are intercepted and discarded.

    BOOL on.flag:                                              
    SEQ
       on.flag := TRUE
       ALT 
         enable ? on.flag
            SKIP
         on.flag & count ? tempvariable
            counter := counter + 1
         NOT on.flag & count ? tempvariable
            SKIP
         total ? tempvariable
            SEQ
               output ! counter
               counter := 0
    

Guarded communications can be used to create a extremely complex communications systems, including devices such as multiplexors and accumulators.

Next: Data Types
Return to Index