CS452 - Real-Time Programming - Winter 2016

Lecture 7 - Create

Public Service Annoucements

  1. Due date for kernel 1: 28 January, 2016
  2. Please remember that some aspects of the system configuration you get depends on the state in which it was left by the previous group. I have seen very hard to find bugs occur at the last minute because a group unknowingly relied on state provided by the group before it.
  3. What swi and movs pc, lr do.

Static/Global Variables.

Global variables

TD tdarray[64];
void main( ) {
  ....
}
int Create( int priority, ( *code( ) ) ) {
  ....
}
tdarray is equally accessible to main and Create. With suitable use of extern it can be made available to code in other files as well. Using your linker script you can have it put anywhere in memory you like.

This kind of global variable is universally recognized as bad programming practice, which doesn't mean that it isn't common in commercial software!

Static variables You are used to lines of code like

printf( "Hello, world.\n" );
How does printf get its argument? Near the end -- usually -- of the function containing printf are 14 bytes containing the ASCII representation of the string. A pointer to it is put into the code by the link editor.

Variables stored like this are called static in C. They preserve their values across function calls. There are three reasons why you might prefer not to make them writable.

  1. You must make the part of memory containing the instructions writable, which leaves you open to a whole lot of widely known exploits.
  2. If you reuse code in different tasks you may find static variables changing value unexpectedly and unpredictably.
  3. If you do something like this
     static TD* tdarray; 
    then read and write the array, strange and amazing things might happen. (Use lint or equivalent.)


Scheduling

There are two important issues for scheduling

  1. When do we reschedule?
  2. Who do we activate when we schedule

When to schedule

Every time we are in the kernel, so the question is `When do we enter the kernel?'
The answer is: whever user code executes SWI.

Who to Schedule

Whoever is needed to meet all the deadlines

Because this is not an easy problem, we don't want to solve it within the kernel. What the kernel does should be fast (=constant time) and not resource constrained.

Scheduling algorithm

  1. Find the highest priority non-empty ready queue. A ready queue can be as simple as a linked list of pointers to task descriptors.
  2. The task found is removed from its queue and becomes the active task. (Until this point active has pointed to the TD of the previously active task.)
  3. When a task is made ready it is put at the end of its ready queue.Thus, all tasks oat the same priority get equal chances of running.

Implementation Comments

The main data structure is usually an array of ready queues, one for each priority.

Each ready queue is a list with a head pointer (for extraction)and a tail pointer (for insertion).

Hint. The Art of Computer Programming (Donald Knuth) says that circular queues are better. Why?

The queues of typical running system

  1. Highest priority:
  2. Medium priority
  3. Low priority
  4. Lowest priority

Implementation

Decisions

  1. How many priorities
  2. Which task should have which priority
  3. What to do when there is no ready task

General structure

Array of ready queues, one for each priority.

Each ready queue is a list with a head pointer (for extraction)and a tail pointer (for insertion).

Implementing lists without memory allocation

You are probably used to implementing lists (and similar data structures) like this

      struct element { struct element *next;
		       struct whatever *content;
      }
      struct list { struct element *head;
		    struct element *tail;
      }
      void insert( list *l, content *c ) {
	struct element *e;
	e = malloc( element );
	e->content = c;
	e->next = null;
	l->tail->next = e;
	l->tail = e;
      }
    
We don't like this because it requires allocating and freeing memory.

Here's the most common way to do this without allocating memory

      typedef struct task-descr { ...
				  struct task-desc *rdy-next;
				  ...
      } TD;
    
All the allocation is done when the task descriptors are declared.

Of course, because allocating and freeing constant sized pieces of data can be done in constant time, you could allocate a pool of list elements when you initialize, and manage it using a free list.


Creating a Task

In creating a task you have to do two things

  1. Get and initialize resources needed by the task
  2. Make the task look as if it had just entered the kernel

Things you need to do

Get an unused TD and memory for its stack

Mostly filling in fields in the TD.

  1. task id
  2. stack pointer
  3. SPSR
  4. link register
  5. parent tid
  6. return value
  7. state, which is READY
  8. priority, needed to install the task into its ready queue

Must also initialize the stack

The following implementation decisions are up to you.


The Create Function

You also need a int Create( int priority, void (*code) ( ) ) function to call from user tasks.

Although it's no more than a wrapper there are a few problems to solve.

  1. Passing arguments
  2. Jumping into the kernel
  3. Getting the return value from the kernel and returning it.


Initializing the Kernel

Set up the Hardware

  1. busy-wait io
  2. low memory
  3. Turn off interrupts in the ICU

Prepare the Kernel Data Structures

Where is the kernel's stack pointer, right now? What does the stack look like?

The kernel data structures. At the very least you need

  1. an array of empty ready queues
  2. a pointer to the TD of the active task
  3. an array of TDs
  4. a free list of pointers to free TDs

Prepare the Memory to be Used by Tasks

  1. task memory

Create the First User Task

Can run with hardware interrupts turned off for now. But when hardware interrupts are turned on in kernel 3 interrupts in user tasks must be turned on, though they stay off in the kernel.

Reminder. The place where the kernel starts executing has the global name main, which cannot be re-used.


Other Primitives

These primitives exist mostly so that we, which includes you, can ensure that task creation and scheduling are working when there is not much else implemented.

Tid MyTid( )

Self-explanatory

A question, to which there is a correct answer, or more specifically, a correct (answer, reason) pair.

Tid MyParentTid( )

Self-explanatory

Where is the parent Tid, and how does the kernel find it?

void Pass( )

Doesn't block: task calling Pass( ) makes a state transition from ACTIVE to READY.

Does reschedule.

When is Pass( ) a NOP?

void Exit( )

Calling task is removed from all queues, but its resources are not reclaimed or reused.

That is, the task goes into a zombie state, and will never become active or ready, but continues to own all its resources.


Return to: