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 polling

2010

Question 52 (2010):

Submission reference: IN1948

Is it possible to loop over some code for a specified duration? Something like:

  SEQ
    tim ? t 
    PRI ALT 
      NOT (tim ? AFTER t PLUS 2000000)
        ...  still waiting, do stuff
      SKIP
        SKIP

I've had a look over lecture slides and cannot find anything. I want to be certain I am receiving all the blobs from the camera before I start searching them for the correct colour.

Answer 52:

Yes, but this is typically simpler than the code you have above. The "AFTER" operator can be used to test whether one time is after another, so can be used in conditionals. For example:

  TIMER tim:
  INT start, stop:
  SEQ
    tim ? start
    stop := start PLUS 2000000
  
    INITIAL BOOL waiting IS TRUE:
    WHILE waiting
      INT now:
      SEQ
        tim ? now
        IF
          stop AFTER now
            ...  still waiting, do stuff
          TRUE
            waiting := FALSE

There are probably better (more elegant) ways to code this, but that should do what you want. If you really wanted to use an ALT with a timeout it could be coded something like:

    TIMER tim:
    INT t:
    SEQ
      tim ? t
  
      INITIAL BOOL waiting IS TRUE:
      WHILE waiting
        PRI ALT
          tim ? AFTER (t PLUS 2000000)
            -- timeout reached
            waiting := FALSE
          SKIP
            ...  still waiting, do stuff

However, this is polling a timer ...so probably isn't the best way to go about it. How about:

    TIMER tim:
    INT t:
    SEQ
      tim ? t
  
      INITIAL BOOL waiting IS TRUE:
      WHILE waiting
        PRI ALT
          tim ? AFTER (t PLUS 2000000)
            -- timeout reached
            waiting := FALSE
	  BLOB blob:
          camera ? blob
            ...  check out this blob (if target, set waiting FALSE)

? That's much better, :). Now, it only does stuff if the camera sends a blob or the timeout happens – and this is the basic pattern for setting timeouts on waiting for things to happen!

You may need some more stuff in the above ... so that you can tell whether you exit the loop because you found the blob or timed out.

Keywords: timers , mars , polling

Referrers: Question 61 (2010)


Question 23 (2010):

Submission reference: IN1918

In a replicated ALT is there a way to place a SKIP guard at the end? I have tried placing it at various points such as:

  ALT i = 0 FOR 10
    in[i] ? y
      ...  do stuff here
    SKIP

But I get a incorrect indentation error.

Answer 23:

A replicated ALT must have one (and only one!) guarded process indented below it – see slide 110 of "choice". So, your replicated ALT finishes with the "... do stuff here" line – and the indentation level returns to that of the ALT. Your SKIP is not at that level, so it's an indentation error (as the compiler correctly reports).

If you really want a SKIP guard to follow a replicated ALT, we must use a nested ALT (slides 123-133 of "choice"). The particular form you need is similar to the use of a replicated IF in slides 92-93 of "replicators":

  PRI ALT
    ALT i = 0 FOR 10
      in[i] ? y
        ...  do stuff here
    SKIP
      ...  do this if no in[i] channels are ready

Note that, as for all un-conditioned SKIP guards, the governing ALT must be PRIioritised – otherwise, the compiler would be correct to throw away the entire ALT and leave just the code reaction to the SKIP (because it's always ready and, therefore, may always be chosen!).

However, are you sure you want that SKIP guard? That would be a poll (see slide 30 in "choice") of all the channels in the 'in' array ... and polling is, usually, a more complex and inefficient way of doing the right thing ... in occam-pi anyway! See Question 32 (2004) and this part of Fred's occam tutorial.

Keywords: replicator , alt , polling

2004

Question 32 (2004):

I'm now somewhat through Q4 and have noticed that I'm using polling `PRI ALT's. It says in the coures notes that they should be used only as a last result -- I'm wondering if this is acceptable in Q4?

I'm not sure of another way of interupting a process based on a signal that may or may not arrive. I've created PRI ALTs in the new control PROCs that have been added to `numbers', `integrate' & `pairs'. I'm now planning on using it to react to the byte `s'

Answer 32:

As the course notes say, polling should only be used as a last resort. I wonder, however, if you're actually polling.. Polling code is specifically this:

    PRI ALT
      c ? x
        ...  do something with `x'
      TRUE & SKIP                -- skip guard
        SKIP                         -- do nothing

When executed, if data is available on the `c' channel (i.e. the corresponding outputting process is blocked in the output), then that data will be accepted immediately (placed in `x') and the guarded process runs. If data is not available on the `c' channel, the `SKIP' guard is selected and does nothing -- so the `PRI ALT' finishes immediately.

That code is polling, and it can create problems.. For example:

    WHILE TRUE
      PRI ALT
        c ? x
          ...  do something with `x'
        TRUE & SKIP
          SKIP

This code will consume as much CPU as it can until some data arrives on the channel `c'. Of course, the correct code in this case (since the process does not interact with anything else in the loop) would be:

    WHILE TRUE
      SEQ
        c ? x
        ...  do something with `x'

This code, on the other hand, will wait for something to arrive on `c' then do something with the data received. And waiting (either blocked on channel communication or waiting for a timeout with `AFTER') does not consume the CPU. (although busy-wait versions of these could be created easily, it would be daft..).

It's the `SKIP' guard in the `PRI ALT' (and subsequent do-nothing process or do-very-little process) that constitutes polling.

The following code, for example, is not strictly polling (but it is not nice code):

    WHILE TRUE
      PRI ALT
        c ? x
          ...  do something with `x'
        TRUE & SKIP
          SEQ
            d ? y
            ...  do something with `y'

If you've written stuff like that, you probably wanted instead to write:

    WHILE TRUE
      PRI ALT
        c ? x
          ...  do something with `x'
        d ? y
          ...  do something with `y'

These two code fragments do not behave in the same way, however. The first will commit to input on `d' if `c' is not ready -- refusing then to service `c' until something has arrived on `d'. The second will happily wait for both, and service the first channel that becomes ready (or `c' in preferance to `d' if both are ready).

You can read some more on ALTs and polling in this occam tutorial.

Keywords: q4 , polling

Referrers: Question 23 (2010)

2002

Question 4 (2002):

Is it possible to take input from a channel un-synchronised? For example, you want to listen for input on the keyboard, but do not wish to hold up the process if a key is not pressed.

Answer 4:

Un-synchronised is not the right word. What you describe is polling the channel to see if input is available from it. If none is available, you would like to be able to do something else and - usually - try again later.

Warning: writing such polling loops is usually the result of bad design. For occam, it's usually simpler to leave a process blocked waiting for input and have another process, running in parallel with it, do that something-else work.

However, if you really need to poll a channel to see if input is available, see slide 5-37. But you will have to study the earlier slides on the ALT construct (and read section 4.3 of the `occam2 Checklist' paper) first to understand it.

Keywords: polling

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.