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 process-orientation

2006

Question 43 (2006):

Submission reference: IN1007

I'm having some trouble with my replicated ALT. My display process contains:

    WHILE TRUE
      ALT i = 0 FOR 5
        phils[i] ? n[i]
	  SEQ
	    pause (50000) -- to be replaced by random delays in phil proc
	    out.string ("Philospher ", 0, out!)
	    out.int (i, 0, out!)
            ...  more stuff

The idea is that it'll pick an arbitrary channel from the phils array but when I run this it always seems to pick 0 for i. I tried replacing the ALT with an alternative block of code using a SEQ to ensure all the channels were producing output, which they are.

Answer 43:

I've no idea why you have a delay in response to a message from one of the philosophers?!! The delay is to model a thinking or eating period in the life of a philosopher – so that (i.e. the philosopher process) is where the delay should be invoked. The code we write in occam-pi really is process-oriented: code that relates to the behaviour of a process always is executed by that process.

[Aside: note that the same statement does not apply to object-orientation. Code that relates to the behaviour of an object might be executed by all sorts of threads of control. Unfortunately, this means that to understand/write the code for one method, we have to understand/write the code for all other methods that operate on the fields used by the orginal method and might be exectued by concurrent threads. Such logic is impossible to scale to the management of complex behaviour.]

Assuming you have no delays in your philosopher code, the behaviour you describe can be explained. At the start of each loop, every philosopher is trying to make a report - their think and eat times are zero. The ALT sees all channels with messages pending and it chooses one arbitrarily. Please note that this does not mean randomly or fairly! Always choosing channel index 0 is quite arbitrary and perfectly legal – you cannot complain.

You need to invoke delays, ideally for random amounts of time within some defined limits, in the philospher process to model its thinking and eating times. Then, there will not generally be messages pending on all report channels each time the display process starts its loop – and the ALT will cope just fine.

By the way, why does your code only receive reports from the philosophers? You may find it much easier to receive reports from all players (the philosophers, forks and security guard) in one replicated ALT (over an array of 11 report channels).

Note: sometimes we really do need to service an array of ALT guards (e.g. channel inputs) fairly – for example, when the server is under stress from the continued pushing of messages by very active clients. In this case, we cannot use a simple replicated ALT. We could try a replicated PRI ALT, but that guarantees unfairness – only channel 0 will be serviced.

However, from guaranteed unfairness, we can obtain fairness. A PRI ALT gives priority to its first guard that's ready – that's first in the listed order, not in time of readyness. Find a way to arrange that the guard serviced last time is the last in the PRI ALT order next time.

Please try to work this out – it's very simple once you see it! The technique is know as fair alting. It guarantees that no pending message has to wait more than (n - 1) cycles of a server process loop before it is taken. This gives us worst case response times in a stressed real-time system.

Keywords: q7 , fair-alt , process-orientation , object-orientation

Referrers: Question 44 (2006) , Question 51 (2006) , Question 51 (2006)

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