XML

kent logo

CO631 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for shared-channel

2004

Question 106 (2004):

Could you explain "SHARED" channels again and maybe give an example ? From the class they seem like a cleaver idea but now when it comes to coding them I am a little lost.

Answer 106:

There are two ways to do shared-channels in occam, and which one you choose will depend on whether you're coding on Linux or Solaris.

KRoC/Linux has built-in support for shared channels, called "anonymous channel types" in the form most useful for Q7. You can find a fairly terse example of these here.

Shared channels on the KRoC installed on raptor must be done manually -- by turning off compiler usage-checks and using "SEMAPHORE"s to provide mutual exclusion.

For comparison and reference, below are two equivalent versions of a simple test program that use shared channels:

    #INCLUDE "semaphore.inc"
    #USE "course.lib"

    PROC hello (SEMAPHORE sem, CHAN OF BYTE out, VAL INT id)
      SEQ
        claim.semaphore (sem)
        --{{{  output stuff
        out.string ("hello world from ", 0, out)
        out.int (id, 0, out)
        out.string ("*n", 0, out)
        --}}}  
        release.semaphore (sem)
    :

    PROC main (CHAN OF BYTE kyb, scr, err)
      #PRAGMA SHARED scr
      SEMAPHORE sem:
      #PRAGMA SHARED sem
      SEQ
        initialise.semaphore (sem, 1)
        PAR i = 0 FOR 10
          hello (sem, scr, i)
    :

and:

    #USE "course.lib"

    PROC hello (SHARED CHAN BYTE out!, VAL INT id)
      CLAIM out!
        SEQ
          --{{{  output stuff
          out.string ("hello world from ", 0, out!)
          out.int (id, 0, out!)
          out.string ("*n", 0, out!)
          --}}}  
    :

    PROC main (SHARED CHAN BYTE scr!)
      PAR i = 0 FOR 10
        hello (scr, i)
    :

Keywords: q7 , shared-channel

2003

Question 108 (2003):

I've seen that in the dining philosophers animation, when fair ALTing the status reports from the philosophers, forks and security guard, you may have three different case protocols, and have one channel going to the display proc for the philosopher, one for the forks, and one for the security guard. Our seminar leader said that semaphores should be used for the channels where more than one PROC could output at the same time (for philosophers and forks). Is it ok to use `semaphore.inc' in the course library on raptor, or do we need to produce our own implementation?

Answer 108:

If you wish to share channels in this way, yes, using the `semaphore.inc' provided is fine -- and encouraged. If you look at the contents of the file, you'll see that implementing your own is very much non-trivial (although you (CS) know the theory from CO501).

This file defines a `SEMAPHORE' data-type and various PROCs to manipulate the semaphore. Since usage-checking must be disabled on the shared variables, there is a substantially increased risk of program error. Mis-handling of such semaphores can cause anything from deadlock to a serious run-time error (segmentation fault of KRoC). Using lots of channels and ALTing is always a safer bet, but not as efficient or ideal.

The new occam provided in KRoC/Linux has support for SHARED channels at the language level; thus ensuring a basic safe usage (deadlock can still occur through design error, however).

Keywords: shared-channel

Valid CSS!

Valid XHTML 1.0!

Last modified Mon May 15 17:39:48 2006
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.