Tuesday, October 9, 2012

2. Starting MQX, Scheduling and Managing Tasks

I talked about the Task templates in my last post.
The lower the priority value of the task the higher the priority. You can remember this easily by knowing that if you create a task with priority 0, this task will run will interrupts disabled.

There is one more thing you should remember, MQX creates one ready queue for each task up to lowest priority. This creation is done by reading task template at the boot up time. So, you cannot create a task having lower priority than the lowest of task template.

If (hopefully) you are aware of any OS task creation APIs, well this ones is not that much different. Only names would change so I am skipping typing the obvious here.

Scheduling of Tasks 

I won't be able to discuss the topic without describing various states of a task in MQX.
There are three states
1) Blocked : task is not ready because its blocked on some condition to occur. 
2) Ready : task is ready to become active but its not active. This is because its same or lower priority than the active task.
3) Active : Task is running.

Task Creation APIs
 Nothing much different than any other OS. You can create a task by api called '_task_create', you could also create a task which would be in blocked state by api named '_task_create_blocked'. Blocked task can be made ready by _task_ready().

There are other APIs too to get task id, get creator task id, get error code id,  getting/setting exit handler, restarting task, setting error code etc.

Its worth mentioning error code here. Whenever something bad happens to a task it given an error code. If task is allowed to run even after error it may enter to some other error state. But the first error code is not overwritten, this is just to give clear indication to the user whats the original error.


Terminating Task 
A task can terminate itself or it can be terminated by other task. Whenever a task is terminated its children are NOT killed. Whenever a task terminates, MQX frees it all resources like dynamic allocated memory blocks and partition blocks, lightweight semaphore, message queues, mutex etc.


Task Scheduling  
Three policies are provided. Please note all are preemptive.

i) FIFO 


Its default policy. Next task to run here would be that which is waiting for longest. The active task runs until anyone of flowing occurs
-        -Active task voluntarily relinquishes the processor because it calls a blocking function.
-       -Higher priority interrupt occurs
-       -A task having higher priority becomes ready


ii) Round Robin
 Same as FIFO but it has additional constraint that each round robin task a maximum amount of time slice during which it can be active.
You do need to set the time slice for this type of scheduling.  

iii) Task Queues
Look for this space will scribe about it later.

Read about MQX Events. Event Handling, Building an Event based application here


No comments: