Why a lower priority task created before higher priority task get executed always? I have turned off time slicing.
Why not if it’s the highest prio ready task at this point of time ?
What do you mean by executed always? Do you mean only the lower priority task ever executes (executes all the time) and the higher priority task never executes? Or do you mean the lower priority task executes before the high priority task but both low and high do execute? If the second case please show an example of where you think that happens.
The first case, higher priority never executes.
What happens when a high priority task enters Ready state? Since it is having the highest priority, it should preempt the lower priority task, right?
Exactly. But if the higher prio task never executes as reported something else if wrong.
Do you check the return values of
xTaskCreate and did you define configASSERT and enabled stack overflow checking as recommended ?
I just tried this demo, which creates two application tasks, a send task and a receive task. I set the priority of the timer task to 0, the send task to 1, and the receive task to 2 - and turned off time slicing. By putting a break point at the start of each task I could see, as expected as it was the highest priority, the receive task executed first. I then switched the priorities of the send and receive task. This time, as as expected as it was now the highest priority, the send task executed first. So I was not able to replicate your result.
How are you determining that your higher priority task is not executing?
I placed print statements in both the tasks.
Are you sure that your print function is thread safe and nothing else went wrong ?
Any chance to use a debugger and use appropriate breakpoints ?
BTW which MCU or port do you use ?
I used GUI flash tool.
I am using NXP’s FRDM K64F series MCU
If two tasks are created back to back with tasks having different priority, if lower priority is created first but the scheduler is started only after creating tasks, then why the lower priority task is running initially.
This is not the case and it would be a fundamental bug otherwise.
Just check yourself looking up the FreeRTOS source port.c - xPortStartScheduler().
There is a problem or misunderstanding somewhere else.
How are you confirming that the lower priority task is running first? If you are relying on prints, then as @hs2 pointed out, it might not be thread safe. Can you remove prints and confirm the same by putting a breakpoint in the debugger instead?
Also, it may be helpful to share code snippet showing the problem so that we can look for any obvious errors.
I have placed breakpoints in both tasks. The lower priority task if created first is executed first.
The next step would be to post the relevant code, but it seems you are already on the right track to resolve your issue.
You’ll see that after fixing the stack overflow problem things will work as expected and documented. Good luck !
Well, I did that beforehanded and the execution was successful and as expected.
What does one exactly mean by thread safety?