CS452 - Real-Time Programming - Winter 2015

Lecture 22 - Task Structure

Public Service Annoucements

  1. First train control demo: Wednesday, 11 March, 2015.
  2. Cubic velocity calibration.
  3. Your project proposal.
  4. Exam: 16.00 14 April to 18.30 15 April, 2015.

4. Administrator, Worker

Administrator is a proprietor who does no work but only assigns work to others

Where do the workers come from?

Most workers prefer employee status to consultant status.

Worker code

  Send( administrator, nil, workOrder );
    workResult = doWork( workOrder );
    Send( administrator, workResult, workOrder );

doWork might require further sends to servers or warehouses, which is harmless in this context.

What kind of work might a worker do?

Administrator code


Administrator( ) {
  for ( i = 0; i < NUM_WORKERS; i++ ) {
    worker[i] = Create( mypriority - 1, workerCode );
  // and other initialization
    Receive( requester, request );
    switch( request.type ) {
    case CLIENT:
      enqueue( orderQ, {order = {client, request}} );
      if ( !empty( employeeQ ) )
	Reply( dequeue( employeeQ ) ), dequeue( orderQ ) ); 
    case WORKER:
      enqueue( workerQ, requester);
      if ( !empty( orderQ ) )
	Reply( dequeue( employeeQ ), { dequeue( orderQ ) );
      if ( request != NULL )
	Reply( request.client, request.request );

Note: Tid of the client is included in the workorder to the administrator does not have to maintain and search a list of jobs being done. (Administrators are by nature lazy!)

Alternative Worker/Administrator Model

  1. As above, Administrator includes Tid of the client in the order.
  2. Worker replies directly to client with result and to administrator with request for another order to process.


  1. The administrator can add a little more value.

5. The Detective

Simple Events

The notifier is a task that waits on events.

You could call a notifier a detective,

Complex Events

In an application there is likely to be lots of waiting on combinations of events.

We use the detective to discover that a complex event has occurred.


Code could be

      Send( part1 );
      Send( part2 );
      Send( master );


Code above doesn't even pretend to work!

For a time-out detective try something like

    for( i = 0; i < MaxCases; i++ ) {
      enqueue( delayQ, Create( delayer ) );
      enqueue( irregularQ, Create( irregular ) );
      Receive( *requester, request );
      switch ( request.type ) {
      case CLIENT:
        openCase = {requester, request.event, request.delay};
        insert( caseDB, openCase );
        if ( !empty( delayQ ) && !empty( irregularQ ) ) {
          Reply( dequeue( delayQ ), openCase);
          Reply( dequeue( irregularQ ), openCase );
      case DELAY:
	enqueue ( delayQ, requester );
	if ( openCase = extract( caseDB, request ) )
	  Reply( openCase.requester, TIME_OUT );
      case CLUE:
        enqueue( irregularQ, requester );
        if ( openCase = extract( caseDB, request ) )
	  Reply( openCase.requester, GOT_IT );

And, for the delayer, Send( detective, READY, openCase ); FOREVER { Delay( openCase.delay ); Send( detective, openCase, openCase ); }

The irregular is only a little bit more complex.

This is the code of a particular detective, one that notifies you which occurs first, a time-out or an event.


We can say that an event has not happened yet.

Only at the end of the universe can we say that an event simply has not happened.

Time-outs are needed for NOT: how to do them is shown above.

Who is the client of the detective

  1. Initiator of a normal action who wants to know if the result of the action occurred before a particular time.
  2. Housekeeper of the system who will clean up pathologies (Idletask?)

How to give a demo.

You are in charge; don't forget it.


Don't leave dead air.

There are two of you.

Plan what you are going to do and who will do it.

Try out what you are going to do and make sure it works.

Keep a copy of an executable that was working before you start the next debugging step.

How to get back in charge

Inhale loudly.

Respond to whomever has taken control of the conversation, and without a pause say that you need to get on with the next part of the demo.

Start setting up the next demo and explaining it.

If nothing else work talk over the person who is speaking. I will respond to you.

We exclude other students from your demo to remove one distraction from staying in charge.

Return to: