Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Fixed Running Order

Posted by Mathias Zenger on 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!

RE: Fixed Running Order

Posted by MEdwards on 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.

RE: Fixed Running Order

Posted by raedbenz on 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?


RE: Fixed Running Order

Posted by Richard Damon on 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).

RE: Fixed Running Order

Posted by raedbenz on 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.. ?

RE: Fixed Running Order

Posted by Richard Damon on 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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner