Challenge - 5 Problems
Semaphore Timeout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateSemaphore 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; }
Attempts:
2 left
💡 Hint
Think about what happens if the semaphore is never given before the timeout expires.
✗ Incorrect
The semaphore is created but never given. The task waits for 100 ms and then times out, so it prints "Timeout occurred".
🧠 Conceptual
intermediateUnderstanding semaphore timeout return values
Which return value indicates that a semaphore was successfully taken before the timeout expired?
Attempts:
2 left
💡 Hint
Check the FreeRTOS API documentation for xSemaphoreTake return values.
✗ Incorrect
xSemaphoreTake returns pdTRUE if the semaphore was successfully taken, and pdFALSE if the timeout expired without taking it.
🔧 Debug
advancedIdentify 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;
}Attempts:
2 left
💡 Hint
Look at the delay before the semaphore is given and the timeout in the taking task.
✗ Incorrect
The taking task waits 100 ms but the giving task gives the semaphore after 200 ms, so the taking task times out first.
📝 Syntax
advancedIdentify 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");
}
}Attempts:
2 left
💡 Hint
Check the syntax of function arguments and commas.
✗ Incorrect
Option D is missing a comma between arguments, causing a syntax error.
🚀 Application
expertCalculate 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?
Attempts:
2 left
💡 Hint
Add all the timeout durations because the task waits fully each time.
✗ Incorrect
The task waits 50 + 100 + 150 ms = 300 ms total before giving up.
