Fixed Running Order

mathias3 wrote on Monday, June 21, 2010:

My application is running on a AT32UC3A and basically has several tasks with different priorities. There are three tasks which should execute in a fixed order. All three tasks have the same priority and are calling vTaskDelayUntil() with the same delay of 10ms. I expected that the tasks would always execute in the same order as I had started them. However, task 2 and 3 sometimes seem to change the order while task one remains the frist to be executed. What could be the reasons for this change and how can I avoid it? Thanks for your advice!

edwards3 wrote on Tuesday, June 29, 2010:

Tasks that have equal priority are not guaranteed to get an equal amount of CPU time. This is because asynchronous events (interrupts) can cause a context switch at any time. This is why the running order could potentially change.

If you want a fixed running order then you could control this manually by suspending and resuming tasks, running all the functionality in a single task, or using something like a semaphore.

raedbenz wrote on Sunday, July 11, 2010:

I wnat to double check something:
if u create task1, task2 and task3 using

xTaskCreate( vTaskCode1, "NAME1", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
xTaskCreate( vTaskCode2, "NAME2", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
xTaskCreate( vTaskCode3, "NAME3", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );

Does this mean initially (ignoring events at running time ) the task execution order will be as defined above?


richard_damon wrote on Sunday, July 11, 2010:

If tasks need to run in a specific order, and that isn’t being enforced by synchronizing operations (and delay isn’t one of these) then the program is wrong.

If they are truly doing a set of sequential operations, it probably makes sense to make them 1 task , maybe with a central loop calling separate functions like:

void backtask(void* p){
/* Maybe add the wait until here */

If there is some reason this won’t work, than task1  needs to set a flag when it is done that task2 takes (and task2 sets a different flag for task3, and task3 another different flag for task1), then this will force the order to be as expected. If this only wants to be run periodically, then task1 after signaling task2, can waituntil before (or after) waiting for the flag from task3. It might be good to also see if it is running behind (which may be what is causing things to switch order).

raedbenz wrote on Sunday, July 11, 2010:

hello richard,
thanks for the detailed reply. one more thing, according to my code above will the scheduler start scheduling task1 first then task2 etc… ?

richard_damon wrote on Sunday, July 11, 2010:

Richard Barry would probably know, otherwise it would take a detailed analysis of how xTaskCreate works to see what order things end up on the ready list (i.e. do new tasks end up at the front or back of the ready list for their priority). One warning, since that order isn’t documented, it can not be counted on, and even if, say, task1 gets scheduled first, you have no idea how much time it will get (without a significant whole program analysis), as it may get started with just a few clock cycles before the next timer tick, so it may look to the tasks like task2 started first. This is why if it matters, your program has an error.