Consider the following FreeRTOS code snippet that creates a binary semaphore and attempts to take it without giving it first. What will be the output?
#include "FreeRTOS.h" #include "semphr.h" #include "task.h" #include <stdio.h> SemaphoreHandle_t xSemaphore; void vTaskFunction(void *pvParameters) { if (xSemaphoreTake(xSemaphore, (TickType_t)10) == pdTRUE) { printf("Semaphore taken successfully\n"); } else { printf("Failed to take semaphore\n"); } vTaskDelete(NULL); } int main(void) { xSemaphore = xSemaphoreCreateBinary(); if (xSemaphore == NULL) { printf("Failed to create semaphore\n"); return 1; } xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Think about the initial state of a binary semaphore created with xSemaphoreCreateBinary() and whether it is given before taking.
A binary semaphore created with xSemaphoreCreateBinary() starts in the 'empty' state. Since the semaphore is never given before the task tries to take it, the xSemaphoreTake() call times out and returns pdFALSE, so the output is "Failed to take semaphore".
Given this FreeRTOS code snippet, what will be the output?
#include "FreeRTOS.h" #include "semphr.h" #include "task.h" #include <stdio.h> SemaphoreHandle_t xSemaphore; void vTaskFunction(void *pvParameters) { if (xSemaphoreTake(xSemaphore, (TickType_t)10) == pdTRUE) { printf("Semaphore taken successfully\n"); } else { printf("Failed to take semaphore\n"); } vTaskDelete(NULL); } int main(void) { xSemaphore = xSemaphoreCreateBinary(); if (xSemaphore == NULL) { printf("Failed to create semaphore\n"); return 1; } xSemaphoreGive(xSemaphore); xSemaphoreGive(xSemaphore); xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Remember that binary semaphores can only be 'given' once before they must be 'taken'. What happens if you give twice?
Giving a binary semaphore twice without taking it first does not increase its count beyond 1. The second give call has no effect. Therefore, the semaphore is available when the task tries to take it, so the output is "Semaphore taken successfully".
Examine the code below. The task waits indefinitely on the semaphore, but the semaphore is never given. What is the cause of this deadlock?
#include "FreeRTOS.h" #include "semphr.h" #include "task.h" #include <stdio.h> SemaphoreHandle_t xSemaphore; void vTaskFunction(void *pvParameters) { if (xSemaphoreTake(xSemaphore, portMAX_DELAY) == pdTRUE) { printf("Semaphore taken\n"); } vTaskDelete(NULL); } int main(void) { xSemaphore = xSemaphoreCreateBinary(); if (xSemaphore == NULL) { printf("Failed to create semaphore\n"); return 1; } xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Check if the semaphore is ever given in the code.
The binary semaphore is created but never given. The task waits indefinitely with portMAX_DELAY on xSemaphoreTake(), so it blocks forever, causing a deadlock.
Choose the code snippet that correctly creates a binary semaphore and gives it so that a task can take it.
Remember the function to create a binary semaphore and how to make it available initially.
Option C correctly creates a binary semaphore and gives it to make it available. Option C creates a mutex, not a binary semaphore. Option C takes the semaphore instead of giving it. Option C creates a counting semaphore, not binary.
In FreeRTOS, if you create a binary semaphore and call xSemaphoreGive() multiple times before any xSemaphoreTake(), how many times will the semaphore count increase?
Think about the nature of binary semaphores and their maximum count.
A binary semaphore can only be 'given' once before it must be 'taken'. Additional calls to xSemaphoreGive() while the semaphore is already available do not increase its count beyond 1.
