Bird
Raised Fist0
FreeRTOSprogramming~20 mins

xSemaphoreCreateMutex() function in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Mutex 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 code snippet using xSemaphoreCreateMutex()?

Consider the following FreeRTOS code snippet. What will be the value of mutexCreated after execution?

FreeRTOS
SemaphoreHandle_t mutex;
BaseType_t mutexCreated;

void setup() {
    mutex = xSemaphoreCreateMutex();
    if (mutex != NULL) {
        mutexCreated = pdTRUE;
    } else {
        mutexCreated = pdFALSE;
    }
}
AmutexCreated will be pdTRUE (1) if the mutex was created successfully.
BmutexCreated will be pdFALSE (0) regardless of creation success.
CmutexCreated will be NULL because xSemaphoreCreateMutex() returns NULL on success.
DmutexCreated will be an undefined value because xSemaphoreCreateMutex() does not return anything.
Attempts:
2 left
💡 Hint

Remember that xSemaphoreCreateMutex() returns a handle or NULL on failure.

🧠 Conceptual
intermediate
1:30remaining
What is the primary purpose of xSemaphoreCreateMutex() in FreeRTOS?

Choose the best description of what xSemaphoreCreateMutex() does in FreeRTOS.

ACreates a counting semaphore with a maximum count of 1.
BCreates a binary semaphore used for task synchronization.
CCreates a mutex semaphore to provide mutual exclusion between tasks.
DCreates a queue to hold multiple data items.
Attempts:
2 left
💡 Hint

Think about what a mutex is used for in multitasking systems.

🔧 Debug
advanced
2:00remaining
Identify the error in this code using xSemaphoreCreateMutex()

What error will this code produce when compiled or run?

FreeRTOS
SemaphoreHandle_t mutex;

void init() {
    xSemaphoreCreateMutex();
}

void task() {
    if (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) {
        // critical section
        xSemaphoreGive(mutex);
    }
}
AxSemaphoreCreateMutex() returns void, so no error occurs.
BThe mutex handle is never assigned, so xSemaphoreTake will use an uninitialized variable causing undefined behavior.
CxSemaphoreTake cannot be called with portMAX_DELAY as a parameter.
DxSemaphoreGive cannot be called without first calling xSemaphoreCreateBinary.
Attempts:
2 left
💡 Hint

Check if the mutex handle is properly initialized before use.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a mutex and checks for success?

Choose the code snippet that correctly creates a mutex and verifies it was created successfully.

ASemaphoreHandle_t mutex = xSemaphoreCreateMutex(); if (mutex == NULL) { /* handle error */ }
BSemaphoreHandle_t mutex; xSemaphoreCreateMutex(&mutex); if (mutex != NULL) { /* success */ }
CSemaphoreHandle_t mutex; mutex = xSemaphoreCreateMutex(); if (mutex == 0) { /* success */ }
DSemaphoreHandle_t mutex = xSemaphoreCreateMutex(); if (mutex != NULL) { /* success */ }
Attempts:
2 left
💡 Hint

Remember the return type and how to check for success.

🚀 Application
expert
2:00remaining
How many items are in the queue underlying a mutex created by xSemaphoreCreateMutex()?

In FreeRTOS, a mutex created by xSemaphoreCreateMutex() is implemented internally as a queue. How many items does this queue hold?

AOne item, representing the mutex being available or taken.
BZero items, because mutexes do not use queues internally.
CMultiple items equal to the number of tasks waiting for the mutex.
DTwo items, one for the mutex and one for priority inheritance.
Attempts:
2 left
💡 Hint

Think about how a mutex represents a single resource that can be taken or given.

Practice

(1/5)
1.

What is the primary purpose of the xSemaphoreCreateMutex() function in FreeRTOS?

easy
A. To delete an existing semaphore
B. To create a counting semaphore for task synchronization
C. To create a mutex for safe access to shared resources
D. To start a new task in the system

Solution

  1. Step 1: Understand the function purpose

    xSemaphoreCreateMutex() is designed to create a mutex, which is a special type of semaphore used to protect shared resources.
  2. Step 2: Compare with other options

    Options A, B, and C describe different FreeRTOS functions or concepts unrelated to mutex creation.
  3. Final Answer:

    To create a mutex for safe access to shared resources -> Option C
  4. Quick Check:

    Mutex creation = safe resource sharing [OK]
Hint: Mutexes protect shared resources safely [OK]
Common Mistakes:
  • Confusing mutex with counting semaphore
  • Thinking it creates a task
  • Assuming it deletes semaphores
2.

Which of the following is the correct way to create a mutex using xSemaphoreCreateMutex() in FreeRTOS?

SemaphoreHandle_t mutex = ?;
easy
A. xSemaphoreCreateMutex();
B. xSemaphoreCreateCounting(1, 0);
C. xTaskCreateMutex();
D. xSemaphoreCreateBinary();

Solution

  1. Step 1: Identify correct function call

    The function to create a mutex is exactly xSemaphoreCreateMutex(), which returns a handle.
  2. Step 2: Eliminate incorrect options

    Options B, C, and D are either different semaphore types or invalid functions.
  3. Final Answer:

    xSemaphoreCreateMutex(); -> Option A
  4. Quick Check:

    Correct mutex creation syntax = xSemaphoreCreateMutex(); [OK]
Hint: Use exact function name xSemaphoreCreateMutex() [OK]
Common Mistakes:
  • Using counting or binary semaphore functions
  • Mistyping function name
  • Confusing task creation with mutex creation
3.

Consider the following code snippet:

SemaphoreHandle_t mutex = xSemaphoreCreateMutex();
if (mutex == NULL) {
    printf("Mutex creation failed\n");
} else {
    printf("Mutex created successfully\n");
}

What will be the output if the mutex is created successfully?

medium
A. Mutex creation failed
B. Compilation error
C. No output
D. Mutex created successfully

Solution

  1. Step 1: Check the return value of xSemaphoreCreateMutex()

    If the mutex is created successfully, the returned handle is not NULL.
  2. Step 2: Follow the if-else logic

    Since mutex != NULL, the else block runs, printing "Mutex created successfully".
  3. Final Answer:

    Mutex created successfully -> Option D
  4. Quick Check:

    Non-NULL handle means success message [OK]
Hint: Check for NULL to confirm creation success [OK]
Common Mistakes:
  • Assuming NULL means success
  • Ignoring the if-else structure
  • Expecting compilation errors
4.

Identify the error in the following code snippet that uses xSemaphoreCreateMutex():

SemaphoreHandle_t mutex;
mutex = xSemaphoreCreateMutex();
if (mutex != NULL) {
    xSemaphoreTake(mutex, 0);
}
// Use shared resource
xSemaphoreGive(mutex);
medium
A. Using zero block time in xSemaphoreTake() may cause immediate failure
B. Mutex handle is not initialized before use
C. xSemaphoreGive() should be called before xSemaphoreTake()
D. xSemaphoreCreateMutex() does not return a handle

Solution

  1. Step 1: Analyze xSemaphoreTake() call with zero block time

    Using 0 as block time means the call will not wait and may fail if mutex is not available immediately.
  2. Step 2: Understand implications for resource use

    If xSemaphoreTake() fails, the shared resource is accessed without protection, causing potential issues.
  3. Final Answer:

    Using zero block time in xSemaphoreTake() may cause immediate failure -> Option A
  4. Quick Check:

    Zero block time risks immediate take failure [OK]
Hint: Avoid zero block time unless sure mutex is free [OK]
Common Mistakes:
  • Assuming zero block time always succeeds
  • Ignoring mutex initialization
  • Misordering take and give calls
5.

You want to protect a shared resource in a FreeRTOS application using a mutex created by xSemaphoreCreateMutex(). Which of the following sequences correctly shows how to use the mutex to safely access the resource?

1. xSemaphoreTake(mutex, portMAX_DELAY);
2. Access shared resource
3. xSemaphoreGive(mutex);
4. Create mutex with xSemaphoreCreateMutex()
hard
A. 1, 4, 2, 3
B. 4, 1, 2, 3
C. 4, 2, 1, 3
D. 1, 2, 3, 4

Solution

  1. Step 1: Identify correct order of operations

    First, create the mutex (step 4), then take it before accessing the resource (step 1), access the resource (step 2), and finally give the mutex back (step 3).
  2. Step 2: Verify sequence correctness

    Only 4, 1, 2, 3 follows the logical order: create, take, access, give.
  3. Final Answer:

    4, 1, 2, 3 -> Option B
  4. Quick Check:

    Create before take, then access, then give [OK]
Hint: Always create mutex before taking it [OK]
Common Mistakes:
  • Taking mutex before creation
  • Accessing resource without taking mutex
  • Giving mutex before accessing resource
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