Tasks not running on priority base

Hi,
I was working on project where i have to run my tasks based on priority value. higher the priority of tasks that task should execute first and followed by next higher task, this should continue to the last task which has lowest priority.

example of code is:

void myTask1(void*p)
{
printf(“task1 is created\n”);

}
void myTask2(void*p)
{
printf(“task2 is created\n”);

}
void myTask3(void*p)
{
printf(“task3 is created\n”);

}
void myTask4(void*p)
{
printf(“task4 is created\n”);

}
int main(void)
{

xTaskCreate(myTask1, ( signed char * ) "TX1",configMINIMAL_STACK_SIZE,NULL,9,&myTaskHandle1);
xTaskCreate(myTask2, ( signed char * ) "TX2",configMINIMAL_STACK_SIZE,NULL,4,&myTaskHandle2);
xTaskCreate(myTask3, ( signed char * ) "TX3",configMINIMAL_STACK_SIZE,NULL,10,&myTaskHandle3);
xTaskCreate(myTask4, ( signed char * ) "TX4",configMINIMAL_STACK_SIZE,NULL,1,&myTaskHandle4);


vTaskStartScheduler();

}
As shown in the above code if we assume the priorities of taks which creating first is 9 and next is 4 ,10,1 respectively.
i want 3rd Task should execute first and then 1st, 2nd and 4th at last.
but we are not getting the output as expected.

i have defined a macro #define configMAX_PRIORITIES ( 10 )

please let me know if any changes required.

Regards,
Mansoor Basha

The FreeRTOS scheduler will always run the highest priority task that is able to run, so will behave as you desire, and we have plenty of tests to ensure that
is the case.

You don’t actually describe the behavior you see, so I’m not sure how to reply.

As per the book, often printf() is the route cause of user issues, and adding in further printf() statements to try and debug just makes it worse. I would recommend
removing the printf() then placing a break point at the start of each task to see the order in which they execute.

As per the book and documentation on the website, tasks cannot just run off the end of their implementing functions, so I assume the code you have posted is just
for demonstration - if not then the implementing functions must either be infinite loops, or otherwise delete the task that runs the loop before it exits the function.

Hi,
Thanks for the Reply.

yes the code which is posted above is just to test the tasks running on priority base because in our project also tasks are not running as expected.

For example the above code’s expected result is.

Expected output:
task3 is created
task1 is created
task2 is created
task4 is created

output getting:
task3 is created
task1 is created

only 2 tasks are getting executed Task2 and Task4 is not executing.

if we change the priority of the 1st task less than the priority of the 2nd task in the above example only task1 is executing.

if i remove the printf()s and empty tasks if i debug. the last task will get execute first irrespective of its priority.and other tasks executes priority wise. but if i change priority of the last task created less than the priority of other tasks. the task created just above the last task gets executed first as same as above, it followes.

hence without printf()s also execution is not happenning as expected but some what it follows the priority in between.

with printf()s the result is shown above.

Thanks and Regards,
Mansoor Basha

I’m finding this a bit confusing so will try and restate the first part as: If I create four tasks that contain a printf() then the first two tasks execute in the expected order, but then the system crashes.

Is that correct? If so then, as per above, my first suspect would be printf() itself. Lots can go wrong with printf(). First it is highly unlikely to be thread safe so calling it from multiple tasks with no protection could in itself cause the system to crash. Second it can unexpectedly call malloc(). Third it can take a very long time to execute. Fourth it often massively bloats the code size because it brings in all the floating point libraries. Fifth, and the cause of most issues reported that are tracked down to the use of printf(), it creates a huge stack frame which often just overflows the stack.

Do you have configCHECK_FOR_STACK_OVERFLOW set to 2?

To the second part of your post - when I run the code I see task 3 executes first, followed by tasks 1, 2 and finally 4 - I think that is as expected.

As per above - if that is literally the code that is running then it will definitely crash as you can’t let a task run off the end of the function that implements it.

Hi,
let me first clear this i am using synopsis metaware ARC IDE.

I am using the FreeRTOSConfig.h file which is in the your website. https://www.freertos.org/a00110.html
in that MAX prioriity is set to 5

Task creation is as shown in the previous post. with or without printf()s.

case1.
i have 4 tasks with a priority of 0 1 2 3 and different combination of this works fine tasks are executing perfectly on the priority base.
the MAX priority in the config.h is changed to 1 then also it is working fine. irresepective of the MAX priority value it is working fine for the above combination of priorities.

case2.
i created 5 tasks with a prioirity 0 1 2 3 4 and different combination of this, it is not working. works fine only for 1 or 2 combinations of the priority. this is also with max priority 5 and also 1

how does MAX priority set in config,h file affects the priority values given during the creation of the task?

case3.
i created the 6 tasks with a priority 0 1 2 3 4 5 it is also it is not working on priority base. i gave the priority like 0 1 2 3 3 3 it execuets 3 tasks with same priority then it executes the other 3 tasks on priority.

case4.
i have increased the MAX priority in the config .h file to 10.
For the same 4 tasks (in case1) gave priorities like 9 6 5 8 (or any combination of 4 no.s b/w 0-9) it is not working.
why the tasks running on the priority base for 0 1 2 3 values fine, is not working for the 9 6 5 8 values.

The behaviours are different as we are expecting. please share any example projects.
for the Tasks running on priority base. where priority values should depend on the MAX Priority value in the config.h.

Thanks and Regards,
Mansoor Basha.

In which case valid priorities are 0 to 4. So in the original case of your first post tasks 1, 2 and 3 would all have priority 4, and task 4 will have priority 1 (I know in that case you said configMAX_PRIORITIES was 10).

We need to be very clear here. Is your task structured like this:

void myTask1(void*p)
{
    printf(“task1 is created\n”);
}

as per your original post, in which case after calling it will drop off the end of the function - which will cause a crash. Or is it structured in an infinite loop, or with a task delete at the end, in which case it will be safe?

void myTask1(void*p)
{
    printf(“task1 is created\n”);
    vTaskDelete( NULL );
}

void myTask1(void*p)
{
    printf(“task1 is created\n”);
    for( ;; )
    {
        // Whatever
    }
}

As documented, valid priorities are 0 to (configMAX_PRIORITIES - 1) - anything above that will be capped to (configMAX_PRIORITIES - 1).

We have many example projects in the FreeRTOS download, and as far as I am aware they all run on a priority basis as the tests check for that. I think the only way we can get to the bottom of the behaviour you are seeing is for you to provide a project where this doesn’t happen - but in a project I can replicate it - I don’t have the ARC IDE so Windows Simulator is best. I tried this in the Windows simulator port using the following code, then placing a break point at the start of each task function to see which order they executed in - this replicates the example from your original post, and all was fine:

void myTask1( void *v )
{
	vTaskSuspend( NULL );
}

void myTask2( void *v )
{
	vTaskSuspend( NULL );
}
void myTask3( void *v )
{
	vTaskSuspend( NULL );
}
void myTask4( void *v )
{
	vTaskSuspend( NULL );
}

/*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/
void main_blinky( void )
{
const TickType_t xTimerPeriod = mainTIMER_SEND_FREQUENCY_MS;


xTaskCreate(myTask1, ( signed char * ) "TX1",configMINIMAL_STACK_SIZE,NULL,9,NULL);
xTaskCreate(myTask2, ( signed char * ) "TX2",configMINIMAL_STACK_SIZE,NULL,4,NULL);
xTaskCreate(myTask3, ( signed char * ) "TX3",configMINIMAL_STACK_SIZE,NULL,10,NULL);
xTaskCreate(myTask4, ( signed char * ) "TX4",configMINIMAL_STACK_SIZE,NULL,1,NULL);

vTaskStartScheduler();
}

You can just add this code to this project, which runs in the free version of Visual Studio: FreeRTOS Real Time Kernel (RTOS) / Code / [r2837] /tags/V10.2.1/FreeRTOS/Demo/WIN32-MSVC