Bird
Raised Fist0
FreeRTOSprogramming~10 mins

xSemaphoreCreateMutex() function in FreeRTOS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mutex semaphore.

FreeRTOS
SemaphoreHandle_t mutex = [1]();
Drag options to blanks, or click blank then click option'
AxSemaphoreCreateMutex
BxQueueCreate
CxTaskCreate
DvSemaphoreDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using xQueueCreate instead of xSemaphoreCreateMutex
Trying to create a task instead of a semaphore
Deleting semaphore instead of creating it
2fill in blank
medium

Complete the code to take the mutex semaphore with a block time of 100 ticks.

FreeRTOS
if (xSemaphoreTake(mutex, [1]) == pdTRUE) {
    // Critical section
}
Drag options to blanks, or click blank then click option'
ApdFALSE
B0
CportMAX_DELAY
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which means no wait
Using pdFALSE which is not a valid block time
Using portMAX_DELAY which means wait forever
3fill in blank
hard

Fix the error in the code to correctly give back the mutex semaphore.

FreeRTOS
xSemaphore[1](mutex);
Drag options to blanks, or click blank then click option'
ACreateMutex
BTake
CGive
DDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using xSemaphoreTake instead of xSemaphoreGive
Using xSemaphoreCreateMutex which creates a mutex
Using xSemaphoreDelete which deletes the semaphore
4fill in blank
hard

Fill both blanks to create a mutex and check if creation was successful.

FreeRTOS
SemaphoreHandle_t mutex = [1]();
if (mutex == [2]) {
    // Handle error
}
Drag options to blanks, or click blank then click option'
AxSemaphoreCreateMutex
BNULL
CpdTRUE
DxSemaphoreTake
Attempts:
3 left
💡 Hint
Common Mistakes
Checking against pdTRUE instead of NULL
Using xSemaphoreTake instead of creation function
Not checking for NULL pointer
5fill in blank
hard

Fill all three blanks to create a mutex, take it with no wait, and then give it back.

FreeRTOS
SemaphoreHandle_t mutex = [1]();
if (xSemaphoreTake(mutex, [2]) == pdTRUE) {
    xSemaphore[3](mutex);
}
Drag options to blanks, or click blank then click option'
AxSemaphoreCreateMutex
B0
CGive
DTake
Attempts:
3 left
💡 Hint
Common Mistakes
Using portMAX_DELAY instead of 0 for block time
Using xSemaphoreTake instead of xSemaphoreGive to release
Not creating the mutex before taking it

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