cooperative vs preemptive

nobody wrote on Saturday, January 15, 2005:

Hi, is ther a configuration maro for cooperative/ preemptive compilation? If I need only coperative OS, will it take less ram and rom?

rtel wrote on Saturday, January 15, 2005:

See the page:

Also, most ports have a macro portUSE_PREEMPTION.  This can be found near the top of the portmacro.h file for the port you are using (described on the port documentation page).  It is best to set portUSE_PREEMPTION to either 1 or 0 (to use preemptive or cooperative respectively), and then pass this macro to vTaskStartScheduler().  For example:

[in portmacro.h]
#define portUSE_PREEMPTION 1

[in your application source code]
vTaskStartScheduler( portUSE_PREEMPTION );

The amount of RAM and ROM used depends on how many tasks you create, not which scheduler is used.  However, the scheduler processing overhead is less with the cooperative scheduler. 

If RAM usage is important then consider how you create tasks.  Each task requires its own stack so the fewer tasks the less RAM is required.

For example, if your application has one periodic function that must run with very regular frequency, and a few other functions that can be considered ‘background’ then:

Option 1:
You can create one task for each function and use preemptive scheduling.  The periodic task has the highest priority to ensure it meets its timing requirements.  This gives the simplest design but uses the most RAM.

Option 2:
You can create a high priority task for the periodic function, and a single low priority task for the other functions and use the preemptive scheduler.  The low priority task would contain some kind of loop that loops through all the background functions.  Whenever the periodic function needed to execute the background function task would be preempted.  This option uses less RAM as there are less tasks, but still maintains the timing requirements of the periodic task.  Provided your background functions never block you could even add them to the Idle task - saving even more RAM.

Option 3:
This is a hybrid option.  It is the same as option 2, but using the cooperative scheduler.  If the periodic task can be signalled by an interrupt then you can perform a context switch to the periodic task from the interrupt handler even though the cooperative scheduler is being used.

There will be some more WEB documentation on this very subject appearing in the next week or so.