Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Why semaphores are needed in FreeRTOS - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Semaphore Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Semaphores in FreeRTOS
Why do we use semaphores in FreeRTOS when multiple tasks share resources?
ATo allow tasks to ignore resource sharing and run independently without any synchronization
BTo speed up the execution of all tasks by running them simultaneously without control
CTo permanently block all tasks except one, stopping multitasking
DTo prevent tasks from accessing shared resources at the same time, avoiding conflicts
Attempts:
2 left
💡 Hint
Think about what happens if two tasks try to use the same resource at once.
❓ Predict Output
intermediate
2:00remaining
Output of Semaphore Usage in Task Synchronization
What will be the output of this FreeRTOS code snippet where two tasks use a binary semaphore to access a shared counter?
FreeRTOS
SemaphoreHandle_t sem;
int counter = 0;

void Task1(void *pvParameters) {
  xSemaphoreTake(sem, portMAX_DELAY);
  counter += 1;
  printf("Task1 counter: %d\n", counter);
  xSemaphoreGive(sem);
  vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
  xSemaphoreTake(sem, portMAX_DELAY);
  counter += 2;
  printf("Task2 counter: %d\n", counter);
  xSemaphoreGive(sem);
  vTaskDelete(NULL);
}

int main() {
  sem = xSemaphoreCreateBinary();
  xSemaphoreGive(sem);
  xTaskCreate(Task1, "T1", 1000, NULL, 1, NULL);
  xTaskCreate(Task2, "T2", 1000, NULL, 1, NULL);
  vTaskStartScheduler();
  return 0;
}
ATask1 counter: 1\nTask2 counter: 3
BTask1 counter: 1\nTask2 counter: 2
CTask1 counter: 2\nTask2 counter: 3
DTask1 counter: 0\nTask2 counter: 2
Attempts:
2 left
💡 Hint
The semaphore ensures one task finishes updating before the other starts.
🔧 Debug
advanced
2:00remaining
Identify the Problem Without Semaphore
What problem will occur if the semaphore is removed from the previous code and both tasks access the counter directly?
FreeRTOS
int counter = 0;

void Task1(void *pvParameters) {
  counter += 1;
  printf("Task1 counter: %d\n", counter);
  vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
  counter += 2;
  printf("Task2 counter: %d\n", counter);
  vTaskDelete(NULL);
}

int main() {
  xTaskCreate(Task1, "T1", 1000, NULL, 1, NULL);
  xTaskCreate(Task2, "T2", 1000, NULL, 1, NULL);
  vTaskStartScheduler();
  return 0;
}
ACounter always stays zero
BCompilation error due to missing semaphore variable
CRace condition causing unpredictable counter values
DDeadlock because tasks wait forever
Attempts:
2 left
💡 Hint
Think about what happens when two tasks change the same variable at the same time.
📝 Syntax
advanced
2:00remaining
Correct Semaphore Initialization Syntax
Which option shows the correct way to create and initialize a binary semaphore in FreeRTOS?
ASemaphoreHandle_t sem = xSemaphoreCreateCounting(1, 1);
BSemaphoreHandle_t sem = xSemaphoreCreateBinary(); xSemaphoreGive(sem);
CSemaphoreHandle_t sem = xSemaphoreCreateMutex(); xSemaphoreTake(sem, 0);
DSemaphoreHandle_t sem = xSemaphoreCreateBinary(1);
Attempts:
2 left
💡 Hint
Binary semaphores need to be given once after creation to be available.
🚀 Application
expert
2:00remaining
Choosing Semaphore Type for Task Synchronization
You have two tasks: one produces data and the other consumes it. You want the consumer to wait until data is ready. Which semaphore type is best to use in FreeRTOS?
ABinary semaphore to signal data availability
BCounting semaphore to count multiple data items
CMutex to protect data access
DNo semaphore, just a shared variable
Attempts:
2 left
💡 Hint
Think about signaling a single event from producer to consumer.

Practice

(1/5)
1. Why are semaphores needed in FreeRTOS when multiple tasks share a resource?
easy
A. To prevent tasks from accessing the resource at the same time and causing errors
B. To speed up the execution of all tasks simultaneously
C. To allow tasks to ignore each other and run independently
D. To increase the memory usage of the system

Solution

  1. Step 1: Understand resource sharing in multitasking

    When multiple tasks share a resource, simultaneous access can cause data corruption or crashes.
  2. Step 2: Role of semaphores in synchronization

    Semaphores act as signals to control access, allowing only one task at a time to use the resource safely.
  3. Final Answer:

    To prevent tasks from accessing the resource at the same time and causing errors -> Option A
  4. Quick Check:

    Semaphores prevent simultaneous access = A [OK]
