Quality RTOS & Embedded Software

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


Best way to access ressource between task

Posted by ogh on June 11, 2015


I'm asking myself which is the best way to access to a shared resource between task, for example: I've a file containing raw data and two task that should access to this resources: -) The first task is getting data from serial port one and should write data to the file. -) The second task should read data from this file and send these to another serial port (based on external request). The first solution is to use a Mutex on this file. The other solution I'm thinking is to create a third task managing access to this file and receiving request (and data) from other other task and sending data to other task through Queue. Which is the best solution?

Thanks for your help

Best way to access ressource between task

Posted by heinbali01 on June 11, 2015

It depends on how complex the actions will be. Are you using real files in a FAT (or other) file-system? In that case a third task could open the file in update mode ("w+") and provide read- and write-services. That task can indeed open a QueueHandle_t and receive commands.

Note that a QueueHandle_t is not ideal for hi-volume data exchange. It is useful for small messages, for instance a token (enum), a pointer to data, or a pointer to a (command) structure.

If the "file" is not really a file but just shared memory, a simpler mutex of binary semaphore will be sufficient and a lot cheaper.


Best way to access ressource between task

Posted by ogh on June 12, 2015

Thanks for your answer. You 're right I'm using a FAT system (FatsFS and STM32Cube lib) on a SD card. Data that tasks should pass through Queue will not exceed 10 bytes.

Best way to access ressource between task

Posted by heinbali01 on June 12, 2015

I'm using a FAT system (FatsFS and STM32Cube lib)

I'm not sure how FatFS (by ChaN) will behave if you open the same file two times: once in read- and once in write-mode.

FreeRTOS' own +FAT library has decided to either allow to open a file by:

  • a single task using "w" or "w+" mode
  • multiples tasks all using "r" mode

In other words: there may be many readers but just one writer.

Writing data to a file is often the realisation of a transaction. It may be a payment, a reservation, a commitment. Now if you think of it as transactions, it becomes more meaningful to introduce a third party (task) who can do the transactions. This task will handle one transaction at a time without getting interrupted, you make that sure by design.

One product I make has four FM tuners. It is very easy to send some SPI data to a tuner and change the frequency. I have several tasks which must interact with the tuners somehow, in read- and/or write-more. Doing that directly gave problems at some point, and so I also introduced a separate

void vTunerTask( void *pvParameters )

which handles request for all four FM tuners, and that works well.


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

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

Latest News

Version 10.1.0 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.


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


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