XML

kent logo

CO538 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 channels

2010

Question 60 (2010):

Submission reference: IN1956

I'm experiencing an embarrassingly stupid problem with reading a value on a channel into a variable.

  PROC a (CHAN INT a?)
    CHAN INT b:
    INT output:
    SEQ
      some.proc (a?, b!)
      b ? output
      ...  do stuff with output
  :

"some.proc" hangs when it outputs on b because I guess nothing picks up the value. Can you please suggest what I need to do to get this to work? The compiler warns about sequential input/output on b but I can't think how else to express that I just want the value on that channel assigned to a variable.

Answer 60:

The code you have above tries, in SEQuence, to output then input on the same channel (b). Because channel communication is synchronous, the output (inside "some.proc") will just block, because there is no parallel process to communicate with. That process ("b ? output") cannot run until the call to "some.proc" completes, because of the SEQ, thus deadlock. The answer is to add some parallelism, e.g.:

    PROC a (CHAN INT a?)
      INT output:
      SEQ

        CHAN INT b:   -- only needed for the PAR (declare stuff only where needed)
        PAR
          some.proc (a?, b!)
          b ? output

        ...  do stuff with output
    :

This assumes that "some.proc" only outputs once on "b" and then terminates, else the "... do stuff with output" won't be able to run. If "some.proc" outputs repeatedly on "b" (and maybe never terminates), you would need something like:

    PROC a (CHAN INT a?)
      CHAN INT b:
      PAR

        some.proc (a?, b!)
  
        INT output:
        SEQ
          b ? output
          ...  do stuff with output
    :

On the other hand, if your "some.proc" does only output one value down the "b" channel, it's much simpler to return that value through a reference parameter – i.e. not to use a channel and the first PAR code above. Question 58 (2010) introduces the (non-examinable) concept of RESULT reference parameters, which would be ideal for this – for example:

    PROC some.proc (CHAN INT in?, RESULT INT answer)
      ...  puts some value into 'answer'
    :

    PROC a (CHAN INT a?)
      INT output:
      SEQ
        some.proc (a?, output)
        ...  do stuff with output
    :

The above code will work the same without the RESULT keyword qualifying the "INT answer" reference parameter. Having the keyword improves safety, since the compiler checks that "answer" is not used before it is given a value and that it is given a value (at least once) before "some.proc" terminates.

Keywords: channels , seq , par , result

Referrers: Question 56 (2011)

2009

Question 29 (2009):

Submission reference: IN1828

I've added the following output channel to the philosopher: CHAN PHIL.STATE report! PHIL.STATE has been defined as a CASE protocol. secure.college outputs an array of this particular protocol type to the display method. This is defined in the secure.college header like so:

    CHAN [n.philosophers]PHIL.STATE phil.out!

However, when attempting to assign each philosopher in the replicated PAR to a particular point in this array I get an error: "phil.out subscripted too many times". I am assigning it as follows:

    philosopher (i, left[i]!, right[i]!, down[i]!, up[i]!, phil.out[i]!)

What does this error mean? Am I doing something completely wrong?

Answer 29:

Your declaration of the channel array phil.out is wrong. You have declared a single channel that carries an array of PHIL.STATE messages (in each message):

    CHAN [n.philosophers]PHIL.STATE phil.out!

You need an array of channels, each one carrying a single PHIL.STATE in each message:

    [n.philosophers]CHAN PHIL.STATE phil.out!

Now, phil.out[i] references element i from the channel array. Before, phil.out was not an array, so adding the array subscript was an array subscript too many - as the error message says!

But there's something wrong with our compiler! occam-pi currently does not support the concept of an array of PHIL.STATE messages. On encountering your [n.philosophers]PHIL.STATE, the compiler should have thrown an error message of the form:

    Error-oc-charray.occ(line-number)- Name PHIL.STATE is not a TYPE

Investigating ...

Keywords: q7 , arrays , channels

2006

Question 9 (2006):

Submission reference: IN757

Just wondering for the bonus task, this is as far as I got, and nothing works, is there something I'm missing? just curious! I gave it a shot anyway...

  -- from demo.occ
  PROC print.streams (VAL INT delay, []CHAN INT in?, CHAN BYTE out!)
    INITIAL MOBILE []INT n IS MOBILE [SIZE in?]INT:
    -- Above is the rather complex way we have to declare an array whose size
    -- in not known until runtime.  This will be made simple in future versions
    -- of occam-pi.
    WHILE TRUE
      SEQ
        PAR i = 0 FOR SIZE n
          in[i] ? n[i]
        SEQ i = 0 FOR SIZE n
          out.int (n[i], 15, out!)	-- out.int is from "course.lib"
        out.string ("*c*n", 0, out!)	-- out.string is from "course.lib"
        pause (delay)
:

Mine:

  PROC cols (INT colNum, CHAN BYTE out!)
    VAL INT colNum1 IS 3:
    INITIAL INT in0 IS 66:
    [colNum1] CHAN INT c:
    
    INITIAL INT i IS 0:
    WHILE TRUE
      SEQ i = 0 FOR colNum1
        SEQ
          c[i] ? in0
          IF
            i = colNum1 
              print.streams (100000, c?, out!)
            TRUE
              SKIP
  :

Answer 9:

What you have there won't compile. The most obvious error is that your "cols" has no inputs -- so can't be connected up to the rest of the network. The spec talks about adding a "VAL INT" parameter, so I'd expect the rest of the PROC header to be intact, e.g.:

  PROC cols (VAL INT n.cols, delay, []CHAN INT in?, CHAN BYTE out!)

The order of parameters is, of course, unimportant. As long as it's wired up in a matching way.

Your code is attempting to declare a run-time sized array of channels, which we don't support (yet). You can do it, but it's a bit ugly:

  MOBILE []CHAN INT c:
  SEQ
    c := MOBILE [n.cols]CHAN INT

But you don't need to do this -- 'n.cols' channels should be passed as parameters (in the array of channels "in". The existing code uses the size of this ("SIZE in") to calculate the size of its data array. Your code also instances "print.streams"; as this contains a infinite loop ("WHILE TRUE"), that procedure call will be made, but won't ever return. Better would be to start with the existing definition of "print.streams" and modify that, rather than starting afresh.

The actual answer shouldn't be too much different from the original. As you learn more occam-pi this should become clearer. :-)

Keywords: channels

2004

Question 98 (2004):

Can I check that "!" is an input and "?" is an output ?

Answer 98:

In fact it's the other way around, but finding this out for yourself should be easy! Look at the occam-2 checklist (in the course-notes and on raptor in "/usr/local/courses/co516/etc/").

Keywords: channels

2002

Question 19 (2002):

For the second modification task, I have created my own processors numbers.reset and integrate.reset which respond to the inputs 'n' and 'i' respectively, however when I assign the replace processor to be used within each of these processors, it displays an invalid process error:-

     :    reset ? ch
     :    IF
     :      (ch = 'n')
     :        PAR
  318:          replace (a, 0, b)
     :          delta (b, out, c)
     :          succ (c, d)
     :          prefix (0, d, a)
     :      TRUE
  Error-oc-q4.occ(304)- Invalid process
  type tree not caught by case (switch) is CHAN

Why does this occur?

Answer 19:

Well ... replace takes 3 channel parameters and you supply an INT (0) as one of them! The Solaris kroc compiler does give a poor error message, but nobody at INMOS ever thought someone would write code like that! The Linux kroc handles this better.

Your code is very wrong though. You are misunderstanding something fundamental. See your seminar leader. [If you get an 'n' in the above, you start up a PAR construct that never ends ...?]

Keywords: q4 , channels

2000

Question 64 (2000):

I am looking at some of the past papers and have a couple of questions about one of the questions. It asks you to explain the synchronisation of a channel? But all it has in the notes about synchronising is:

              input synchronising  =  input ? x

and the same for output. Is this all it means or is there more? Thanks.

Answer 64:

This is a pretty fundamental issue. If you don't know the answer, then you cannot have been following the course and will not be able to appreciate how any of the exercises or examples work. More likely, you have just been thrown by terminology and really do understand the concepts.

The basic facts of channel synchronisation are given in slides 4-27 through 4-31. This is repeated in sections 3.2 and 3.3 of the "occam2 Checklist" paper in your notes. The idea is assumed by almost everything else in the course.

Keywords: channels , synchronisation


Question 22 (2000):

How can I check the value of an input channel? That is, if I have:

  PROC check (CHAN OF BOOL bool, bool2)
    SEQ
      IF
        bool = TRUE
          bool2 ! TRUE
        ...  other tests
  :

how do I check the value of bool?

Answer 22:

This shows the same misunderstanding as Question 21 (2000). Channels do not have values.

Maybe you mean to input a value from your bool channel and, then, check that. If so, this is what you need:

  PROC check (CHAN OF BOOL bool, bool2)
    BOOL b:
    SEQ
      bool ? b
      IF
        b
          bool2 ! TRUE
        ...  other tests
  :

Or maybe you want to check if there is input available on the channel and, if so, take it - but if no data is available, do something else. This is known as polling - see slide 5-37 for how to do it in general. For the above, you could have:

  PROC check (CHAN OF BOOL bool, bool2)
    SEQ
      BOOL b:
      PRI ALT
        bool ? b
          IF
            b
              bool2 ! TRUE
            ...  other tests
        TRUE & SKIP
          ...  do something else
      ...  this happens after execution of one of the PRI ALTernatives
  :

Keywords: channels


Question 21 (2000):

Can I assign a value to a channel? For example:

  PROC foo (CHAN OF BOOL a)
    SEQ
      a := TRUE
      ...

Answer 21:

No. You can only assign values to variables, not channels.

In your code fragment, a is a channel. Channels are only used to communicate values computed in the outputting process to variables in the inputting process. Channels do not hold data values themselves. Therefore, your above assignment makes no sense and will be rejected by the compiler.

The only symbol you can have to the right of a line that starts with a channel variable is either a ? (followed by a variable that is the target of the input) or a ! (followed by an expression that yields the value to be output).

Keywords: channels

Referrers: Question 22 (2000)

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:26 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.