XML

kent logo

CO538 Anonymous Questions and Answers

This page lists the various questions and answers. 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.

We have taken the liberty of making some minor typographical corrections to some of the questions as originally put. Although most of the questions here will have been submitted anonymously, this page also serves to answer some questions of general interest to those on the course.

When asking questions, please do not simply paste in your code and ask what is wrong with it, as these types of question are hard to answer in a public forum (resulting in either no answer, or an answer which will only be of use to the person asking the question). In many cases, a question about code can be formulated in general terms. For example, if you have an `IF' block that results in an `incorrect indentation' error, you might ask, ``what is the correct syntax for an occam `if' statement ?''. Questions of this form are easier to answer, and are meaningful to all.

Questions that are not suitable for these public pages (i.e. those that contain assignment-specific code), should be mailed to your seminar-leader.


Question 21:

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)


Question 22:

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 23:

In the example below, how can I execute the quoted code after any of the two guards is executed? I could put the code with a SEQ in the process part of both guards, but that wouldn't look nice. If I indent the quoted code as in the example below, the compiler complains.

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    PRI ALT
      inA ? x
        outA ! 1
      inB ? z
        outB ! 2
  " SEQ
      ...  do in all cases"

Answer 23:

This (I think) is what you want but wouldn't look nice:

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    PRI ALT
      inA ? x
        SEQ
          outA ! 1
          ...  do in all cases
      inB ? z
        SEQ
          outB ! 2
          ...  do in all cases

If so, the common code can be factored out (so that it appears only once) by moving the SEQ outwards:

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    SEQ
      PRI ALT
        inA ? x
          outA ! 1
        inB ? z
          outB ! 2
      ...  do in all cases

Now, the PRI ALT executes one of its guarded processes (when one or both of them become ready). Then, the do in all cases code gets executed.

Keywords: alternation , sequential-execution


Question 24:

I am working on q4.occ. Inside one of my PROC headers, I have delcared a BYTE variable:

  BYTE c:

I then read in a BYTE and store it into this variable. Then, I wish to do checks on this variable to see what character it is holding. For example:

  IF
    c = 'n'
      ...  do something

This obviously isn't how it's done? Looking back at bugs.occ quotations marks give the ASCII value of the char.

Please let me know how this is done as I can find nothing about this in the notes.

Answer 24:

There is nothing wrong with the code fragments above. A character within single quotation marks is a BYTE literal, whose value is the ASCII code of the quoted character. Mail me (or your seminar leader) your complete code and say precisely what goes wrong.

Keywords: q4


Question 25:

I've just been told that q4 is due for Monday(19th), but on the website it says that it is due for 30th Nov. Please could you confirm which one is true?

Answer 25:

Monday (yesterday) was the 20th. November (not 19th.)? However, the Co516 web site is correct - that assignment is due on the 30th. November, 2000 (by close of business at the CAS office).


Question 26:

In Q4, when you say `suspends output to the screen', do you mean the processes should keep going and not output anything to the screen, or pause and wait until they are allowed to continue?

Answer 26:

The latter - the processes should become blocked until output is allowed back through to the screen. Don't modify those processes in any way to get this functionalty. Do it the same way you did it for question 3 from the Execrcise Sheet (which was considered in your seminar groups).

Keywords: q4 , suspend

Referrers: Question 32 (2000)


Question 27:

When implementing the part where the plus process is changed to a subtractor when `p' is pressed, am I right in saying we don't really have to change pairs apart from the reset. The rest is done by changing the component plus?

Answer 27:

Yes - that is one way to do it. There is another way that does not require changing that plus process - merely inserts another component into the (modified) pairs network.

Keywords: q4


Question 28:

Is there a way of allowing two commands to be carried out within an IF statement's condition in occam?

Answer 28:

See the answer to Question 1 (2000) of these anonymous questions. When there are two things to do in occam, we have to say whether they are to be done in SEQuence or in PARallel. Probably, you want then to be done in the SEQuence they are written - in which case:

  IF
    some.test > or.other
      SEQ
        ...  first thing to do
        ...  second thing to do
    another.test
      SEQ
        ...  another first thing to do
        ...  another second thing to do

