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 replace
2004 |
In the code given in q4 there is a replace procedure. I don't understand what it does. Could you explain how it works please.
This question was discussed in the seminars, attendance at which is compulsary. If you miss one, it is your responsibility to find out what you missed!
Having said that, look at slides 6-21/22/23 ... and ...
This procedure is probably best explained in terms of its occam code -- occam is actually very structured, although getting used to the syntax/indentation can take a little time. The code for `replace' (with the channels dressed up with direction specifiers) is:
PROC replace (CHAN INT in?, control?, out!) WHILE TRUE PRI ALT INT x: control ? x PAR INT any: in ? any out ! x INT x: in ? x out ! x :
The main body of this process is a `WHILE TRUE' loop, so it's going to do something forever. The process inside the `while' is a `PRI ALT' with two guards. The first guard will accept an input from the `control' channel, that has priority over the second guard, an input from `in?'.
As with most ALTs, if neither channel is ready the process will stop running and wait until one of them is. If the `in' channel is ready, and selected, a value will be read and simply output on `out' -- so values can just "flow" through the component unhindered.
However, if the `control' channel is ready (and this will be selected if both channels are ready) a value is read from it and placed in `x'. The code then, and in parallel, performs an input from `in' (throwing the value away) and output the value sent down the `control' channel to `out'. The effect of this is that a communication on the `control' channel causes the next value on `in' to be replaced. In this version, the communications are done in parallel -- so the replaced output may appear before the value that it was replacing. This is not a problem however, and those communications could be done sequentially if desired.
The `replace' component should have been covered in the seminars already, however..
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |