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




Loading

Use of semaphores

Posted by Sachin Bhujbal on April 2, 2009
Dear All,

In my application,There are lot of variables( more than 200 ) which are shared between the tasks simultaneously.
My question is
Should I use Semaphores or mutexes for each variable ?
If I used this much Semaphores what will be RAM
consumption ?

Sachin D. Bhujbal



RE: Use of semaphores

Posted by Samuel Tardieu on April 2, 2009
It depends on many things:

- what is the size of the variables compared to the processor word size?
- will several tasks update those variables?
- do you update each variable independently or do you need to update them at the same time?

If all your variables are of type portBASE_TYPE (or equivalent), if they are accessed independently, and if only one task can update them, then you're probably safe with no locking at all.

Otherwise, you can use a mutex (which will bring you priority inheritance) by variable (and yes, it will consume a lot of RAM), or only one for the whole variables set (it will be a giant lock).

Clearly, you need to be more precise in your description if you want to obtain useful help.

RE: Use of semaphores

Posted by Sachin Bhujbal on April 2, 2009
OK,
Actually, my requirement is clearified in one sentence only.
We will take a example to make it more clear.
consider following arrays of structures,
{
ucVariable1;
usVariable2;
usVariable3;
ulVariable4;
ullVariable5;
.
.
ulVariable30;
}gastStructure1[4]

{
ucVariable1;
usVariable2;
usVariable3;
ulVariable4;
ullVariable5;
.
.
ulVariable30;
}gastStructure2[4]
.
.
.
{
ucVariable1;
usVariable2;
usVariable3;
ulVariable4;
ullVariable5;
.
.
ulVariable30;
}gastStructure15[4]

And consider Tasks A, B, C, D, E, F, G

Now in my case
Task A writes ucVariabel1 in gastStructure1[0].
Task B writes ucVariabel1 in gastStructure1[0].
Task C reads ucVariabel1 in gastStructure1[0].

It is sure that I will require to use Semaphore or Mutex for ucVariable1 in gastStructure1 in Task A & B.

But task C only reads it, so do I required to use semaphore in
Task C also ?

And as explained above each Variable in each structure is shared like that.
Your suggestion about using semaphore to group of variables is good but in my application, those variables cannot be grouped, as each variable is dedicated for specific requirement by different tasks.

Can there be some other solution for such logic ?

Sachin D. Bhujbal

RE: Use of semaphores

Posted by incrediball on April 2, 2009
Actually your requirements are still not very clear. Why would you need so many variables that are just called "variable", and all for some unrelated purpose? I cannot think of any serious application that needs to do something like that.

Data shared by tasks is usually limited to a small (e.g. 1 or 2 or so) number of objects. If there are more than that then the objects are usually passed from one task to the next using a queue, in which case your synchronisation issues are dealt with automatically. Note that with the abstract term "object" I am not referring to a C++ object but some entity, i.e. a collection of values that belong to something larger. It is these objects that you firstly need to define and then you need to protect them as a whole so that their content does not become inconsistent due to a race condition.


[ 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