FreeRTOS: What is Binary Semaphore and it's Advantages?


FreeRTOS: What Binary Semaphore and it's Advantages?

FreeRTOS is a popular real-time operating system that provides a range of synchronization mechanisms to enable tasks to communicate with each other and share resources in a coordinated manner. One of the key synchronization mechanisms provided by FreeRTOS is the binary semaphore.

What is Binary Semaphore in FreeRTOS?

In FreeRTOS, a Binary Semaphore is a synchronization object that can be used to control access to a shared resource or to synchronize the execution of multiple tasks. A binary semaphore in FreeRTOS has two states: "available" or "unavailable".

The xSemaphoreCreateBinary() API function is used to create a binary semaphore in FreeRTOS. This function returns a handle to the semaphore, which can be used to perform semaphore operations such as taking the semaphore (xSemaphoreTake()) and giving the semaphore (xSemaphoreGive()).

The xSemaphoreTake() function is used by a task to request ownership of the semaphore. If the semaphore is available, the function immediately returns and the task takes ownership of the semaphore. If the semaphore is unavailable (already taken by another task), the function blocks the task until the semaphore becomes available.

The xSemaphoreGive() function is used by a task to release ownership of the semaphore. If any tasks are blocked waiting for the semaphore, the highest priority waiting task will be unblocked and given ownership of the semaphore.

Binary semaphores are often used in FreeRTOS to implement mutual exclusion and to synchronize the execution of multiple tasks. For example, a binary semaphore can be used to protect a shared resource such as a hardware peripheral, ensuring that only one task at a time can access the resource. 

How Binary Semaphore in FreeRTOS works?

In FreeRTOS, binary semaphores work in a similar way to binary semaphores in other operating systems or real-time systems. Here's how a binary semaphore in FreeRTOS works:

Create the Binary Semaphore: The binary semaphore is created using the xSemaphoreCreateBinary() function. The function returns a handle to the semaphore, which can be used to perform semaphore operations such as taking the semaphore (xSemaphoreTake()) and giving the semaphore (xSemaphoreGive()).

Initialize the semaphore: The semaphore is initialized with an initial value of either 1 or 0, depending on the implementation. If the initial value is 1, the semaphore is said to be "available", and if it is 0, the semaphore is said to be "unavailable".

Task requests the semaphore: When a task wants to access the shared resource, it requests ownership of the semaphore by calling the xSemaphoreTake() function. If the semaphore value is 1 (available), the task takes ownership of the semaphore and sets the semaphore value to 0 (unavailable). If the semaphore value is already 0 (unavailable), the task is blocked and put into a waiting state until the semaphore becomes available.

Task releases the semaphore: When a task is done with the shared resource, it releases ownership of the semaphore by calling the xSemaphoreGive() function. This increments the semaphore value from 0 to 1, making the semaphore available again. If there are any tasks waiting for the semaphore, the highest priority waiting task is unblocked and given ownership of the semaphore.

Binary semaphores in FreeRTOS can be used to implement mutual exclusion and to signal events between tasks. They can be used to protect shared resources such as hardware peripherals, ensuring that only one task at a time can access the resource. Additionally, binary semaphores can be used to synchronize the execution of multiple tasks, such as when one task needs to wait for another task to finish before proceeding.

When to use Binary Semaphore in freeRTOS?

Binary semaphores in FreeRTOS are typically used in situations where mutual exclusion or simple task synchronization is required. Here are some common situations where binary semaphores might be used in FreeRTOS:

Protecting a shared resource: If multiple tasks need to access a shared resource such as a hardware peripheral or a memory buffer, a binary semaphore can be used to protect the resource. Only one task at a time can hold the semaphore, ensuring that multiple tasks don't attempt to access the resource simultaneously.

Task synchronization: If one task needs to wait for another task to complete a certain operation before proceeding, a binary semaphore can be used to signal completion. The waiting task can use the xSemaphoreTake() function to wait for the semaphore to be given by the other task.

Controlling access to a critical section: A critical section is a section of code that must be executed atomically, without interruption from other tasks or interrupts. A binary semaphore can be used to protect a critical section, ensuring that only one task at a time can execute the critical section.

Event signaling: A binary semaphore can be used to signal an event between tasks. For example, if one task is waiting for a specific condition to occur before proceeding, another task can use the xSemaphoreGive() function to give the semaphore and signal the event.

In general, binary semaphores are a useful synchronization primitive in FreeRTOS when you need to coordinate the access to a shared resource or to synchronize the execution of multiple tasks.

What are the advantages of using Binary Semaphore in FreeRTOS?

There are several advantages to using binary semaphores in FreeRTOS:

  1. Simple and lightweight synchronization: Binary semaphores provide a simple and lightweight way to synchronize the execution of multiple tasks in FreeRTOS. They are easy to use and don't require a lot of overhead or configuration.
  2. Efficient resource protection: Binary semaphores can be used to protect shared resources such as hardware peripherals or memory buffers. They provide a way to ensure that only one task at a time can access the resource, which helps prevent race conditions and other synchronization problems.
  3. Priority inversion prevention: Binary semaphores in FreeRTOS implement priority inheritance, which helps prevent priority inversion problems. Priority inversion occurs when a high-priority task is blocked by a low-priority task holding a resource. With priority inheritance, the priority of the low-priority task is temporarily raised to the priority of the high-priority task while the high-priority task is waiting for the resource.
  4. Event signaling: Binary semaphores can be used to signal events between tasks in FreeRTOS. This provides a way for tasks to communicate with each other without requiring more complex synchronization mechanisms.
  5. Easy to integrate with other synchronization mechanisms: Binary semaphores can be used in conjunction with other synchronization mechanisms in FreeRTOS, such as mutexes or queues. This allows for more complex synchronization scenarios to be implemented as needed.

