In a preemptive operating system the highest priority task ready to run will always run. This is an extremely important point, so we will mention it again: the highest priority task ready to run will always run. This is different than a Windows or Unix system that employs a round-robin in which each task will run based on its time slice. If you create a high priority task that can always run and never blocks, then no lower priority tasks will ever run. Lower priority tasks can only run when a higher priority task blocks on a resource or blocking function call.
For example, there are two tasks A and B. Task A has priority 50 and Task B has priority 51. Since Task A is of higher priority, it will always run (and Task B will never run) unless it calls a blocking function. Task B will then run for as long as Task A blocks; this could be 1 second due to a call to OSTimeDly(TICKS_PER_SECOND), until a shared resource is available due to a call to OSSemPend(), or until data is available from a network connection due to a select()
or read()
. It both tasks are in a blocking state, then the RTOS idle task will run.
Generally any function that pends on a resource or creates a time delay:
Generally any function that does a read operation or pends on a file descriptor
select()
read()
, including variants with timeoutswrite()
, blocks until at least one char can be writtengets()
getchar()
fgets()
Generally calls that will pend on an incoming connection or received data
accept()
UDPPacket
packet object when initialized to receive dataGenerally functions that post to pending objects
The number and type of system tasks and priorities priorities used by the system will depend on your platform and the system features used by your application. For example, if your application calls StartHTTP( ) to enable web services, then a system task will be created that handles web server requests. The system task priority levels are defined in .h.
Whether you use the Application Wizard in NBEclipse to create a new application, or start with one of the example programs, you will notice that the point at which your application takes control is a function named UserMain(). UserMain() is a task created by the system. The first few lines will consist of system initialization functions such as init()
and StartHttp()
.
Additional tasks are created with the OSTaskCreatewName()
or OSSimpleTaskCreatewName()
functions. The following is an example program that demonstrates the use of each function. When using OSTaskCreaewName()
you specify all parameters:
A significant benefit of the full function call is that it returns the status of the task creation process. For example, one possible error is that you cannot create a second task at the same priority as any other task.
The 'OSSimpleTaskCreatewName()' implementation lets you specify just the task function, priority, and name.