Bird
Raised Fist0
FreeRTOSprogramming~20 mins

xTaskNotifyGive() as lightweight semaphore in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Notification 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 FreeRTOS task notification example?

Consider two tasks: TaskA and TaskB. TaskA calls xTaskNotifyGive(TaskBHandle); and TaskB waits using ulTaskNotifyTake(pdTRUE, portMAX_DELAY);. What will TaskB receive?

FreeRTOS
void TaskA(void *pvParameters) {
    // Notify TaskB
    xTaskNotifyGive(TaskBHandle);
    vTaskDelay(pdMS_TO_TICKS(1000));
}

void TaskB(void *pvParameters) {
    // Wait for notification
    uint32_t count = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
    printf("Notification count: %lu\n", count);
    vTaskDelete(NULL);
}
ANotification count: 2
BNotification count: 0
CNotification count: 1
DThe code causes a runtime error
Attempts:
2 left
💡 Hint

Think about what xTaskNotifyGive() does to the notification value and how ulTaskNotifyTake() reads it.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes xTaskNotifyGive() as a lightweight semaphore?

In FreeRTOS, how does xTaskNotifyGive() function as a lightweight semaphore?

AIt increments a task's notification value, signaling availability of a resource.
BIt creates a new semaphore object in the kernel.
CIt blocks the calling task until a resource is free.
DIt resets the task notification value to zero.
Attempts:
2 left
💡 Hint

Think about what happens to the task notification value when xTaskNotifyGive() is called.

🔧 Debug
advanced
2:30remaining
Why does this code cause TaskB to block forever?

TaskA calls xTaskNotifyGive(TaskBHandle); twice quickly. TaskB calls ulTaskNotifyTake(pdTRUE, 1000); once. TaskB never proceeds. Why?

FreeRTOS
void TaskA(void *pvParameters) {
    xTaskNotifyGive(TaskBHandle);
    xTaskNotifyGive(TaskBHandle);
    vTaskDelete(NULL);
}

void TaskB(void *pvParameters) {
    uint32_t count = ulTaskNotifyTake(pdTRUE, 1000);
    if(count == 0) {
        printf("Timeout waiting for notification\n");
    } else {
        printf("Received notification count: %lu\n", count);
    }
    vTaskDelete(NULL);
}
ABecause ulTaskNotifyTake resets the count to zero after first call, second notify is lost.
BBecause TaskB never calls ulTaskNotifyTake twice to consume both notifications.
CBecause xTaskNotifyGive does not increment the notification value.
DBecause TaskA deletes itself before TaskB starts.
Attempts:
2 left
💡 Hint

Consider how many notifications TaskB consumes with one call to ulTaskNotifyTake().

📝 Syntax
advanced
2:00remaining
Which option correctly uses xTaskNotifyGive() and ulTaskNotifyTake() as a semaphore?

Choose the code snippet that correctly uses xTaskNotifyGive() and ulTaskNotifyTake() to synchronize two tasks.

A
xTaskNotifyGive(TaskHandle);
uint32_t count = ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
B
xTaskNotifyGive(TaskHandle);
uint32_t count = ulTaskNotifyTake(pdTRUE);
C
xTaskNotifyGive(TaskHandle, 1);
uint32_t count = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
D
xTaskNotifyGive(TaskHandle);
uint32_t count = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Attempts:
2 left
💡 Hint

Check the parameters of ulTaskNotifyTake() and how xTaskNotifyGive() is called.

🚀 Application
expert
2:30remaining
How many notifications can ulTaskNotifyTake() accumulate before blocking?

If TaskA calls xTaskNotifyGive(TaskBHandle); 5 times quickly, and TaskB calls ulTaskNotifyTake(pdTRUE, portMAX_DELAY); once, what is the value returned by TaskB?

A5
B0
C1
DIt causes a runtime error
Attempts:
2 left
💡 Hint

Think about how xTaskNotifyGive() increments the notification value and how ulTaskNotifyTake() returns it.

Practice

(1/5)
1. What is the primary purpose of xTaskNotifyGive() in FreeRTOS?
easy
A. To send a simple signal from one task to another as a lightweight semaphore
B. To create a new task in the system
C. To delete a task permanently
D. To allocate memory dynamically

Solution

  1. Step 1: Understand the function role

    xTaskNotifyGive() is designed to send a notification signal from one task to another.
  2. Step 2: Compare with other options

    Creating or deleting tasks and memory allocation are unrelated to xTaskNotifyGive().
  3. Final Answer:

    To send a simple signal from one task to another as a lightweight semaphore -> Option A
  4. Quick Check:

    Signal sending = To send a simple signal from one task to another as a lightweight semaphore [OK]
Hint: Remember: xTaskNotifyGive() signals tasks like a lightweight semaphore [OK]
Common Mistakes:
  • Confusing it with task creation or deletion functions
  • Thinking it manages memory
  • Assuming it blocks tasks directly