Keywords: if


Question 29:

Help! I am trying to do part three of question 4 (the second assignment) and I can't get the following to compile. The error message is that state is not declared on the line marked:

  WHILE TRUE
    SEQ
      BOOL state:
      state := TRUE  -- start with normal pairs process
      WHILE state    -- <<<< COMPILER COMPLAINS HERE
        ...  loop body

Surely though, it is - just above the line which causes the compiler to fail!

Answer 29:

No it's not!! It's declared two lines before the line complained of by the compiler. Declarations have scope for subsequent declarations and the first executable that follows (which must all be at the same level of indentation). In the above, the first executable following the declaration is the assignment - so state is in scope there, but not for the following executable (the while-loop).

To make the declaration have scope for more than one executable, those executables must be wrapped up in a SEQ (or PAR or WHILE or IF etc.) - that turns them into a single executable. This question has been asked before - see the answer to Question 3 (2000).

Keywords: q4 , variable-scope

Referrers: Question 54 (2004)


Question 30:

I am currently working on modification 3 of q4. I have modified the code so that it switches between being a plus and minus process - but when switching to the minus process all the numbers printed are 1 greater than the number entered. Is this OK or should a further modification be made to compensate for this error?

Answer 30:

It's OK! Turning the plus into a minus changes pairs into a differentiate, except that the first number that ought to be output is missing. All the question asks is that you make this change (dynamically, in response to a `p' from the keyboard). What it does doesn't matter. So the fact that it turns that last process into something that (almost) cancels out the second, leaving you with traffic that equals the input to the second - except that one number gets lost - is fine.

Keywords: q4


Question 31:

Can we use the keyword STOP in response to a `q' being pressed in Assignment 2?

Answer 31:

Yes - but KRoC is currently set up to regard STOP as an error, so your program will crash. That's OK but not very nice!

We might change KRoC to handle a STOP differently - occam allows more than one semantics. The default one is the current one - treat it as an error. The (arguably proper) one is that the process executing the STOP just stops, but other processes running in parallel continue.

If we made this change, then simply getting your monitor process to execute a STOP would stop that monitor - but the rest of your network might carry on producing output (depending on how you set it up). That would be wrong.

So, your monitor process needs to do something to block everything else as well (e.g. cause a deadlock). The model answer blocks all of the network, but leaves the monitor running (all it does is go into a state where it black holes all keyboard input). The system is not deadlocked because the monitor is still alive and accepting further input. Nothing more is being produced, though, and keyboard input is consumed but ignored - we have livelock.

Bringing a network to a graceful halt (by causing all its component processes to reach their natural end without triggering deadlock) is hard. The paper in the course book ("Graceful Termination - Graceful Resetting") shows how to do it - it's fairly mechanical but tedious. For this exercise, you would have to make new versions of all the basic cycle processes used from the course library - these new versions would be ready to terminate (i.e. no WHILE TRUEs).

But graceful termination is not required. Just getting the monitor to execute STOP is also not good. Getting the monitor to trigger a deadlock (in response to a `q') is easy and will earn full marks (for this part).

Keywords: q4 , stop

Referrers: Question 13 (2001)


Question 32:

