circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

December 1998, Issue 101

A Minimalist Multitasking Executive


by Richard Man & Christina Willrich
Start Task Function Scheduling and Timeslice Task Context Task Control Block Global Variables Memory Management API Assembly Functions Give it a Try Enhancements References, Sources & PDF

TASK FUNCTION

A µexec task is a C function that does not take an argument or return a result. Any such function can be made into a task via UEXC_CreateTask.

Tasks can be created and killed dynamically, even within other tasks. Normally, a task function should not terminate (i.e., it should execute an infinite loop). If it does, µexec deletes it from the internal data structures.

Typically, you create some tasks in your main routine and then start the scheduler running. After that, the processor executes your tasks and never returns to the main routine.

Each task needs its own stack, which is supplied to the system when a task is created. Unfortunately, stack overrun (i.e., when a task uses stack space that is beyond the range of the supplied stack) can be a deadly problem.

µexec checks the stack pointer of a task to ensure that it’s within bounds, but this check may not be of much value since a stack overrun may have already damaged important data, including µexec’s internal data structures. So, it’s best to be conservative and allocate a large stack for a task. In fact, if your system crashes mysteriously, try allocating larger stacks for your tasks.

Stack overrun is a general problem in embedded programs. Multiple tasks exacerbate the problem because each task uses its own stack. If there is only one control loop, then all the functions execute with one stack and it is easier to be conservative with just that stack. Unfortunately, you have to pay the price for using a multitasking executive.