Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Semaphores and mutex play an important role while dealing with different processes in a multiprogramming environment. They are responsible for synchronizing the processes by introducing locks in the block. But, one big misconception that often arises among readers is that semaphores and mutex are the same terms, which is not the case. Although they work similarly, there are some differences between them.
This blog will help you understand the concept of semaphores and mutex better, along with their differences given below.
Semaphore and mutex work similarly, so it is hard to determine which one to use in different instances. Semaphores are faster than mutex because any other process can unlock the semaphore, which isn’t the case in mutex. It requires the same thread to unlock it which has acquired it.
So, in conclusion, we can say that in all those instances where we have several resources, semaphores should be preferred, whereas, in the case of a single instance of a resource, mutex should be chosen.
No, a mutex isn’t busy waiting. It puts all the threads waiting for the lock into sleep(blocked).
What is the difference between spinlocks and mutex?
A spinlock is a lock that causes a thread trying to acquire it to simply wait in the loop and repeatedly check for its availability whereas a mutex is a program object that is created so that multiple processes can take turns sharing the same resource.
Why are spinlocks avoided?
They become wasteful if held for a longer time, as they may prevent other threads from running and may require rescheduling.
Is spinlock a deadlock?
Deadlock is a problem in concurrent programming whereas spinlock is a solution for threads so that no two threads can access the same resources concurrently.
Can busy waiting be avoided?
Busy waiting cannot be avoided. While it can be avoided on uniprocessors, it is unavoidable mainly on multiprocessor machines, so it’s best that busy waiting is used for a brief wait period.
What is CPU spinning?
CPU spinning occurs when a certain thread gets into either an infinite loop or is running a computation-intensive operation that takes a long time.
Conclusion
This article discussed the difference between semaphores and mutex in the operating system and all the information one needs to know about them.