Complete the code to create a counting semaphore with 3 resources available.
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting([1], [1]);
The counting semaphore is created with a maximum count and initial count both set to 3, representing 3 available resources.
Complete the code to take (acquire) the semaphore, waiting indefinitely if not available.
if(xSemaphoreTake(xSemaphore, [1]) == pdTRUE) { // Access resource }
Using portMAX_DELAY makes the task wait indefinitely until the semaphore is available.
Fix the error in releasing the semaphore after using the resource.
xSemaphore[1](xSemaphore);To release the semaphore and return the resource, use xSemaphoreGive().
Fill both blanks to create a counting semaphore with a maximum of 4 resources and initial count of 2.
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting([1], [2]);
The maximum count is 4, meaning 4 resources total. The initial count is 2, meaning 2 resources are initially available.
Fill all three blanks to check if the semaphore can be taken without waiting, and handle success or failure.
if(xSemaphoreTake(xSemaphore, [1]) == [2]) { // Resource acquired } else { // Resource not available [3] }
Using 0 as block time means do not wait. If xSemaphoreTake returns pdTRUE, resource is acquired. Otherwise, delay the task briefly.