Hint: Semaphores stop tasks from clashing over shared resources [OK]
Common Mistakes:
  • Thinking semaphores speed up all tasks
  • Believing tasks run independently without coordination
  • Assuming semaphores increase memory usage
2. Which of the following is the correct way to create a binary semaphore in FreeRTOS?
easy
A. xSemaphoreCreateBinary();
B. SemaphoreCreateBinary();
C. createBinarySemaphore();
D. newSemaphoreBinary();

Solution

  1. Step 1: Recall FreeRTOS semaphore creation functions

    FreeRTOS provides the function xSemaphoreCreateBinary() to create a binary semaphore.
  2. Step 2: Verify syntax correctness

    The function name must start with 'x' and match the exact FreeRTOS API naming conventions.
  3. Final Answer:

    xSemaphoreCreateBinary(); -> Option A
  4. Quick Check:

    Correct FreeRTOS binary semaphore function = D [OK]
Hint: FreeRTOS functions start with 'x' for handles [OK]
Common Mistakes:
  • Using incorrect function names without 'x' prefix
  • Confusing with other RTOS APIs
  • Adding extra words to function names
3. Consider this FreeRTOS code snippet:
SemaphoreHandle_t sem = xSemaphoreCreateBinary();
xSemaphoreGive(sem);
if(xSemaphoreTake(sem, 0) == pdTRUE) {
    printf("Access granted\n");
} else {
    printf("Access denied\n");
}
What will be the output?
medium
A. Access denied
B. Access granted
C. Compilation error
D. No output

Solution

  1. Step 1: Understand semaphore creation and initial state

    The binary semaphore is created and then given once, so it is available (count = 1).
  2. Step 2: Check the take operation with zero wait

    The xSemaphoreTake tries to take the semaphore immediately (timeout 0). Since it was given before, it succeeds.
  3. Final Answer:

    Access granted -> Option B
  4. Quick Check:

    Semaphore given then taken immediately = Access granted [OK]
Hint: Semaphore given before take means immediate success [OK]
Common Mistakes:
  • Assuming zero timeout means failure
  • Forgetting to give semaphore before take
  • Confusing binary semaphore initial state
4. This FreeRTOS code snippet tries to protect a shared resource:
SemaphoreHandle_t sem = xSemaphoreCreateBinary();
// Task A
xSemaphoreTake(sem, portMAX_DELAY);
// Access resource
xSemaphoreGive(sem);
// Task B
xSemaphoreTake(sem, portMAX_DELAY);
// Access resource
xSemaphoreGive(sem);
What is the main problem here?
medium
A. The semaphore is created as binary but used as counting
B. The semaphore is given twice causing overflow
C. The semaphore is never given initially, so tasks block forever
D. The tasks do not take the semaphore before accessing resource

Solution

  1. Step 1: Check semaphore initial state after creation

    Binary semaphore created with xSemaphoreCreateBinary() starts empty (not given).
  2. Step 2: Analyze task behavior on take

    Task A tries to take semaphore but it was never given, so it blocks forever. Same for Task B.
  3. Final Answer:

    The semaphore is never given initially, so tasks block forever -> Option C
  4. Quick Check:

    Binary semaphore must be given once before take = C [OK]
Hint: Binary semaphore must be given once before tasks take it [OK]
Common Mistakes:
  • Assuming semaphore is given automatically on creation
  • Thinking multiple gives cause overflow
  • Ignoring semaphore take before resource access
5. You have two tasks sharing a sensor. Task 1 reads data and Task 2 processes it. Without semaphores, data corruption occurs. How should you use semaphores to fix this?
hard
A. Use a counting semaphore with count 0 to block both tasks
B. Use a mutex but never give it back after taking
C. Use no semaphore and rely on task priorities to avoid conflicts
D. Use a binary semaphore to let only one task access the sensor at a time

Solution

  1. Step 1: Identify the problem of simultaneous access

    Both tasks accessing the sensor at the same time cause data corruption.
  2. Step 2: Choose semaphore type and usage

    A binary semaphore ensures only one task accesses the sensor at a time, preventing conflicts.
  3. Final Answer:

    Use a binary semaphore to let only one task access the sensor at a time -> Option D
  4. Quick Check:

    Binary semaphore controls exclusive access = B [OK]
Hint: Binary semaphore allows one task access at a time [OK]
Common Mistakes:
  • Using counting semaphore incorrectly with zero count
  • Ignoring semaphore and trusting priorities alone
  • Not releasing mutex causing deadlock
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