Bird
Raised Fist0
FreeRTOSprogramming~30 mins

Counting semaphore for resource pools in FreeRTOS - Mini Project: Build & Apply

Choose your learning style10 modes available
Counting Semaphore for Resource Pools in FreeRTOS
📖 Scenario: You are managing a small pool of identical resources in an embedded system using FreeRTOS. To control access to these resources, you will use a counting semaphore. This helps multiple tasks share the limited resources safely without conflicts.
🎯 Goal: Build a simple FreeRTOS program that creates a counting semaphore representing a pool of 3 resources. Then, simulate tasks taking and releasing resources by using the semaphore.
📋 What You'll Learn
Create a counting semaphore with an initial count of 3
Create a variable to hold the maximum number of resources
Write code to take the semaphore once
Write code to give the semaphore back
Print the semaphore count after taking and giving
💡 Why This Matters
🌍 Real World
Counting semaphores are used in embedded systems to control access to limited hardware resources like sensors, communication ports, or memory buffers.
💼 Career
Understanding semaphores is essential for embedded software engineers working with real-time operating systems to ensure safe and efficient resource sharing.
Progress0 / 4 steps
1
Create a counting semaphore for 3 resources
Create a counting semaphore called resourceSemaphore with a maximum count of 3 and an initial count of 3 using xSemaphoreCreateCounting(3, 3).
FreeRTOS
Hint

Use xSemaphoreCreateCounting with max count 3 and initial count 3.

2
Create a variable for maximum resources
Add an integer variable called maxResources and set it to 3 to represent the total number of resources in the pool.
FreeRTOS
Hint

Just create an integer variable maxResources and set it to 3.

3
Take the semaphore once to use a resource
Use xSemaphoreTake(resourceSemaphore, portMAX_DELAY) to take one resource from the pool. Store the result in a variable called taken.
FreeRTOS
Hint

Use xSemaphoreTake with portMAX_DELAY to wait indefinitely.

4
Give back the semaphore and print results
Use xSemaphoreGive(resourceSemaphore) to release the resource. Then print "Resource taken: " followed by taken, and print "Resource released" after giving back.
FreeRTOS
Hint

Use xSemaphoreGive to release and printf to show messages.

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