Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


Need help on priorities

Posted by kuku2010 on January 6, 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!

RE: Need help on priorities

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

RE: Need help on priorities

Posted by kuku2010 on January 6, 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?

RE: Need help on priorities

Posted by kuku2010 on January 6, 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?


RE: Need help on priorities

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

RE: Need help on priorities

Posted by kuku2010 on January 9, 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!

RE: Need help on priorities

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

RE: Need help on priorities

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

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

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists