Resources sharing in Real Time System

I really don’t understand what is resources sharing in context of programming and real time system.

FreeRTOS is portable and can be ported to microcontroller. Microcontrollers have many in-built peripherals like UART, I2C, SPI, CAN, ADC, memory sharing, time sharing

Do real time tasks share with any peripheral it is called resource sharing?

Resource sharing is a general term, not specific to peripherals. A resource could be as simple as a variable. Suggest starting with the FreeRTOS book.

1 Like

Is this general term not applicable to hardware?

Multiple tasks can share physical devices but not at the same time.

What does it means in freeRTOS?

As mentioned, Resource Sharing can refer to many types of resources. Physical Devices are one type of resource. Generally, most physical devices can only do one thing at a time, so you need some form of sharing strategy to make it so only one task at a time is working with that device.

Generally for a Physical Device, either you write your application so only a single task manipulates the device, and any other task that needs something from it communicates with that task, or you set up a Mutex, and before a task wants to use the device, it takes the Mutex, then it can use the device knowing no other task will interfere, and then when it is done it gives the Mutex back so some other task can use it now.

The same can be done with other sorts of resources, like memory blocks or variables.

The basic idea is you ‘Share’ things by giving each user of that resource an period of exclusive access, which it then gives back so some other task can take its turn.

1 Like

Perhaps a more intuituve term than “sharing” would be “arbitrating.” An operating system allows multiple threads of execution controlled access to a ressource.

FreeRTOS’s kernel only arbitrates the CPU. In essence it’s a scheduler and a provider of synchronization objects that allow multiple threads of execution (tasks and ISRs) controlled and arbitrated access to resources like peripherals, but unlike operating systems such as Linux, there are no arbitration strategies for resources other than the CPU predefined by FreeRTOS.

1 Like

I recommend the Book by Brian Amos. Explains resource sharing in FreeRTOS and writing drivers, with copious examples.

If you’re an Arduino user then you should notice that the Serial.println() and alike is a shared resource that have serious issues in a multi-tasking environment.

This module wasn’t meant to be thread safe so get into real troubles when several tasks want to use it at the same time.

Another example is the ADC module in most microcontrollers. There’s only one ADC module that has 6, 9 or 16 channels. Most of the time one cannot convert multiple channels at once, so when a task initiate a convertion while there’s on ongoing one, the reading turns out corrupted.

That same scenario happens with the rest of shared resources: SPI, I2C, etc.

Thread safety is only one issue when dealing with resources. Another issue is abstraction: Many operating system provide abstract resource access mechanisms such as I/O models that allow applications to look at resources in a uniform matter. Consequently, device drivers for those operating systems must adhere to fairly strict coding conventions, such as exporting a predefined set of entry points that are being called from the OS and must behave in ways prescribed by the OS (for example, return success or error codes as required by the OS to further propagate those up and down the system).

FreeRTOS does not provide any of those abstractions. As a result, applications can not, for example, address UARTs or other communication interfaces via an Open/Read/Write/Close interface that treats those devices like other I/O streams (as they can, for example, when running as Windows or Linux applications).

It is not impossible that one (or more) vendors, foundations or other organizations roll up their sleeves and develop (possibly competing) uniform I/O models along with compliant device drivers on top of FreeRTOS, but it’s important to understand that the FreeRTOS kernel does NOT arbitrate anything other than the CPU(s), leaving arbitration of other resources to middleware and applications (possibly with the help of OS-provided synchronization primitives, as pointed out before).