Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Counting semaphore for resource pools in FreeRTOS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a counting semaphore with 3 resources available.

FreeRTOS
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting([1], [1]);
Drag options to blanks, or click blank then click option'
A5
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 0 as the count will not allow multiple resources.
Setting different values for max and initial count can cause unexpected behavior.
2fill in blank
medium

Complete the code to take (acquire) the semaphore, waiting indefinitely if not available.

FreeRTOS
if(xSemaphoreTake(xSemaphore, [1]) == pdTRUE) {
    // Access resource
}
Drag options to blanks, or click blank then click option'
A100
BportMAX_DELAY
C0
DpdFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 causes the function to return immediately if the semaphore is not available.
Using pdFALSE is invalid as a block time.
3fill in blank
hard

Fix the error in releasing the semaphore after using the resource.

FreeRTOS
xSemaphore[1](xSemaphore);
Drag options to blanks, or click blank then click option'
AGive
BTake
CCreateCounting
DDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using xSemaphoreTake again will try to acquire instead of release.
Using xSemaphoreDelete removes the semaphore entirely.
4fill in blank
hard

Fill both blanks to create a counting semaphore with a maximum of 4 resources and initial count of 2.

FreeRTOS
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting([1], [2]);
Drag options to blanks, or click blank then click option'
A4
B2
C3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting initial count higher than maximum causes errors.
Confusing the order of parameters.
5fill in blank
hard

Fill all three blanks to check if the semaphore can be taken without waiting, and handle success or failure.

FreeRTOS
if(xSemaphoreTake(xSemaphore, [1]) == [2]) {
    // Resource acquired
} else {
    // Resource not available
    [3]
}
Drag options to blanks, or click blank then click option'
A0
BpdTRUE
CvTaskDelay(10);
DpdFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-zero block time when no wait is intended.
Checking against pdFALSE instead of pdTRUE.
Not handling the else case properly.

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