Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Counting semaphore for resource pools in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Counting semaphore for resource pools
Initialize semaphore count = N
↓
Task requests resource
↓
Check semaphore count > 0?
No→Task waits
Yes↓
Decrement semaphore count
↓
Task uses resource
↓
Task releases resource
↓
Increment semaphore count
↓
Task requests resource
The counting semaphore starts with a count equal to available resources. Tasks request resources by decrementing the count if available, or wait if none. When done, tasks release resources by incrementing the count.
Execution Sample
FreeRTOS
SemaphoreHandle_t sem = xSemaphoreCreateCounting(3, 3);

// Task tries to take resource
if(xSemaphoreTake(sem, 0) == pdTRUE) {
    // Use resource
    xSemaphoreGive(sem);
}
This code creates a counting semaphore with 3 resources, takes one if available, uses it, then releases it.
Execution Table
StepSemaphore CountActionResultTask State
13Task tries to take resourceCount > 0, take succeedsRunning
22Task uses resourceResource in useRunning
33Task releases resourceCount incrementedRunning
43Next task tries to take resourceCount > 0, take succeedsRunning
52Task uses resourceResource in useRunning
63Task releases resourceCount incrementedRunning
73Task tries to take resourceCount > 0, take succeedsRunning
82Task uses resourceResource in useRunning
93Task releases resourceCount incrementedRunning
100Task tries to take resourceCount == 0, task waitsBlocked
111Resource released by another taskCount incrementedUnblocked
💡 Execution stops when tasks no longer request resources or system shuts down.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 7After Step 9After Step 10After Step 11
Semaphore Count323333301
Task StateIdleRunningRunningRunningRunningRunningRunningBlockedUnblocked
Key Moments - 3 Insights
Why does the task wait when semaphore count is zero?
When the count is zero (see Step 10 in execution_table), it means no resources are free. The task must wait until another task releases a resource and increments the count.
What happens to the semaphore count when a task takes a resource?
The semaphore count decreases by one (see Step 1 and Step 4). This shows one resource is now in use and unavailable to others.
How does releasing a resource affect waiting tasks?
Releasing a resource increments the count (Step 3, 6, 9). If tasks are waiting (Step 10), one will unblock and take the resource (Step 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the semaphore count after Step 4?
A3
B0
C2
D1
💡 Hint
Check the 'Semaphore Count' column at Step 4 in the execution_table.
At which step does the task become blocked waiting for a resource?
AStep 8
BStep 10
CStep 6
DStep 3
💡 Hint
Look for 'Blocked' in the 'Task State' column in the execution_table.
If the initial semaphore count was 2 instead of 3, what would happen at Step 10?
ATask would take resource and count becomes 0
BTask would not wait because count is 1
CTask would still wait because count is zero
DTask would fail to take resource immediately
💡 Hint
Refer to variable_tracker for how semaphore count changes and when task blocks.
Concept Snapshot
Counting Semaphore for Resource Pools:
- Initialize with count = number of resources
- xSemaphoreTake() decrements count if > 0, else task waits
- xSemaphoreGive() increments count, releasing resource
- Controls access to limited identical resources
- Tasks block when no resources free
- Tasks unblock when resources released
Full Transcript
A counting semaphore manages a pool of identical resources by keeping a count of how many are free. When a task wants a resource, it tries to take the semaphore. If the count is above zero, it decrements the count and proceeds. If zero, the task waits. When done, the task gives the semaphore back, incrementing the count and possibly waking a waiting task. This ensures no more tasks use resources than available. The execution table shows the semaphore count and task states step-by-step as tasks take and release resources. Key moments clarify why tasks wait and how counts change. The visual quiz tests understanding of these steps. This helps beginners see how counting semaphores control resource pools in FreeRTOS.

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