Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Counting semaphore for resource pools in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Counting Semaphore Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Counting Semaphore Initial Count Behavior
Consider a counting semaphore created with an initial count of 3 in FreeRTOS. If 4 tasks try to take the semaphore without giving it back, what happens to the fourth task?
FreeRTOS
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting(3, 3);

// Four tasks call xSemaphoreTake(xSemaphore, portMAX_DELAY) one after another
AThe fourth task skips taking the semaphore and continues execution.
BThe fourth task causes a runtime error because the semaphore count is exceeded.
CThe fourth task blocks indefinitely until one of the first three tasks gives the semaphore back.
DThe fourth task immediately takes the semaphore without blocking.
Attempts:
2 left
💡 Hint
Think about how counting semaphores limit concurrent access to resources.
❓ Predict Output
intermediate
2:00remaining
Semaphore Count After Multiple Give and Take
Given a counting semaphore created with max count 5 and initial count 2, what is the count after these operations? 1. Take semaphore once 2. Give semaphore twice 3. Take semaphore three times
FreeRTOS
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting(5, 2);

xSemaphoreTake(xSemaphore, 0);
xSemaphoreGive(xSemaphore);
xSemaphoreGive(xSemaphore);
xSemaphoreTake(xSemaphore, 0);
xSemaphoreTake(xSemaphore, 0);
xSemaphoreTake(xSemaphore, 0);
AThe semaphore count is 0 after these operations.
BThe semaphore count is 2 after these operations.
CThe semaphore count is 1 after these operations.
DThe semaphore count is 3 after these operations.
Attempts:
2 left
💡 Hint
Track the count step by step, considering max count limits.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Semaphore Usage
This code snippet uses a counting semaphore to control access to a pool of 2 resources. What is the bug that can cause a deadlock?
FreeRTOS
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting(2, 2);

void task(void *params) {
    if (xSemaphoreTake(xSemaphore, portMAX_DELAY) == pdTRUE) {
        // Use resource
        // Missing xSemaphoreGive(xSemaphore);
    }
    vTaskDelete(NULL);
}
AThe semaphore is created with wrong initial count causing immediate failure.
BThe semaphore is never given back, causing other tasks to block forever.
CThe task deletes itself before taking the semaphore causing undefined behavior.
DThe semaphore take uses zero timeout causing busy waiting.
Attempts:
2 left
💡 Hint
Think about what happens if a task never releases the semaphore.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Creating a Counting Semaphore
Which of the following code snippets correctly creates a counting semaphore with max count 4 and initial count 2 in FreeRTOS?
ASemaphoreHandle_t sem = xSemaphoreCreateCounting(4, 2);
BSemaphoreHandle_t sem = xSemaphoreCreateCounting(2);
CSemaphoreHandle_t sem = xSemaphoreCreateCounting(4);
DSemaphoreHandle_t sem = xSemaphoreCreateCounting(2, 4);
Attempts:
2 left
💡 Hint
Check the order and number of parameters for xSemaphoreCreateCounting.
🚀 Application
expert
3:00remaining
Designing a Resource Pool with Counting Semaphore
You want to manage a pool of 3 identical hardware peripherals using a counting semaphore in FreeRTOS. Which approach correctly ensures that no more than 3 tasks use the peripherals simultaneously and that tasks wait if all are busy?
ACreate a mutex and allow 3 tasks to lock it simultaneously without blocking.
BCreate a binary semaphore and give it 3 times initially. Tasks take the semaphore once before use and give it once after use.
CCreate a counting semaphore with max count 1 and initial count 3. Tasks take the semaphore before use and give it after use.
DCreate a counting semaphore with max count 3 and initial count 3. Each task calls xSemaphoreTake before using a peripheral and xSemaphoreGive after finishing.
Attempts:
2 left
💡 Hint
Think about how counting semaphores limit concurrent access to multiple identical resources.

Practice

(1/5)
1. What is the main purpose of a counting semaphore in FreeRTOS when managing resource pools?
easy
A. To track the number of available identical resources
B. To create tasks with different priorities
C. To store data between tasks
D. To handle interrupts directly

Solution

  1. Step 1: Understand counting semaphore role

    A counting semaphore keeps track of how many identical resources are free or in use.
  2. Step 2: Compare with other options

    Creating tasks, storing data, or handling interrupts are not roles of counting semaphores.
  3. Final Answer:

    To track the number of available identical resources -> Option A
  4. Quick Check:

    Counting semaphore = resource count tracker [OK]
Hint: Counting semaphore counts free resources, not tasks or data [OK]
Common Mistakes:
  • Confusing counting semaphore with binary semaphore
  • Thinking it creates or manages tasks
  • Assuming it stores data
2. Which of the following is the correct way to create a counting semaphore with a maximum count of 5 and an initial count of 3 in FreeRTOS?
easy
A. xSemaphoreCreateCounting(3, 5);
B. xSemaphoreCreateCounting(5, 3);
C. xSemaphoreCreateCounting(5);
D. xSemaphoreCreateCounting(3);

Solution

  1. Step 1: Recall function parameters

    The function xSemaphoreCreateCounting takes max count first, then initial count.
  2. Step 2: Match parameters to options

    xSemaphoreCreateCounting(5, 3); uses (5, 3) which means max count 5 and initial count 3, correct order.
  3. Final Answer:

    xSemaphoreCreateCounting(5, 3); -> Option B
  4. Quick Check:

    Max count first, initial count second [OK]
Hint: Remember max count is first parameter, initial count second [OK]
Common Mistakes:
  • Swapping max and initial count parameters
  • Using only one parameter
  • Confusing with binary semaphore creation
3. Given the following code snippet, what will be the value of the semaphore count after both tasks successfully take the semaphore once?
SemaphoreHandle_t sem = xSemaphoreCreateCounting(3, 3);
xSemaphoreTake(sem, 0);
xSemaphoreTake(sem, 0);
medium
A. 3
B. 0
C. 2
D. 1

Solution

  1. Step 1: Understand initial semaphore count

    The semaphore starts with count 3 (max and initial both 3).
  2. Step 2: Subtract count for each take

    Each xSemaphoreTake reduces count by 1 if successful. Two takes reduce count by 2.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    3 - 2 takes = 1 [OK]
Hint: Each take reduces count by one if successful [OK]
Common Mistakes:
  • Assuming count stays the same after take
  • Confusing take with give operation
  • Thinking count can go below zero
4. Identify the error in this code snippet that uses a counting semaphore for a resource pool:
SemaphoreHandle_t sem = xSemaphoreCreateCounting(2, 2);
xSemaphoreGive(sem);
medium
A. xSemaphoreGive should be called before creation
B. xSemaphoreCreateCounting parameters are reversed
C. Calling xSemaphoreGive when semaphore count is at max causes overflow
D. No error, code is correct

Solution

  1. Step 1: Check initial semaphore count

    The semaphore is created with max count 2 and initial count 2, so it's full.
  2. Step 2: Analyze xSemaphoreGive call

    Giving semaphore when count is already max causes overflow error or undefined behavior.
  3. Final Answer:

    Calling xSemaphoreGive when semaphore count is at max causes overflow -> Option C
  4. Quick Check:

    Giving at max count causes overflow [OK]
Hint: Don't give semaphore if count is already at max [OK]
Common Mistakes:
  • Ignoring semaphore max count limits
  • Swapping max and initial counts
  • Calling give before creating semaphore
5. You have a resource pool of 4 identical devices. You want to create a counting semaphore to manage access. Initially, 2 devices are free. Which code snippet correctly creates and initializes the semaphore for this scenario?
hard
A. SemaphoreHandle_t sem = xSemaphoreCreateCounting(4, 2);
B. SemaphoreHandle_t sem = xSemaphoreCreateCounting(2, 2);
C. SemaphoreHandle_t sem = xSemaphoreCreateCounting(4, 4);
D. SemaphoreHandle_t sem = xSemaphoreCreateCounting(2, 4);

Solution

  1. Step 1: Identify max and initial counts

    Max count is total devices (4), initial count is free devices (2).
  2. Step 2: Match parameters to function call

    Correct call is xSemaphoreCreateCounting(4, 2) with max 4 and initial 2.
  3. Final Answer:

    SemaphoreHandle_t sem = xSemaphoreCreateCounting(4, 2); -> Option A
  4. Quick Check:

    Max = total devices, initial = free devices [OK]
Hint: Max count = total resources, initial count = free resources [OK]
Common Mistakes:
  • Swapping max and initial counts
  • Setting initial count higher than max
  • Using max count less than total resources
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard