A mutex is a special type of semaphore used to protect shared resources by allowing only one task to access the resource at a time. It includes priority inheritance to avoid priority inversion.
Click to reveal answer
beginner
What is a binary semaphore in FreeRTOS?
A binary semaphore is a synchronization tool that can take only two values: 0 or 1. It is used to signal events or synchronize tasks but does not support priority inheritance.
Click to reveal answer
intermediate
How does priority inheritance differ between mutex and binary semaphore?
Mutexes support priority inheritance to prevent priority inversion, temporarily boosting the priority of the task holding the mutex. Binary semaphores do not support priority inheritance.
Click to reveal answer
intermediate
Can a binary semaphore be used for mutual exclusion like a mutex?
While a binary semaphore can be used for mutual exclusion, it lacks priority inheritance and is generally better suited for signaling events rather than protecting shared resources.
Click to reveal answer
intermediate
Why is a mutex preferred over a binary semaphore for resource protection?
A mutex is preferred because it includes priority inheritance, which helps avoid priority inversion problems when multiple tasks with different priorities access the same resource.
Click to reveal answer
Which synchronization tool in FreeRTOS supports priority inheritance?
ACounting semaphore
BBinary semaphore
CEvent group
DMutex
✗ Incorrect
Mutexes support priority inheritance to prevent priority inversion, while binary semaphores do not.
What is the main use of a binary semaphore in FreeRTOS?
AProtecting shared resources with priority inheritance
BCounting multiple resources
CSignaling events between tasks
DScheduling tasks
✗ Incorrect
Binary semaphores are mainly used for signaling events or synchronizing tasks.
Which of the following is true about mutexes?
AThey are used only for signaling events
BThey include priority inheritance to avoid priority inversion
CThey can only take values 0 or 1 and do not support priority inheritance
DThey are not suitable for resource protection
✗ Incorrect
Mutexes include priority inheritance, making them suitable for protecting shared resources.
Can a binary semaphore be used to protect a shared resource?
AYes, but it lacks priority inheritance
BNo, it cannot be used for resource protection
CYes, and it supports priority inheritance
DNo, it is only for counting resources
✗ Incorrect
Binary semaphores can be used for mutual exclusion but do not support priority inheritance.
What problem does priority inheritance in mutexes help to solve?
APriority inversion
BDeadlock
CRace conditions
DMemory leaks
✗ Incorrect
Priority inheritance helps prevent priority inversion, where a low-priority task blocks a higher-priority task.
Explain the key differences between a mutex and a binary semaphore in FreeRTOS.
Think about how each handles task priorities and their main use cases.
You got /5 concepts.
Describe a scenario where you would choose a binary semaphore over a mutex in FreeRTOS.
Consider event signaling versus resource locking.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes the main difference between a mutex and a binary semaphore in FreeRTOS?
easy
A. Binary semaphores have ownership; mutexes do not.
B. Mutexes have ownership and priority inheritance; binary semaphores do not.
C. Mutexes are used only for signaling; binary semaphores are for exclusive access.
D. There is no difference; both behave exactly the same.
Solution
Step 1: Understand ownership concept
Mutexes have ownership, meaning the task that locks it must unlock it. Binary semaphores do not have ownership.
Step 2: Understand priority inheritance
Mutexes support priority inheritance to avoid priority inversion; binary semaphores do not.
Final Answer:
Mutexes have ownership and priority inheritance; binary semaphores do not. -> Option B
B. Taking the semaphore with portMAX_DELAY is invalid.
C. Binary semaphore must be created with xSemaphoreCreateMutex().
D. Giving the semaphore twice causes incorrect behavior.
Solution
Step 1: Understand binary semaphore usage
Binary semaphores act like flags with max count 1. Giving twice without taking in between can cause logic errors.
Step 2: Analyze the code's semaphore give calls
The code gives the semaphore twice without an intervening take, which can lead to unexpected behavior or overflow.
Final Answer:
Giving the semaphore twice causes incorrect behavior. -> Option D
Quick Check:
Binary semaphore max count = 1, double give is wrong [OK]
Hint: Binary semaphore max count 1, don't give twice without take [OK]
Common Mistakes:
Thinking portMAX_DELAY is invalid
Confusing binary semaphore with mutex creation
Ignoring semaphore initialization
5. You have two tasks: Task A (high priority) and Task B (low priority). Both need exclusive access to a shared resource. Which synchronization method should you use to avoid priority inversion and why?
A) Use a mutex because it supports priority inheritance.
B) Use a binary semaphore because it is simpler.
C) Use a counting semaphore to allow multiple accesses.
D) Use no synchronization to improve performance.
hard
A. Use a counting semaphore to allow multiple accesses.
B. Use a binary semaphore because it is simpler.
C. Use a mutex because it supports priority inheritance.
D. Use no synchronization to improve performance.
Solution
Step 1: Identify need for exclusive access with priority handling
Both tasks need exclusive access, so synchronization is required. Priority inversion can occur if a low priority task holds the resource.
Step 2: Choose synchronization with priority inheritance
Mutexes provide priority inheritance, temporarily boosting the low priority task's priority to avoid blocking the high priority task.
Final Answer:
Use a mutex because it supports priority inheritance. -> Option C
Quick Check:
Priority inversion avoidance = mutex [OK]
Hint: Use mutex for priority inheritance and exclusive access [OK]