When implementing the response to an `s' being pressed should it either:

Answer 32:

The former - see the answer to Question 26 (2000).

Keywords: q4 , suspend


Question 33:

When I do the following:

 INT x:

is it ok to assume that the x variable is always initialised to zero?

Or is it a bad assumption (like assuming the ALT statement will always act like the PRI ALT statement)?

Answer 33:

No, it's not OK - and yes, it is a bad assumption.

occam does not specify any initial values for variables. The KRoC implementation specifically does not initialise variables to zero - we get whatever happens to be in the memory allocated for the variable.

In general, it's probably a bad thing for languages to initialise variables automatically to arbitrary values like zero. Ideally, I would prefer any automatic initialisation to set data values to something very bad - like NaN (Not-a-Number) for floating-point or MinInt (for integers). We don't always want variables initialised to zero and having that happen by default makes us lazy - we may miss out a non-zero initialisation when it's needed. [Of course, this is not a universally held view ...]

Keywords: variable-initialisation


Question 34:

I am having trouble with the layout.max.chans process. I am calling it within another PROC as follows:

  PROC q4 (...)
    ...
    ...
    [max.chans] CHAN OF INT c:
    PAR
      ...
      ...
      ...
      ...
      ...
      layout.max.chans ([c], screen)
  :

Am I calling it correctly?

Answer 34:

No. Your c variable is already an array holding max.chans elements (that happen to be channels). The expression [c] creates an array with one element (that happens to be an array of three channels) - this is not what layout.max.chans is expecting! Your call should be:

      layout.max.chans (c, screen)

Keywords: q4


Question 35:

For the assessment, are we allowed to code the modified versions of int and pairs sequentially, as is lecture slide 5-11? Or do we have to stick to creating them from the given components in "course.lib"?

Answer 35:

No - when you modify those processes to introduce the reset/toggle channels, keep the same level of concurrency. But this can't be done just by inserting extra components from "course.lib" - you have to use some others. A big hint is supplied by the inclusion of the replace process in your starter file (exercises/q4.occ) - you just have to work out how to use it. The model answer reuses the existing sub-components (of numbers, integrate and pairs) unchanged - their behaviour is modified simply by inserting an extra process into their respective circuits.

Modified versions of slide 5-11 type implementations will be accepted, but will not attract full marks.

Keywords: q4


Question 36:

Do you just want the code for q4.occ or do you want any new diagrams we have done drawn out as well?

Answer 36:

You should submit hard-copy of your program listing attached to a cover sheet to the CAS office (by close of business on Thursday, 30th. November, 2000). You should also include network diagrams of any networks you have designed. In this case, that will be for your modified versions of numbers, integrate and pairs, together with the overall network for the q4 process. On these diagrams, channel names should be labelled with the names used in your code and the direction of data-flow should be arrowed. Neat hand-drawn diagrams on you program listings are fine.

Keywords: q4 , diagrams


Question 37:

I'm in the process of doing q4 and I've implemented everything except pairs. It all compiles but, when I run it, it immediately gives me an error:

  Range error / STOP executed (signal 4)
  Fatal error code 1, core dumped
  Abort (core dumped)

First thoughts were that I had used STOP wrong. However, I took it out and still got the same error. I can't work out where I could be going out of range, so I just wondered if you could explain the error message to me so I could work out what part of my code it is refering to.

Answer 37:

The run-time error messages are a bit terse, I'm afraid. The above error message can be caused by at least 4 things:

It's probably the last one that's your problem ...

Keywords: q4 , range-error , stop


Question 38:

In question 4, when you press the i key to reset the integrate thingy, should it output 0, or the next value that comes in?

Answer 38:

The question did not specify - so either version will be accepted.

Keywords: q4


Question 39:

In the specification for the p bit in q4.occ, it says: "take the output arriving from the tail from that arriving directly from the delta". I'm certain, well confident, well satisfied, ... ERM.. at least I think I've implemented it as specified, but my output gives:

  -1
  -2
  -3
  -4
  ...

I am very tempted just to switch the channels going into plus so that it gives:

  1
  2
  3
  4
  ...

when its in subtractor mode. Or are the negative numbers actually what's required ... i.e. the assignment means do as I say not do what you think I mean ... ?

Answer 39:

You've got me here! In question 4 on the exercise sheet, it doesn't attempt to specify which way around the minusing should operate (in response to a p from the keyboard). So, doing it either way round would be OK.

In the q4.occ file, I tried to make this precise but - given the complexities of natural language - managed to say it the wrong way around :-( ... Apologies for this. So, doing it either way round - what I said or what I meant - is OK.

Keywords: q4 , negative-numbers


Question 40:

Is it possible to invert a BOOL variable - for example:

  minus = NOT minus

???

Answer 40:

Yes - but it should be:

  minus := NOT minus

Keywords: inverting

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:24 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.