Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Semaphore timeout handling in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Semaphore Timeout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Semaphore take with timeout behavior
What is the output of this FreeRTOS code snippet when the semaphore is not given before the timeout?
FreeRTOS
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"

SemaphoreHandle_t sem;

void task(void *params) {
    if (xSemaphoreTake(sem, pdMS_TO_TICKS(100)) == pdTRUE) {
        printf("Semaphore taken\n");
    } else {
        printf("Timeout occurred\n");
    }
    vTaskDelete(NULL);
}

int main() {
    sem = xSemaphoreCreateBinary();
    xTaskCreate(task, "Task", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ATimeout occurred
BSemaphore taken
CCompilation error due to missing semaphore initialization
DDeadlock, no output
Attempts:
2 left
💡 Hint
Think about what happens if the semaphore is never given before the timeout expires.
🧠 Conceptual
intermediate
1:30remaining
Understanding semaphore timeout return values
Which return value indicates that a semaphore was successfully taken before the timeout expired?
ApdFALSE
BpdFAIL
CpdTRUE
DpdTIMEOUT
Attempts:
2 left
💡 Hint
Check the FreeRTOS API documentation for xSemaphoreTake return values.
🔧 Debug
advanced
2:30remaining
Identify the cause of unexpected semaphore timeout
Why does this task always print "Timeout occurred" even though another task gives the semaphore?
FreeRTOS
SemaphoreHandle_t sem;

void givingTask(void *params) {
    vTaskDelay(pdMS_TO_TICKS(200));
    xSemaphoreGive(sem);
    vTaskDelete(NULL);
}

void takingTask(void *params) {
    if (xSemaphoreTake(sem, pdMS_TO_TICKS(100)) == pdTRUE) {
        printf("Semaphore taken\n");
    } else {
        printf("Timeout occurred\n");
    }
    vTaskDelete(NULL);
}

int main() {
    sem = xSemaphoreCreateBinary();
    xTaskCreate(takingTask, "Take", 1000, NULL, 1, NULL);
    xTaskCreate(givingTask, "Give", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ASemaphore is given after the taking task's timeout expires
BSemaphore is never given because givingTask is not started
CxSemaphoreTake is called with zero timeout
DSemaphore handle is NULL due to failed creation
Attempts:
2 left
💡 Hint
Look at the delay before the semaphore is given and the timeout in the taking task.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in semaphore timeout usage
Which option contains a syntax error in the semaphore take call with timeout?
FreeRTOS
SemaphoreHandle_t sem = xSemaphoreCreateBinary();

void task(void *params) {
    if (xSemaphoreTake(sem, 100) == pdTRUE) {
        printf("Taken\n");
    } else {
        printf("Timeout\n");
    }
}
AxSemaphoreTake(sem, (TickType_t)100)
BxSemaphoreTake(sem, 100)
CxSemaphoreTake(sem, pdMS_TO_TICKS(100))
DxSemaphoreTake(sem 100)
Attempts:
2 left
💡 Hint
Check the syntax of function arguments and commas.
🚀 Application
expert
3:00remaining
Calculate total wait time with multiple semaphore takes and timeouts
A task tries to take a semaphore three times with timeouts of 50 ms, 100 ms, and 150 ms respectively. It fails each time. What is the total time the task waited before giving up?
A50 ms
B300 ms
C250 ms
D150 ms
Attempts:
2 left
💡 Hint
Add all the timeout durations because the task waits fully each time.

Practice

(1/5)
1.

What does the timeout parameter in xSemaphoreTake() control?

easy
A. How long the task waits for the semaphore before giving up
B. The priority of the task waiting for the semaphore
C. The number of times the semaphore can be taken
D. The size of the semaphore queue

Solution

  1. Step 1: Understand the role of timeout in xSemaphoreTake()

    The timeout parameter specifies the maximum time the task will wait to obtain the semaphore.
  2. Step 2: Relate timeout to task blocking

    If the semaphore is not available within this time, the function returns failure, preventing indefinite blocking.
  3. Final Answer:

    How long the task waits for the semaphore before giving up -> Option A
  4. Quick Check:

    Timeout controls wait duration = B [OK]
Hint: Timeout limits wait time to avoid blocking forever [OK]
Common Mistakes:
  • Confusing timeout with task priority
  • Thinking timeout changes semaphore count
  • Assuming timeout affects queue size
2.

Which of the following is the correct way to call xSemaphoreTake() with a 100 tick timeout?

SemaphoreHandle_t sem;
easy
A. xSemaphoreTake(&sem, 100);
B. xSemaphoreTake(100, sem);
C. xSemaphoreTake(sem, &100);
D. xSemaphoreTake(sem, 100);

Solution

  1. Step 1: Check function signature

    xSemaphoreTake takes the semaphore handle first, then the timeout as TickType_t.
  2. Step 2: Match parameters correctly

    Passing sem as first argument and 100 as timeout matches the correct syntax.
  3. Final Answer:

    xSemaphoreTake(sem, 100); -> Option D
  4. Quick Check:

    Semaphore handle first, timeout second = A [OK]
Hint: Semaphore handle is first argument in xSemaphoreTake [OK]
Common Mistakes:
  • Swapping arguments order
  • Passing address of timeout
  • Passing address of semaphore handle
3.

What will be the output of this code snippet?

SemaphoreHandle_t sem = xSemaphoreCreateBinary();
if(xSemaphoreTake(sem, 10) == pdTRUE) {
    printf("Got semaphore\n");
} else {
    printf("Timeout occurred\n");
}
medium
A. Got semaphore
B. No output
C. Timeout occurred
D. Compilation error

Solution

  1. Step 1: Understand semaphore initial state

    xSemaphoreCreateBinary creates a semaphore initially empty (not given).
  2. Step 2: Check xSemaphoreTake behavior

    Since semaphore is empty and no give is called, xSemaphoreTake waits 10 ticks then times out.
  3. Final Answer:

    Timeout occurred -> Option C
  4. Quick Check:

    Empty semaphore + timeout = timeout message [OK]
Hint: Binary semaphore starts empty, so take times out if not given [OK]
Common Mistakes:
  • Assuming semaphore is initially given
  • Ignoring timeout effect
  • Expecting compilation error
4.

Identify the error in this code snippet that uses xSemaphoreTake() with timeout:

SemaphoreHandle_t sem = xSemaphoreCreateBinary();
if(xSemaphoreTake(sem, portMAX_DELAY)) {
    // Critical section
} else {
    // Handle timeout
}
medium
A. Using portMAX_DELAY disables timeout, so else block never runs
B. Semaphore handle is not initialized
C. Missing sem give before take
D. Incorrect if condition syntax

Solution

  1. Step 1: Understand portMAX_DELAY meaning

    portMAX_DELAY means wait indefinitely for the semaphore.
  2. Step 2: Analyze if-else behavior

    Since wait is infinite, timeout never occurs, so else block is unreachable.
  3. Final Answer:

    Using portMAX_DELAY disables timeout, so else block never runs -> Option A
  4. Quick Check:

    portMAX_DELAY means infinite wait = C [OK]
Hint: portMAX_DELAY means wait forever, no timeout occurs [OK]
Common Mistakes:
  • Thinking else block handles timeout with portMAX_DELAY
  • Assuming semaphore uninitialized causes error here
  • Confusing syntax error with logic error
5.

You want to wait for a semaphore but only up to 50 ticks. If the semaphore is not available, you want to perform a fallback task immediately. Which code snippet correctly implements this behavior?

hard
A.
if(xSemaphoreTake(sem, portMAX_DELAY)) {
    // Use resource
} else {
    // Fallback task
}
B.
if(xSemaphoreTake(sem, 50) == pdTRUE) {
    // Use resource
} else {
    // Fallback task
}
C.
if(xSemaphoreTake(sem, 0) == pdFALSE) {
    // Use resource
} else {
    // Fallback task
}
D.
if(xSemaphoreTake(sem, 50)) {
    // Fallback task
} else {
    // Use resource
}

Solution

  1. Step 1: Check timeout usage

    Using 50 ticks as timeout means wait up to 50 ticks for semaphore.
  2. Step 2: Verify fallback logic

    If take returns pdTRUE, resource is used; else fallback task runs immediately after timeout.
  3. Final Answer:

    Correctly waits 50 ticks and runs fallback if timeout -> Option B
  4. Quick Check:

    Timeout 50 ticks + fallback on failure = A [OK]
Hint: Wait with timeout, fallback in else block after failure [OK]
Common Mistakes:
  • Using portMAX_DELAY disables fallback
  • Swapping if-else blocks
  • Using zero timeout incorrectly
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