Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


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 ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

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

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

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner