Need help on priorities

kuku2010 wrote on Friday, January 06, 2012:

Hi all,

Is there any response/timing difference in the following two cases? I am focusing on the thread in higher priority.

Only two threads in both cases:

1) one with priority of 3, the other 5.
2) one with priority of 2, the other 8.

Thanks for clarification!

richard_damon wrote on Friday, January 06, 2012:

FreeRTOS uses strict priorities, so the numerical value doesn’t really matter, just which task has the highest value. FreeRTOS will always be running the highest priority task that is ready to run.  If there are multiple tasks at a given priority, then they will be cycled through every timer tick. If a higher priority task never blocks on something, lower priority tasks will NOT get any time (as opposed to a soft priority system which might give some time to a lower priority task)

There is one small effect in your case, having empty priority levels does have some cost, one in memory to create the ready queue for the empty priority level, and a time cost to find the highest available task, as the scheduler will need to check each of the unused priority levels to see that there is no ready task at that level.

kuku2010 wrote on Friday, January 06, 2012:

Thanks to richard_damon,

When you say “If a higher priority task never blocks on something, lower priority tasks will NOT get any time”, do you mean I need to insert

vTaskDelay( 0 );

in the higher priority thread to make sure the lower one gets attended?

kuku2010 wrote on Friday, January 06, 2012:

ichard_damon, Actually, I am at lost and I need your pointer again:

Say, I have these two threads, each handles its own job. job1 for thread 1, job2 for thread 2.

job1 takes way less than 1 ms to complete. 99% time, job2 takes way less than 1 ms to complete, but may higher once a while/ (assuming the time slice is 1ms here)

I would like thread 1 to be attended every 1ms,  and thread 2 will use what ever remaining CPU time

Originally,  I thought I should set thread 1 to higher priority, with vTaskDelay (1) at the end of its job to give thread 2 a chance to work on it, but it seems this can NOT guarantee job 1 to have 1ms monitoring accuracy.

What should I do?


richard_damon wrote on Saturday, January 07, 2012:

First, a vTaskDelay(0) is, I believe, basically the same as a taskYield() which does NOT give time to lower priority tasks. The task needs to block on some condition saying it doesn’t have anything more to do now, and the block will end when it has something more to do. The block can be a vTasDelay() to indicate a time when it should restart, or a wait on a queue/semaphore of some sort to wait for that to become ready.

A vTskDelay(1) will block the task till the next timer tick, at which point it will be make back ready, and will be switched to UNLESS there is a task of higher priority that is also ready to run. If a given task is the highest priority, then it WILL run on the next timer tick, unless some other task is being unfriendly and does a long critical section or scheduler disable block.

If your delay is bigger than 1 tick, and you want a constant period, than look at vTaskDelayUntil as a possibly better option as it sets the delay based on the time this run started as opposed to the current time.

Also, you are describing that these tasks are getting new information to process on a time basis, is this really what is happening? I tend to find that I am actually processing the data from a divide (like an A/D converter) and it actually makes more sense to have (if possible) an interrupt be triggered when the data is available, and the interrupt signal the task with a queue or semaphore, and that starts up the processing task.

kuku2010 wrote on Monday, January 09, 2012:

Thanks richard_damon,

No, this is not interrupt-based readings.

The project can be simplified as

Thread one is monitoring UDP packet, which should be at a rate of 1000Hz to capture some critical information
Thread two is monitoring less time crtical TCP packets.

Thanks for your time!

richard_damon wrote on Tuesday, January 10, 2012:

Actually I am fairly sure the ethernet adaptor  generated an interrupt when the UDP packet arrived, and some routine did something to get the packet out of the hardware and got things ready for the next packet, otherwise you are just going to lose too many packets.

This routine should then tell your task that a new packet has arrived, and process it as soon as it arrives. If you do this on a periodic interrupt, at the same rate as they should be received, will cause a typical delay between packet arriving and being processed  of 1/2 ms and possible as high as a full ms. It is also subject to missing packets at time. For example, if the packets are arriving normally just before you timer interrupt, if one packet is slightly delayed (as is the want of ethernet) and arrives just after the timer interrupt, Task 1 won’t see it, and re-run on the previous packet. If the next packet arrives as normal, it will overwrite the delayed packeted and Task 1 will never see the delayed packet.

kuku2010 wrote on Tuesday, January 10, 2012:

I fully understand that interrupt can do a much better job, but this requires reading into the TCP stacks. This is a quick project, and if FreeRTOS can guarantee a 1ms rate for the UDP thread, my project is done, and that saves a lot of time.

So can I config FreeRTOS to guarantee a 1ms rate for a thread while the other picks up the remaining CPU time?

Thanks for your reply in advance!