CS452 - Real-Time Programming - Fall 2009

Lecture 2 - Polling Loops


Polling Loops

What is important for real-time?

  1. Throughput
  2. Response time

In cs452 we take guaranteed response time as the defining quality of real-time computation.

Busy Waiting

This is used to synchronize with an external event, minimizing response time.

#define FOREVER for( ; ; )
   while( !ready( ) ) ;
   do-it( );

or in another form

   if ( ready( ) ) do-it( );

Worst case response time

From the time that the ready bit sets until the first instruction of do-it is executed

The NOP technique

Maybe this isn't fast enough for you. Here's an "improvement", for your amusement

  ldb  r0, DATA-ADDRESS // get the next byte
  mov  pc, r5           // r5 contains the address to which we branch

In the initialization code we determine, by experiment, exactly how many nops we need in order to land on the ldb instruction just after the ready bit sets.

Worst case response time

The lesson

There's a lesson for everybody in this extreme example. (Actually there are more extreme examples, and they are used also.) Wherever there is a nicely structured solution with response time x seconds there is a disgustingly-structured example with reponse time less than x.

The problem with busy-waiting

What if the CPU has to both things at once?


  1. collect bytes coming in a serial port
  2. maintain a clock

Unless the rate of bytes coming in and rate of clock ticks are identical

Polling Loops

Polling loops allow you to manage more than one condition/activity pair at the same time.

The basic polling loop

  if( c1 ) a1;
  if( c2 ) a2;
  if( cN ) aN;

Worst case response time

Testing more than once

Suppose you want a better response time for a1. Then try the loop

  if( c1 ) a1;
  if( c2 ) a2;
  if( c1 ) a1;
  if( c3 ) a3;
  if( c1 ) a1;
  if( cN ) aN;

Worst case response time for a1

Breaking into pieces

Suppose the response time is still too long, because the execution of one action, say a2, is too long. Then you can break a2 into two parts

  if( c1 ) a1;
  if( c2 ) { a2.1; half-done = TRUE; }
  if( c1 ) a1;
  if( half-done ) { a2.2; half-done = FALSE; }

This is strarting to get a little complicated and we haven't said anything about inter-action communication

Assignment 1

Overall System

Provided and maintained by CSCF


`COM' ports

Connected to UARTs

Only really two

Ethernet port

Reset switch



System on chip


Byte addressable, word size 32 bits

Separate instruction and data caches



GNU tool chain


Partial implementation

Returns when program terminates

Busy-wait IO

COM2 uses monitor; COM1 goes to train

  1. initialization
  2. output
  3. input

Data Communication using RS-232

Communication protocol existing since forever.

Why do we still use it in the age of USB, etc.

Very simple RS-232

Three wires

Wired like this

GND ---------- GND

XMIT --\ /---- XMIT
RCV----/ \---- RCV

What could be simpler?

Less Simple RS-232

Distinguish Data Terminal Equipment (DTE) from Data Circuit-terminating Equipment (DCE)

On the DE-9 connector we have for DTE

5 -- GND
3 -- XMIT
2 -- RCV

and for DCE

5 -- GND
3 -- RCV
2 -- XMIT

A very simple `straight through' cable connects these perfectly. But when we conect DTE to DTE we need the cable above, which is called a `null modem'.

Train RS-232

Three wire works fine as long as both ends of the wire are much faster than the wire itself. But the train controller is very slow. Why?

The train controller needs to be able to say STOP. How is this done?

Software Flow Control


Hardware Flow Control


DTR, DSRWhat Happens if You Omit Flow Control

Return to: