Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Semaphore for ISR-to-task signaling in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Semaphore Signaling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this ISR-to-task semaphore signaling code?

Consider the following FreeRTOS code snippet where an ISR signals a task using a binary semaphore. What will be the output printed by the task?

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include <stdio.h>

SemaphoreHandle_t xBinarySemaphore;

void vISR_Handler(void) {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreGiveFromISR(xBinarySemaphore, &xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

void vTaskFunction(void *pvParameters) {
    if(xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) {
        printf("Semaphore taken, task running\n");
    } else {
        printf("Failed to take semaphore\n");
    }
    vTaskDelete(NULL);
}

int main(void) {
    xBinarySemaphore = xSemaphoreCreateBinary();
    xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, NULL);
    vISR_Handler();
    vTaskStartScheduler();
    return 0;
}
ASemaphore taken, task running
BFailed to take semaphore
CNo output, task blocked forever
DCompilation error due to missing includes
Attempts:
2 left
💡 Hint

Think about what happens when the ISR gives the semaphore before the task tries to take it.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the use of semaphores for ISR-to-task signaling?

In FreeRTOS, why are semaphores commonly used for signaling from an ISR to a task?

ASemaphores allow the ISR to directly execute task code synchronously.
BSemaphores are used to disable interrupts during task execution.
CSemaphores prevent tasks from running until the ISR finishes execution.
DSemaphores provide a safe way for ISRs to unblock tasks without blocking themselves.
Attempts:
2 left
💡 Hint

Consider what ISRs must avoid doing and how tasks can be notified safely.

🔧 Debug
advanced
2:00remaining
What error occurs in this ISR-to-task semaphore code?

Identify the error in the following code snippet and what happens when the ISR tries to give the semaphore.

FreeRTOS
SemaphoreHandle_t xSem;

void ISR_Handler(void) {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreGive(xSem); // Incorrect function used
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

void Task(void *pvParameters) {
    if(xSemaphoreTake(xSem, portMAX_DELAY) == pdTRUE) {
        // Task code
    }
}
ARuntime error: Semaphore not given from ISR safely
BCompilation error: xSemaphoreGiveFromISR missing
CNo error, code runs correctly
DDeadlock because task never unblocks
Attempts:
2 left
💡 Hint

Check the function used inside the ISR to give the semaphore.

📝 Syntax
advanced
1:30remaining
Which option correctly signals a binary semaphore from an ISR?

Choose the correct syntax to give a binary semaphore from an ISR in FreeRTOS.

AxSemaphoreGive(xSemaphore);
BxSemaphoreGiveFromISR(xSemaphore, NULL);
CxSemaphoreTakeFromISR(xSemaphore, NULL);
DxSemaphoreGiveFromISR(xSemaphore);
Attempts:
2 left
💡 Hint

Remember the function signature requires a pointer to a variable for context switching.

🚀 Application
expert
2:30remaining
How many times will the task unblock given this ISR signaling pattern?

A binary semaphore is created and initially empty. The ISR calls xSemaphoreGiveFromISR three times in quick succession before the task attempts to take the semaphore once. How many times will the task unblock if it calls xSemaphoreTake once with portMAX_DELAY?

AThe task will unblock three times, once per ISR call.
BThe task will unblock twice, because the first give is ignored.
CThe task will unblock only once, because the semaphore is binary and cannot count beyond one.
DThe task will never unblock because the semaphore was given before task creation.
Attempts:
2 left
💡 Hint

Think about the difference between binary and counting semaphores.

Practice

(1/5)
1. What is the main purpose of using a semaphore in FreeRTOS for ISR-to-task signaling?
easy
A. To disable interrupts during task execution
B. To create a new task inside an ISR
C. To safely notify a task from an interrupt service routine (ISR)
D. To allocate memory dynamically in an ISR

Solution

  1. Step 1: Understand semaphore role in FreeRTOS

    Semaphores are synchronization tools used to signal between tasks or between ISRs and tasks.
  2. Step 2: Identify ISR-to-task signaling purpose

    In FreeRTOS, semaphores let an ISR notify a task safely without blocking or complex synchronization.
  3. Final Answer:

    To safely notify a task from an interrupt service routine (ISR) -> Option C
  4. Quick Check:

    Semaphore signals task from ISR = A [OK]
Hint: Semaphores signal tasks from ISRs safely [OK]
Common Mistakes:
  • Thinking semaphores create tasks
  • Confusing semaphores with disabling interrupts
  • Using semaphores for memory allocation
2. Which FreeRTOS function is used inside an ISR to give a semaphore to a task?
easy
A. xSemaphoreGiveFromISR()
B. xSemaphoreTakeFromISR()
C. xSemaphoreCreateFromISR()
D. xSemaphoreGive()

Solution

  1. Step 1: Recall ISR-safe semaphore functions

    FreeRTOS provides special functions for ISR context to avoid blocking and ensure safety.
  2. Step 2: Identify the correct ISR semaphore give function

    The function to give a semaphore from an ISR is xSemaphoreGiveFromISR().
  3. Final Answer:

    xSemaphoreGiveFromISR() -> Option A
  4. Quick Check:

    Give semaphore from ISR = xSemaphoreGiveFromISR() [OK]
Hint: ISR uses xSemaphoreGiveFromISR() to signal tasks [OK]
Common Mistakes:
  • Using xSemaphoreGive() inside ISR (not safe)
  • Confusing take and give functions
  • Assuming xSemaphoreCreateFromISR() exists
3. Given the code snippet below, what will be the output printed by the task?
SemaphoreHandle_t xSemaphore;

void ISR_Handler() {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);
}

void TaskFunction(void *pvParameters) {
    if (xSemaphoreTake(xSemaphore, portMAX_DELAY) == pdTRUE) {
        printf("Semaphore received\n");
    }
}
medium
A. Semaphore received twice
B. No output
C. Runtime error
D. Semaphore received

Solution

  1. Step 1: Analyze ISR giving semaphore

    The ISR calls xSemaphoreGiveFromISR() which releases the semaphore.
  2. Step 2: Analyze task waiting on semaphore

    The task waits indefinitely with xSemaphoreTake() and prints "Semaphore received" when it gets the semaphore.
  3. Final Answer:

    Semaphore received -> Option D
  4. Quick Check:

    Task prints after semaphore take = C [OK]
Hint: Task prints after successful semaphore take [OK]
Common Mistakes:
  • Thinking semaphore is given twice
  • Assuming no output if ISR runs
  • Expecting runtime error without initialization
4. Identify the error in the following ISR code snippet for giving a semaphore:
void ISR_Handler() {
    xSemaphoreGive(xSemaphore);
}
medium
A. xSemaphoreGive() cannot be called from an ISR
B. Semaphore must be taken before giving
C. ISR must not use semaphores
D. xSemaphoreGive() requires a pointer argument

Solution

  1. Step 1: Check function suitability for ISR

    xSemaphoreGive() is not safe to call inside an ISR because it may block or cause race conditions.
  2. Step 2: Identify correct ISR function

    Use xSemaphoreGiveFromISR() inside ISRs to safely give semaphores.
  3. Final Answer:

    xSemaphoreGive() cannot be called from an ISR -> Option A
  4. Quick Check:

    Use xSemaphoreGiveFromISR() in ISR, not xSemaphoreGive() [OK]
Hint: Use xSemaphoreGiveFromISR() inside ISR, not xSemaphoreGive() [OK]
Common Mistakes:
  • Calling xSemaphoreGive() inside ISR
  • Ignoring ISR context restrictions
  • Assuming semaphore must be taken before giving
5. You want a task to respond immediately when a hardware interrupt occurs. Which sequence correctly uses a semaphore for ISR-to-task signaling in FreeRTOS?
hard
A. Create counting semaphore; task calls xSemaphoreGive(); ISR calls xSemaphoreTakeFromISR()
B. Create binary semaphore; ISR calls xSemaphoreGiveFromISR(); task calls xSemaphoreTake() with portMAX_DELAY
C. Create mutex; ISR calls xSemaphoreGive(); task calls xSemaphoreTake() with zero timeout
D. Create binary semaphore; task calls xSemaphoreGiveFromISR(); ISR calls xSemaphoreTake()

Solution

  1. Step 1: Identify correct semaphore type and usage

    Binary semaphore is suitable for signaling events from ISR to task.
  2. Step 2: Confirm correct function calls and order

    ISR must call xSemaphoreGiveFromISR() to signal; task waits with xSemaphoreTake() blocking indefinitely (portMAX_DELAY) until signaled.
  3. Final Answer:

    Create binary semaphore; ISR calls xSemaphoreGiveFromISR(); task calls xSemaphoreTake() with portMAX_DELAY -> Option B
  4. Quick Check:

    Binary semaphore + ISR give + task take = A [OK]
Hint: ISR gives semaphore, task takes with infinite wait [OK]
Common Mistakes:
  • Using mutex instead of semaphore
  • Calling give in task and take in ISR
  • Using zero timeout causing missed signals
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