A counting semaphore is a synchronization tool that allows multiple tasks to access a limited number of resources. It keeps a count of available resources and blocks tasks when none are free.
Click to reveal answer
beginner
How does a counting semaphore help manage resource pools?
It tracks how many resources are free in the pool. When a task takes a resource, the semaphore count decreases. When a task releases a resource, the count increases, allowing other tasks to use it.
Click to reveal answer
beginner
Which FreeRTOS function is used to create a counting semaphore?
The function xSemaphoreCreateCounting() is used to create a counting semaphore by specifying the maximum count and initial count.
Click to reveal answer
intermediate
What happens if a task tries to take a counting semaphore when the count is zero?
The task will block (wait) until the semaphore count becomes greater than zero, meaning a resource has been released and is available.
Click to reveal answer
intermediate
Explain the difference between a binary semaphore and a counting semaphore.
A binary semaphore has only two states (0 or 1) and is used for simple signaling. A counting semaphore can have a count greater than one, managing multiple identical resources.
Click to reveal answer
What does the count in a counting semaphore represent?
ANumber of tasks waiting
BNumber of available resources
CPriority of the task
DMemory size
✗ Incorrect
The count tracks how many resources are currently free and available for tasks to use.
Which function creates a counting semaphore in FreeRTOS?
AxSemaphoreCreateBinary()
BxSemaphoreGive()
CxSemaphoreTake()
DxSemaphoreCreateCounting()
✗ Incorrect
xSemaphoreCreateCounting() creates a counting semaphore with specified max and initial counts.
What happens when a task calls xSemaphoreTake() on a counting semaphore with count zero?
ATask proceeds immediately
BSemaphore count increases
CTask blocks until count > 0
DSystem resets
✗ Incorrect
The task waits (blocks) until a resource is released and the count becomes positive.
When a task releases a resource, which function should it call on the counting semaphore?
AxSemaphoreGive()
BxSemaphoreCreateCounting()
CxSemaphoreTake()
DvTaskDelay()
✗ Incorrect
xSemaphoreGive() increases the semaphore count, signaling a resource is free.
Counting semaphores are best used when:
AMultiple identical resources are managed
BOnly one resource is available
CTasks never block
DNo synchronization is needed
✗ Incorrect
Counting semaphores track multiple identical resources, allowing several tasks to access them.
Describe how a counting semaphore manages access to a pool of resources in FreeRTOS.
Think about how the semaphore count changes when tasks take or give resources.
You got /3 concepts.
Explain the difference between binary and counting semaphores and when to use each.
Consider the number of resources each semaphore type can represent.
You got /3 concepts.
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
Step 1: Understand counting semaphore role
A counting semaphore keeps track of how many identical resources are free or in use.
Step 2: Compare with other options
Creating tasks, storing data, or handling interrupts are not roles of counting semaphores.
Final Answer:
To track the number of available identical resources -> Option A
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
Step 1: Recall function parameters
The function xSemaphoreCreateCounting takes max count first, then initial count.
Step 2: Match parameters to options
xSemaphoreCreateCounting(5, 3); uses (5, 3) which means max count 5 and initial count 3, correct order.
Final Answer:
xSemaphoreCreateCounting(5, 3); -> Option B
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
Step 1: Understand initial semaphore count
The semaphore starts with count 3 (max and initial both 3).
Step 2: Subtract count for each take
Each xSemaphoreTake reduces count by 1 if successful. Two takes reduce count by 2.
Final Answer:
1 -> Option D
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
Step 1: Check initial semaphore count
The semaphore is created with max count 2 and initial count 2, so it's full.
Step 2: Analyze xSemaphoreGive call
Giving semaphore when count is already max causes overflow error or undefined behavior.
Final Answer:
Calling xSemaphoreGive when semaphore count is at max causes overflow -> Option C
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
Step 1: Identify max and initial counts
Max count is total devices (4), initial count is free devices (2).
Step 2: Match parameters to function call
Correct call is xSemaphoreCreateCounting(4, 2) with max 4 and initial 2.
Final Answer:
SemaphoreHandle_t sem = xSemaphoreCreateCounting(4, 2); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.