0
0
FreeRTOSprogramming~20 mins

Multiple tasks running concurrently in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Concurrency 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 scheduling code?

Consider two tasks created with the same priority in FreeRTOS. Each task prints a message and then delays for 100 ticks. What will be the output sequence?

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

void Task1(void *pvParameters) {
    while(1) {
        printf("Task 1 running\n");
        vTaskDelay(100);
    }
}

void Task2(void *pvParameters) {
    while(1) {
        printf("Task 2 running\n");
        vTaskDelay(100);
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask 1 running\nTask 2 running\nTask 1 running\nTask 2 running\n... (alternating)
BTask 1 running\nTask 1 running\nTask 1 running\nTask 2 running\nTask 2 running\nTask 2 running\n...
CTask 2 running\nTask 1 running\nTask 2 running\nTask 1 running\n... (alternating)
DOnly Task 1 runs repeatedly; Task 2 never runs.
Attempts:
2 left
💡 Hint

Tasks with the same priority and equal delay times will share CPU time in a round-robin manner.

🧠 Conceptual
intermediate
1:00remaining
Which FreeRTOS function is used to create a new task?

In FreeRTOS, what is the correct function to create a new task that will run concurrently?

AvTaskRun()
BxTaskCreate()
CvTaskStart()
DxCreateTask()
Attempts:
2 left
💡 Hint

Look for the function starting with 'x' that creates tasks.

🔧 Debug
advanced
2:00remaining
Why does this FreeRTOS code cause a stack overflow?

Examine the following task code. Why might it cause a stack overflow?

FreeRTOS
void Task(void *pvParameters) {
    char buffer[1024];
    while(1) {
        // Do some work
        vTaskDelay(10);
    }
}

int main(void) {
    xTaskCreate(Task, "Task", 100, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
AThe stack size (100) is too small for the 1024-byte buffer, causing overflow.
BThe task priority is too high, causing stack overflow.
CThe task function is missing a return statement, causing overflow.
DvTaskDelay(10) causes the task to never yield, leading to overflow.
Attempts:
2 left
💡 Hint

Consider the size of local variables versus the stack size allocated.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this FreeRTOS task creation code

Which option contains the correct syntax to create a FreeRTOS task?

AxTaskCreate(TaskFunction, Task, 256, NULL, 2, NULL);
BxTaskCreate(TaskFunction, "Task", 256, NULL, 2);
CxTaskCreate("TaskFunction", "Task", 256, NULL, 2, NULL);
DxTaskCreate(TaskFunction, "Task", 256, NULL, 2, NULL);
Attempts:
2 left
💡 Hint

Check the parameter types and count for xTaskCreate.

🚀 Application
expert
2:30remaining
How to ensure two FreeRTOS tasks do not access a shared variable simultaneously?

You have two tasks that both read and write a shared variable. Which method ensures safe concurrent access?

AIncrease task priorities so one runs before the other.
BUse vTaskDelay() in both tasks to avoid overlap.
CUse a mutex to lock the variable during access.
DDeclare the variable as volatile to prevent conflicts.
Attempts:
2 left
💡 Hint

Think about synchronization tools FreeRTOS provides.