Bird
0
0

Given the code snippet:

medium📝 Predict Output Q4 of 15
FreeRTOS - Debugging and Monitoring
Given the code snippet:
void vTask(void *pvParameters) {
  for(;;) {
    if(xSemaphoreTake(xSem, 0) == pdTRUE) {
      // Critical section
      xSemaphoreGive(xSem);
    }
  }
}

What is the likely problem with this code?
AIt causes a deadlock because the semaphore is never given
BIt causes a busy wait consuming CPU cycles
CIt will block indefinitely on xSemaphoreTake
DIt will cause a stack overflow
Step-by-Step Solution
Solution:
  1. Step 1: Analyze semaphore take with zero block time

    The call xSemaphoreTake(xSem, 0) tries to take the semaphore without waiting.
  2. Step 2: Understand effect of zero block time in loop

    If the semaphore is not available, the task loops immediately, causing busy waiting and high CPU usage.
  3. Final Answer:

    It causes a busy wait consuming CPU cycles -> Option B
  4. Quick Check:

    Busy wait due to zero block time = C [OK]
Quick Trick: Zero block time in loop causes busy wait [OK]
Common Mistakes:
  • Thinking it causes deadlock because semaphore is given
  • Assuming it blocks indefinitely with zero wait
  • Confusing busy wait with stack overflow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes