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
Step 1: Check the return value of xSemaphoreCreateMutex()
If the mutex is created successfully, the returned handle is not NULL.
Step 2: Follow the if-else logic
Since mutex != NULL, the else block runs, printing "Mutex created successfully".
Final Answer:
Mutex created successfully -> Option D
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
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.
Step 2: Understand implications for resource use
If xSemaphoreTake() fails, the shared resource is accessed without protection, causing potential issues.
Final Answer:
Using zero block time in xSemaphoreTake() may cause immediate failure -> Option A
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?
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).
Step 2: Verify sequence correctness
Only 4, 1, 2, 3 follows the logical order: create, take, access, give.
Final Answer:
4, 1, 2, 3 -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.