Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Event group for task synchronization in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event Group Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of event group bits after task synchronization

Consider two tasks that synchronize using an event group. Task A sets bit 0, and Task B waits for bit 0 before setting bit 1. What will be the output of the event bits after both tasks run?

FreeRTOS
EventGroupHandle_t xEventGroup = NULL;

void TaskA(void *pvParameters) {
    xEventGroupSetBits(xEventGroup, (1 << 0));
    vTaskDelete(NULL);
}

void TaskB(void *pvParameters) {
    xEventGroupWaitBits(xEventGroup, (1 << 0), pdFALSE, pdTRUE, portMAX_DELAY);
    xEventGroupSetBits(xEventGroup, (1 << 1));
    vTaskDelete(NULL);
}

int main() {
    xEventGroup = xEventGroupCreate();
    xTaskCreate(TaskA, "TaskA", 1000, NULL, 1, NULL);
    xTaskCreate(TaskB, "TaskB", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    EventBits_t bits = xEventGroupGetBits(xEventGroup);
    printf("Event bits: 0x%02X\n", bits);
    return 0;
}
AEvent bits: 0x01
BEvent bits: 0x03
CEvent bits: 0x02
DEvent bits: 0x00
Attempts:
2 left
💡 Hint

Remember that Task B waits for bit 0 before setting bit 1, so both bits should be set at the end.

❓ Predict Output
intermediate
2:00remaining
Event group wait with clear bits option

What is the output of the event bits after a task waits for bits 0 and 1 with the clear-on-exit option enabled?

FreeRTOS
EventGroupHandle_t xEventGroup = xEventGroupCreate();

xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 1));
EventBits_t bits = xEventGroupWaitBits(xEventGroup, (1 << 0) | (1 << 1), pdTRUE, pdTRUE, 0);
EventBits_t after_wait = xEventGroupGetBits(xEventGroup);
printf("Bits returned by wait: 0x%02X\n", bits);
printf("Bits after wait: 0x%02X\n", after_wait);
A
Bits returned by wait: 0x02
Bits after wait: 0x01
B
Bits returned by wait: 0x00
Bits after wait: 0x03
C
Bits returned by wait: 0x03
Bits after wait: 0x00
D
Bits returned by wait: 0x01
Bits after wait: 0x02
Attempts:
2 left
💡 Hint

Clear-on-exit means bits are cleared after the wait returns.

🔧 Debug
advanced
2:00remaining
Identify the error in event group usage

What error will occur in the following code snippet that uses an event group for synchronization?

FreeRTOS
EventGroupHandle_t xEventGroup;

void Task(void *pvParameters) {
    xEventGroupSetBits(xEventGroup, (1 << 0));
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(Task, "Task", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ANo error, code runs correctly
BSyntax error: missing semicolon after xEventGroup declaration
CDeadlock: Task waits forever for a bit that is never set
DRuntime error: xEventGroup is NULL causing a crash
Attempts:
2 left
💡 Hint

Check if the event group is created before use.

❓ Predict Output
advanced
2:00remaining
Event group bits after multiple tasks set bits

Three tasks set bits 0, 1, and 2 respectively on the same event group. What is the final value of the event bits?

FreeRTOS
EventGroupHandle_t xEventGroup = xEventGroupCreate();

void Task0(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 0)); vTaskDelete(NULL); }
void Task1(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 1)); vTaskDelete(NULL); }
void Task2(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 2)); vTaskDelete(NULL); }

int main() {
    xTaskCreate(Task0, "Task0", 1000, NULL, 1, NULL);
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    EventBits_t bits = xEventGroupGetBits(xEventGroup);
    printf("Event bits: 0x%02X\n", bits);
    return 0;
}
AEvent bits: 0x07
BEvent bits: 0x01
CEvent bits: 0x03
DEvent bits: 0x00
Attempts:
2 left
💡 Hint

Bits 0, 1, and 2 correspond to 0x01, 0x02, and 0x04 respectively.

🧠 Conceptual
expert
3:00remaining
Effect of auto-clear bits on multiple waiting tasks

Two tasks wait on the same event group bits 0 and 1 with the clear-on-exit option enabled. If bit 0 and bit 1 are set once, what happens to the second task waiting for those bits?

AThe first task unblocks and clears the bits; the second task waits indefinitely because bits are cleared
BBoth tasks unblock simultaneously because bits are set
CThe bits remain set after the first task, so the second task unblocks immediately
DBoth tasks wait forever because bits are never set
Attempts:
2 left
💡 Hint

Consider how clear-on-exit affects bits after the first task unblocks.

Practice

(1/5)
1. What is the primary purpose of an event group in FreeRTOS for task synchronization?
easy
A. To allocate memory dynamically for tasks
B. To allow tasks to wait for multiple events using bits
C. To create queues for message passing
D. To manage task priorities automatically

