What is a semaphore?
A semaphore is a non-negative variable and is shared among threads. A semaphore is a signaling mechanism where one thread is waiting for a semaphore to be emitted by another thread. It uses two atomic operations, 1) wait
and 2) signal
to achieve thread synchronization.
A semaphore implementation allows or disallows access to the resource, depending on how it is set up.
What is a mutex?
The full form of mutual exclusion is the mutex object. It is a special type of binary signal used to control access to shared resources. It includes a priority inheritance mechanism to avoid extension priority inversion problems. It allows the currently higher priority task to remain blocked for as short a time as possible. However, priority inheritance does not solve the priority inversion problem, only minimizes its impact.
Main difference:
A mutex is a lock mechanism, a semaphore is a signal mechanism, a mutex is an object, and a semaphore is an integer. Mutex has no subtype, and a semaphore has two subtypes, counting semaphore and binary semaphore semaphore The semaphore supports wait and signal operation modification, and the mutex can only be modified by the process that may request or release the resource to modify the value of thewait()
andsignal()
These two methods are modified, while Mutexes are operated using lock and unlock.
use semaphore
In the case of a single buffer, we can split the 4KB buffer into four 1KB buffers. A semaphore can be associated with these four buffers. This allows users and producers to work on different buffers at the same time.
use mutex
Mutual exclusion locks provide the function of mutual exclusion. Both producers and consumers can hold locks. The party holding the lock can continue to work, while the other party has to wait. At the same time, only one thread can process the entire buffer. Area.
Common Facts About Mutexes and Semaphores
- Only one task can acquire the mutex, the mutex has ownership, and only the task holding the lock can release the mutex.
- The scenarios for using mutexes and semaphores are different, but because of the similarities in implementation, mutexes are also called binary semaphores
- A well-known mistake: Mutex and Semaphore are almost the same, the only difference is that a Mutex can count up to 1, while a Semaphore can count from 0 to N
- There is always uncertainty between binary semaphores and mutexes. You may have heard that a mutex is a binary semaphore, this is not true
Advantages of semaphores
- Allow multiple threads to access critical sections
- Semaphores are machine independent (as they are implemented in kernel services)
- Multiple processes are not allowed to enter the critical section.
- The semaphore has a busy state, because it will not waste time and resources
Advantages of Mutexes
- A mutex is just a simple lock, it is held when entering a critical section and released when leaving
- Since only one thread is in the critical section at any given time, there is no data race and data consistency is always maintained
Disadvantages of semaphores
- One of the biggest limitations of semaphores is the priority inversion problem
- The operating system must keep track of all semaphore calls
- To avoid deadlocks in semaphores,
wait
andsignal
operations need to be executed in the correct order - Semaphore programming is a complex approach, so it is possible that mutual exclusion cannot be achieved
- It's also not a practical method that can be used at scale, as their use would break modularity
- Programmers using semaphores are more error-prone and prone to deadlocks
Disadvantages of Mutexes
- If a thread holding a lock sleeps or is occupied by the CPU, other threads cannot continue to execute
- Only one thread is allowed to access the critical section at a time
- Normal implementation may result in busy-wait state, wasting CPU time
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。