2. Which of the following is the correct syntax to send a notification using xTaskNotifyGive() to a task handle named xTaskHandle?
easy
A. xTaskNotifyGive(xTaskHandle, 1);
B. xTaskNotifyGive(&xTaskHandle);
C. xTaskNotifyGive(xTaskHandle);
D. xTaskNotifyGive();

Solution

  1. Step 1: Recall function signature

    xTaskNotifyGive() takes a task handle as a parameter, not a pointer or extra arguments.
  2. Step 2: Check each option

    xTaskNotifyGive(xTaskHandle); correctly passes the task handle. xTaskNotifyGive(&xTaskHandle); passes address (wrong), C adds an extra argument (wrong), D has no argument (wrong).
  3. Final Answer:

    xTaskNotifyGive(xTaskHandle); -> Option C
  4. Quick Check:

    Correct syntax = xTaskNotifyGive(xTaskHandle); [OK]
Hint: Pass the task handle directly without extra arguments [OK]
Common Mistakes:
  • Passing pointer to task handle instead of handle itself
  • Adding extra parameters
  • Calling without any argument
3. Consider the following code snippet:
void Task1(void *pvParameters) {
    // Wait for notification
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
    // Continue after notification
    printf("Task1 resumed\n");
}

void Task2(void *pvParameters) {
    // Notify Task1
    xTaskNotifyGive(Task1Handle);
}
What will be printed when Task2 calls xTaskNotifyGive()?
medium
A. Task2 resumes instead of Task1
B. Task1 resumed
C. Syntax error at compile time
D. No output, Task1 blocks forever

Solution

  1. Step 1: Understand notification flow

    Task1 waits for a notification with ulTaskNotifyTake(). When Task2 calls xTaskNotifyGive(), it sends the notification.
  2. Step 2: Effect of notification

    The notification unblocks Task1, so it prints "Task1 resumed".
  3. Final Answer:

    Task1 resumed -> Option B
  4. Quick Check:

    Notification received = Task1 prints [OK]
Hint: xTaskNotifyGive() wakes waiting task to continue [OK]
Common Mistakes:
  • Assuming Task1 never resumes
  • Confusing which task prints output
  • Thinking code causes compile error
4. Identify the error in this code snippet using xTaskNotifyGive():
void TaskA(void *pvParameters) {
    ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
    printf("TaskA running\n");
}

void TaskB(void *pvParameters) {
    xTaskNotifyGive(&TaskAHandle);
}
medium
A. Passing address of task handle instead of task handle to xTaskNotifyGive()
B. Using pdFALSE in ulTaskNotifyTake() causes immediate return
C. Missing delay in TaskB before notify
D. printf statement is incorrect syntax

Solution

  1. Step 1: Check parameter passed to xTaskNotifyGive()

    The function expects a task handle, but &TaskAHandle passes its address, which is incorrect.
  2. Step 2: Validate other parts

    Using pdFALSE is valid to clear notification count, no syntax error in printf, and delay is optional.
  3. Final Answer:

    Passing address of task handle instead of task handle to xTaskNotifyGive() -> Option A
  4. Quick Check:

    Correct parameter type needed = Passing address of task handle instead of task handle to xTaskNotifyGive() [OK]
Hint: Pass task handle directly, not its address [OK]
Common Mistakes:
  • Passing pointer to handle instead of handle
  • Misunderstanding pdFALSE effect
  • Assuming printf syntax error
5. You want to use xTaskNotifyGive() as a lightweight semaphore to synchronize two tasks: Producer and Consumer. The Consumer waits for the notification before processing data. Which sequence correctly implements this synchronization?
hard
A. Producer calls ulTaskNotifyTake(pdTRUE, portMAX_DELAY);, Consumer calls xTaskNotifyGive(ProducerHandle);
B. Consumer calls xTaskNotifyGive() and Producer calls ulTaskNotifyTake()
C. Both tasks call xTaskNotifyGive() simultaneously without waiting
D. Consumer calls ulTaskNotifyTake(pdTRUE, portMAX_DELAY); to wait, Producer calls xTaskNotifyGive(ConsumerHandle); to signal

Solution

  1. Step 1: Identify roles of tasks

    The Consumer should wait for a signal before processing, so it uses ulTaskNotifyTake() to block until notified.
  2. Step 2: Producer signals Consumer

    The Producer sends the notification using xTaskNotifyGive() with the Consumer's task handle.
  3. Final Answer:

    Consumer calls ulTaskNotifyTake(pdTRUE, portMAX_DELAY); to wait, Producer calls xTaskNotifyGive(ConsumerHandle); to signal -> Option D
  4. Quick Check:

    Wait then signal = Consumer calls ulTaskNotifyTake(pdTRUE, portMAX_DELAY); to wait, Producer calls xTaskNotifyGive(ConsumerHandle); to signal [OK]
Hint: Consumer waits, Producer signals with xTaskNotifyGive() [OK]
Common Mistakes:
  • Reversing roles of Producer and Consumer
  • Both tasks signaling without waiting
  • Using wrong task handles in calls
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