The Binary semaphores in FreeRTOS provide a simple and efficient way to synchronize tasks and protect shared resources. They are a valuable tool for developing reliable and efficient real-time systems.

What are the limitations of using Binary Semaphore ?

While binary semaphores in FreeRTOS are a powerful synchronization mechanism, there are some limitations that should be considered when using them:

  1. Limited to binary synchronization: Binary semaphores are limited to binary synchronization, which means they can only represent two states - available and unavailable. This may not be sufficient for some synchronization scenarios that require more complex states or conditions.
  2. Can cause priority inversion: While binary semaphores in FreeRTOS implement priority inheritance to prevent priority inversion, there are still some situations where priority inversion can occur. For example, if multiple tasks are waiting on the same semaphore, priority inversion may occur if the higher priority task is blocked by the lower priority task.
  3. Potential for deadlock: If binary semaphores are not used correctly, they can lead to deadlock - a situation where tasks are blocked waiting for resources that are held by other tasks. Deadlock can be difficult to detect and resolve, and can cause the system to become unresponsive.
  4. Not suitable for some synchronization scenarios: Binary semaphores may not be suitable for some synchronization scenarios, such as those that require more complex synchronization mechanisms like condition variables or read/write locks.
  5. Can be prone to race conditions: If binary semaphores are not used carefully, they can be prone to race conditions - situations where the timing of events can cause unexpected behavior or errors in the system.

APIs for Binary Semaphore in FreeRTOS

FreeRTOS provides a set of APIs for working with binary semaphores. Here are the key APIs for using binary semaphores in FreeRTOS:

xSemaphoreCreateBinary(): This API creates a new binary semaphore and returns a handle to the semaphore. The initial state of the semaphore is set to "unavailable", which means that the first task that takes the semaphore will be blocked.

xSemaphoreTake(): This API is used by a task to take the binary semaphore. If the semaphore is currently unavailable, the task will be blocked until the semaphore becomes available.

xSemaphoreGive(): This API is used by a task to give the binary semaphore back. If there are tasks waiting for the semaphore, the highest priority task will be unblocked and can take the semaphore.

xSemaphoreGiveFromISR(): This API is similar to xSemaphoreGive(), but is used to give the semaphore back from an interrupt service routine (ISR).

xSemaphoreTakeFromISR(): This API is similar to xSemaphoreTake(), but is used to take the semaphore from an ISR.

vSemaphoreDelete(): This API deletes a binary semaphore that was created using xSemaphoreCreateBinary().

These APIs provide a simple and easy-to-use interface for working with binary semaphores in FreeRTOS. By using these APIs, tasks can synchronize their execution and protect shared resources with minimal overhead and configuration.

Code Example for Implementing Binary Semaphore

The code uses ESP32 and Arduino IDE to implement the binary semaphore

#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif

SemaphoreHandle_t binarySemaphore;

void taskMaster(void *pvParameters)
{
    while (1)
    {
        
         Serial.println("Master Gives signal");
         xSemaphoreGive(binarySemaphore);
         Serial.println("Master finishesh Giving the signal");
         vTaskDelay(2000 / portTICK_PERIOD_MS);         
         Serial.println();
         Serial.println();
    }
}



void taskServent (void *pvParameters)
{
    while (1)
    {
        if (xSemaphoreTake(binarySemaphore, portMAX_DELAY) == pdTRUE)
        {
            
            Serial.println("servent task takes the semaphore");
            Serial.println("servent finishes executing the task ");
            
            Serial.println();
            Serial.println();   
            
        }
    }
}

void setup()
{
    Serial.begin(115200);

    binarySemaphore = xSemaphoreCreateBinary();

    xTaskCreatePinnedToCore(taskMaster, "Task 1", 1024, NULL, 1,NULL ,ARDUINO_RUNNING_CORE);
    xTaskCreatePinnedToCore(taskServent, "Task 2", 1024, NULL, 2,NULL, ARDUINO_RUNNING_CORE);
    
}

void loop()
{
    // Do nothing
}

Conclusion

In conclusion, binary semaphores are a valuable synchronization mechanism in FreeRTOS that can be used to protect shared resources, synchronize task execution, prevent priority inversion, and signal events between tasks. They provide a simple and lightweight way to synchronize tasks, and can be easily integrated with other synchronization mechanisms in FreeRTOS. However, there are some limitations to their use, including their binary nature, potential for deadlock and race conditions, and limitations in more complex synchronization scenarios. It is important to carefully consider the advantages and limitations of binary semaphores when using them in FreeRTOS, and to use them appropriately in order to develop reliable and efficient real-time systems.

Related YouTube Video

FreeRTOS: Using Binary semaphore|ESP32 and Arduino IDE



Post a Comment

Previous Post Next Post