Solution

  1. Step 1: Understand event groups in FreeRTOS

    Event groups use bits to represent events that tasks can wait for or signal.
  2. Step 2: Identify the main use of event groups

    They help tasks synchronize by waiting on or setting bits representing events.
  3. Final Answer:

    To allow tasks to wait for multiple events using bits -> Option B
  4. Quick Check:

    Event groups = wait for multiple events [OK]
Hint: Event groups use bits to sync tasks, not memory or priorities [OK]
Common Mistakes:
  • Confusing event groups with queues
  • Thinking event groups manage memory
  • Assuming event groups change task priorities
2. Which of the following is the correct function to wait for bits in an event group in FreeRTOS?
easy
A. xEventGroupClearBits()
B. xEventGroupSetBits()
C. xEventGroupWaitBits()
D. xEventGroupCreate()

Solution

  1. Step 1: Recall FreeRTOS event group functions

    xEventGroupWaitBits() is used to wait for specific bits to be set.
  2. Step 2: Differentiate from other functions

    xEventGroupSetBits() sets bits, ClearBits clears bits, Create creates the group.
  3. Final Answer:

    xEventGroupWaitBits() -> Option C
  4. Quick Check:

    Wait for bits = xEventGroupWaitBits() [OK]
Hint: Wait uses xEventGroupWaitBits(), set uses xEventGroupSetBits() [OK]
Common Mistakes:
  • Using xEventGroupSetBits() to wait
  • Confusing create with wait functions
  • Mixing clear bits with wait bits
3. Given the following code snippet, what will be the value of bits after xEventGroupWaitBits() returns?
EventBits_t bits;
bits = xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY);
Assume BIT_0 and BIT_1 are both set in eventGroup before the call.
medium
A. Only BIT_0 set
B. Only BIT_1 set
C. No bits set
D. Both BIT_0 and BIT_1 set

Solution

  1. Step 1: Understand xEventGroupWaitBits parameters

    The function waits for BIT_0 or BIT_1 to be set, clears bits on exit (pdTRUE), and does not wait for all bits (pdFALSE).
  2. Step 2: Analyze the bits set before call

    Both BIT_0 and BIT_1 are set, so the function returns with both bits set in the return value.
  3. Final Answer:

    Both BIT_0 and BIT_1 set -> Option D
  4. Quick Check:

    WaitBits returns bits set before clearing [OK]
Hint: WaitBits returns bits set before clearing them [OK]
Common Mistakes:
  • Assuming only one bit is returned
  • Ignoring bits cleared on exit
  • Confusing wait for all bits with wait for any
4. Identify the error in the following FreeRTOS event group usage:
EventGroupHandle_t eventGroup = xEventGroupCreate();

void Task1(void *pvParameters) {
    xEventGroupWaitBits(eventGroup, BIT_0, pdFALSE, pdTRUE, portMAX_DELAY);
    // Task code
}

void Task2(void *pvParameters) {
    xEventGroupSetBits(eventGroup, BIT_0);
}
medium
A. Not checking if eventGroup creation succeeded
B. Using portMAX_DELAY as wait time
C. Using pdFALSE for clearOnExit in xEventGroupWaitBits
D. Setting bits before event group creation

Solution

  1. Step 1: Check event group creation

    xEventGroupCreate() can return NULL if creation fails; code does not check this.
  2. Step 2: Verify other parameters

    Using pdFALSE for clearOnExit is valid; portMAX_DELAY is valid; bits are set after creation.
  3. Final Answer:

    Not checking if eventGroup creation succeeded -> Option A
  4. Quick Check:

    Always check event group creation success [OK]
Hint: Always verify event group creation returned non-NULL [OK]
Common Mistakes:
  • Ignoring NULL return from xEventGroupCreate
  • Misusing clearOnExit parameter
  • Setting bits before creating event group
5. You want two tasks to synchronize so that Task A waits until Task B signals both BIT_0 and BIT_1. Which xEventGroupWaitBits() call correctly waits for both bits before continuing?
hard
A. xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
B. xEventGroupWaitBits(eventGroup, BIT_0 & BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
C. xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY);
D. xEventGroupWaitBits(eventGroup, BIT_0 & BIT_1, pdFALSE, pdFALSE, portMAX_DELAY);

Solution

  1. Step 1: Understand wait for all bits parameter

    The fourth parameter (xWaitForAllBits) must be pdTRUE to wait for both bits together.
  2. Step 2: Check bit mask and parameters

    Use BIT_0 | BIT_1 to specify both bits; pdTRUE for clearOnExit clears bits after wait.
  3. Final Answer:

    xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY); -> Option A
  4. Quick Check:

    Wait for all bits = xEventGroupWaitBits with xWaitForAllBits = pdTRUE [OK]
Hint: Set xWaitForAllBits to pdTRUE to wait for all bits [OK]
Common Mistakes:
  • Using bitwise AND (&) instead of OR (|) for bit mask
  • Setting xWaitForAllBits to pdFALSE when waiting for all bits
  • Confusing clearOnExit with wait for all bits
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